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%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
分享
微博
QQ
微信
回复
2024-12-20 19:49:39
相关问题
HarmonyOS CanvasRenderingContext2D使用问题
857浏览 • 1回复 待解决
使用Drawing进行2d图像绘制
1853浏览 • 1回复 待解决
HarmonyOS Image组件如何设置填充颜色
956浏览 • 1回复 待解决
ArkGraphics 2D都有哪些使用场景?
1726浏览 • 1回复 待解决