HarmonyOS Cavans没有找到类似画笔清除模式

HarmonyOS Canvas有没有像给画笔设置清除模式的api,用于清除已绘制的路径

其他系统代码:

mPaint.setColor(Color.TRANSPARENT);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

示例参考如下:

@Entry
@Component
export struct CanvasDemo {
  @State paintSize: number = 5 // 当前画笔大小
  @State paintColor: Color = Color.Black // 当前画笔颜色
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  canvasContext: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  //签字区域
  guessContext: CanvasRenderingContext2D = new CanvasRenderingContext2D(new RenderingContextSettings(true))
  tempPath: Path2D = new Path2D() //
  @State pathArray: Array<Path2D | undefined> = [] // 所有画图路径信息
  @State removeArray: Array<Path2D | undefined> = [] // 回退的路径集合
  @State text: string = ''
  @State eventType: string = ''
  @State mTargetAngelY: number = 40

  build() {
    Column() {
      Row() {
        Button("橡皮擦")
          .onClick(() => {
            this.canvasContext.globalCompositeOperation = 'destination-out';
            this.paintSize = 50;
          })
        Button("画笔")
          .onClick(() => {
            this.canvasContext.globalCompositeOperation = 'source-over';
            this.paintSize = 5;
          })

        Button("清屏")
          .onClick(() => {
            this.canvasContext.clearRect(0, 0, 360, 720)
          })
      }
      .margin({ top: 25 })
      .width('100%')
      .justifyContent(FlexAlign.SpaceEvenly)

      Stack({ alignContent: Alignment.Top }) {
        Canvas(this.canvasContext)
          .width('100%')
          .height('100%')
          .backgroundColor(Color.Yellow)
          .onReady(() => {
            this.pathArray = []
            this.canvasContext.strokeStyle = this.paintColor
            this.canvasContext.lineWidth = this.paintSize
            this.canvasContext.stroke(this.tempPath)
            for (let index = 0; index < this.pathArray.length; index++) {
              this.canvasContext.stroke(this.pathArray[index])
            }
          })
          .onTouch((event?: TouchEvent) => {
            if (event) {
              if (event.type === TouchType.Down) {
                this.eventType = 'Down'
                this.canvasContext.beginPath()
                this.tempPath = new Path2D()
                this.pathArray.push(this.tempPath)
                this.tempPath.moveTo(event.touches[0].x, event.touches[0].y)
                this.canvasContext.moveTo(event.touches[0].x, event.touches[0].y)
              }
              if (event.type === TouchType.Up) {
                this.eventType = 'Up'
              }
              if (event.type === TouchType.Move) {
                this.eventType = 'Move'
                this.tempPath.lineTo(event.touches[0].x, event.touches[0].y)
                this.canvasContext.stroke(this.tempPath)
              }
              this.text = 'TouchType:' + this.eventType + '\n touch point and touch element:\nx: '
                + event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\nwidth:'
                + event.target.area.width + '\nheight:' + event.target.area.height + '\npathArray size:' +
              this.pathArray.length
            }
          })
        Text(this.text)
      }
    }
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
AlertDialog没有找到设置背景色API
625浏览 • 1回复 待解决
没有找到相应的MD5算法实现
891浏览 • 1回复 待解决
如何清除Web隐私模式下所有cookie。
806浏览 • 1回复 待解决
HarmonyOS没有清除通知相关的API
319浏览 • 1回复 待解决
HarmonyOS APP有没有清除缓存的接口
690浏览 • 1回复 待解决
HarmonyOS 文档中没有找到DES加解密
518浏览 • 1回复 待解决
图片进行画笔或者马赛克绘制
1172浏览 • 1回复 待解决
Canvas画的时候,怎么设置画笔颜色
623浏览 • 1回复 待解决