【如此之白】简单的canvas绘图板 原创

如此之白
发布于 2022-4-14 14:37
9241浏览
5收藏

一个简单的绘图板
【如此之白】简单的canvas绘图板-鸿蒙开发者社区

快速体验ets canvas绘图

@Entry
@Component
struct Index {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private ctx: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  @State x: number = 0
  @State y: number = 0
  @State startX: number = 0
  @State startY: number = 0
  private colors = ['#000000','#ff0000','#ffff00','#0000ff','#ff00ff']

  build() {
    Stack() {
      Canvas(this.ctx)
        .width('100%')
        .height('100%')
        .onReady(() => {
          this.ctx.lineWidth = 6
          this.ctx.strokeStyle = '#000000'
          this.ctx.lineCap = 'round'
          this.ctx.lineJoin = 'round'
        })
        .onTouch(e => {
          let pos = e.touches[0]
          if (!pos) return

          if (e.type == TouchType.Down) {
            this.ctx.restore()
            this.ctx.beginPath()
            this.startX = pos.x
            this.startY = pos.y
          } else if (e.type == TouchType.Move) {
            this.x = pos.x
            this.y = pos.y

            this.ctx.moveTo(this.startX, this.startY)
            this.ctx.lineTo(this.x, this.y)
            this.ctx.stroke()

            this.startX = pos.x
            this.startY = pos.y
          } else if (e.type == TouchType.Up) {
            this.ctx.save()
          }
        })

      Flex({justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center}) {
        ForEach(this.colors,(color)=>{
          Row(){}.backgroundColor(color).width(50).height(50)
          .onClick(()=>{
            this.ctx.restore()
            this.ctx.strokeStyle = color
          })
        })
      }
      .height(50)
      .width('100%')
      .backgroundColor('rgba(0,0,0,0.5)')
      .position({
        x: 0,
        y: 0
      })
    }
    .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.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.

参考文档地址:
https://gitee.com/openharmony/docs/blob/5654c2b940ab3e2f4f0baf435e630c4ef3536428/zh-cn/application-dev/reference/arkui-ts/ts-components-canvas-canvas.md
https://gitee.com/openharmony/docs/blob/5654c2b940ab3e2f4f0baf435e630c4ef3536428/zh-cn/application-dev/reference/arkui-ts/ts-canvasrenderingcontext2d.md

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
已于2022-9-7 11:46:48修改
7
收藏 5
回复
举报
7
4
5
4条回复
按时间正序
/
按时间倒序
红叶亦知秋
红叶亦知秋

简单实用,感觉会很适合我

2
回复
2022-4-14 14:39:58
民之码农
民之码农

666

1
回复
2022-4-15 08:49:00
科技维度
科技维度

好东西,就是内容有点太简单了

1
回复
2022-4-19 09:00:17
淼淼似水
淼淼似水

向美女学习学习

1
回复
2022-4-19 23:07:58


回复
    相关推荐