#创作者激励#HarmonyOS/OpenHarmony应用开发ArkTS画布组件(二) 原创

鸿蒙时代
发布于 2023-3-1 09:40
浏览
0收藏

【本文正在参加2023年第一期优质创作者激励计划】
HarmonyOS/OpenHarmony应用开发-ArkTS画布组件-CanvasRenderingContext2D对象(二
::: hljs-center

**fillStyle、lineWidth、strokeStyle **
::: hljs-left

fillStyle

代码:

// xxx.ets
@Entry
@Component
struct FillStyleExample {
  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.fillStyle = '#0000ff'
          this.context.fillRect(20, 160, 150, 100)
        })
    }
    .width('100%')
    .height('100%')
  }
}

效果
#创作者激励#HarmonyOS/OpenHarmony应用开发ArkTS画布组件(二)-鸿蒙开发者社区
lineWidth
代码:

// xxx.ets
@Entry
@Component
struct LineWidthExample {
  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.lineWidth = 5
        this.context.strokeRect(25, 25, 85, 105)
      })
    }
    .width('100%')
    .height('100%')
  }
}

效果
#创作者激励#HarmonyOS/OpenHarmony应用开发ArkTS画布组件(二)-鸿蒙开发者社区
strokeStyle
代码

// xxx.ets
@Entry
@Component
struct StrokeStyleExample {
  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.lineWidth = 10
          this.context.strokeStyle = '#0000ff'
          this.context.strokeRect(25, 25, 155, 105)
        })
    }
    .width('100%')
    .height('100%')
  }
}

效果
#创作者激励#HarmonyOS/OpenHarmony应用开发ArkTS画布组件(二)-鸿蒙开发者社区

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
标签
HarmonyOSOpenHarmony应用开发-ArkTS画布组.doc 33.6K 10次下载
收藏
回复
举报
回复
    相关推荐