HarmonyOS 怎样使用CanvasRenderingContext2D在特定形状内填充颜色

怎样使用 CanvasRenderingContext2D 在特定形状内填充颜色,如:圆角矩形 ,贝塞尔曲线和其它直线组合的区域。

HarmonyOS
2024-12-20 16:30:00
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu
@Entry
@Component
struct BezierCurveTo {
  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)
        .backgroundColor('#ffff00')
        .width('100%')
        .height('100%')
        .onReady(() => {
          this.context.strokeStyle = "#333"
          this.context.fillStyle = '#ffe514d0'
          this.context.lineWidth = 2
          this.context.beginPath()
          this.context.moveTo(30, 30)
          this.context.lineTo(30, 150)
          this.context.lineTo(100, 150)
          this.context.lineTo(100, 80)
          this.context.bezierCurveTo(75, 20, 75, 20, 30, 30)
          this.context.stroke()

          this.context.fill()
          this.context.beginPath()
          this.context.moveTo(100, 80)
          this.context.lineTo(100, 150)
          this.context.lineTo(150, 150)
          this.context.lineTo(150, 120)
          this.context.bezierCurveTo(130, 90, 160, 30, 100, 80)
          this.context.stroke()
          this.context.fillStyle = '#ff1f7fe5'
          this.context.fill()
        })
    }
    .width('100%')
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
2024-12-20 19:49:39
相关问题
HarmonyOS CanvasRenderingContext2D使用问题
474浏览 • 1回复 待解决
HarmonyOS Image组件如何设置填充颜色
555浏览 • 1回复 待解决
使用Drawing进行2d图像绘制
1439浏览 • 1回复 待解决
ArkGraphics 2D都有哪些使用场景?
1259浏览 • 1回复 待解决