HarmonyOS/OpenHarmony应用开发-ArkTS画布组件(十七)

鸿蒙时代
发布于 2023-5-5 10:48
浏览
0收藏

rect
rect(x: number, y: number, w: number, h: number): void
创建矩形路径。
HarmonyOS/OpenHarmony应用开发-ArkTS画布组件(十七)-鸿蒙开发者社区
示例

// xxx.ets
@Entry
@Component
struct CanvasExample {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.context.rect(20, 20, 100, 100) // Create a 100*100 rectangle at (20, 20)
          this.context.stroke()
        })
    }
    .width('100%')
    .height('100%')
  }
}

HarmonyOS/OpenHarmony应用开发-ArkTS画布组件(十七)-鸿蒙开发者社区
fill
fill(fillRule?: CanvasFillRule): void
对封闭路径进行填充。
HarmonyOS/OpenHarmony应用开发-ArkTS画布组件(十七)-鸿蒙开发者社区
示例

// xxx.ets
@Entry
@Component
struct Fill {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.context.rect(20, 20, 100, 100) // Create a 100*100 rectangle at (20, 20)
          this.context.fill()
        })
    }
    .width('100%')
    .height('100%')
  }
}

HarmonyOS/OpenHarmony应用开发-ArkTS画布组件(十七)-鸿蒙开发者社区

标签
HarmonyOSOpenHarmony应用开发-ArkTS画布组.docx 25.47K 10次下载
收藏
回复
举报
回复
    相关推荐