HarmonyOS 如何实现马赛克功能

HarmonyOS
2025-01-09 16:21:40
1043浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

参考示例:

export class DrawPathPointModel {
  x: number = 0
  y: number = 0
}

export enum DrawPathType {
  pen = 0,     //画笔
  pattern,     //马赛克
}

// 配置画笔
export class DrawPathModel {

  public pathType: DrawPathType = DrawPathType.pen

  public color: string = "#ED1B1B"
  public lineWidth: number = 8

  public img: ImageBitmap = new ImageBitmap("Images/canvas.png")

}

@Observed
export class DrawViewModel {
  public settings: RenderingContextSettings = new RenderingContextSettings(true)
  public context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  public drawModel: DrawPathModel = new DrawPathModel()
  public canvasHeight: number = 0
  public canvasWidth: number = 0

  private pattern: CanvasPattern | null
  private points: DrawPathPointModel[] = []
  // 绘制路径
  private drawPath = new Path2D()

  constructor() {
    this.pattern = this.context.createPattern(this.drawModel.img, 'repeat')
  }

  moveStart(x: number, y: number) {
    this.points.push({x: x, y: y})
    this.drawPath.moveTo(x, y)
    this.drawCurrentPathModel()
  }

  moveUpdate(x: number, y: number) {
    let lastPoint = this.points[this.points.length - 1]
    this.points.push({x: x, y: y})
    this.drawPath.quadraticCurveTo((x + lastPoint.x) / 2, (y + lastPoint.y) / 2, x, y)
    this.drawCurrentPathModel()
  }

  moveEnd() {
    this.points = []
    this.drawPath = new Path2D()
  }

  clearPath() {
    this.points = []
    this.drawPath = new Path2D()
    this.context.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
  }

  canvasAreaChange(area: Area) {
    this.canvasHeight = area.height as number
    this.canvasWidth = area.width as number
  }

  private drawCurrentPathModel() {
    this.context.globalCompositeOperation = 'source-over'
    this.context.lineWidth = this.drawModel.lineWidth
    if (this.drawModel.pathType == DrawPathType.pen) {
      this.context.strokeStyle = this.drawModel.color
    } else {
      if (this.pattern) {
        this.context.strokeStyle = this.pattern
      }
    }
    this.context.lineJoin = 'round'
    this.context.stroke(this.drawPath)
  }

}。export class DrawPathPointModel {
  x: number = 0
  y: number = 0
}

export enum DrawPathType {
  pen = 0, //画笔
  pattern, //马赛克
}

// 配置画笔
export class DrawPathModel {

  public pathType: DrawPathType = DrawPathType.pen

  public color: string = "#ED1B1B"
  public lineWidth: number = 8

  public img: ImageBitmap = new ImageBitmap("Images/canvas.png")

}

@Observed
export class DrawViewModel {
  public settings: RenderingContextSettings = new RenderingContextSettings(true)
  public context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  public drawModel: DrawPathModel = new DrawPathModel()
  public canvasHeight: number = 0
  public canvasWidth: number = 0

  private pattern: CanvasPattern | null
  private points: DrawPathPointModel[] = []
  // 绘制路径
  private drawPath = new Path2D()

  constructor() {
    this.pattern = this.context.createPattern(this.drawModel.img, 'repeat')
  }

  moveStart(x: number, y: number) {
    this.points.push({x: x, y: y})
    this.drawPath.moveTo(x, y)
    this.drawCurrentPathModel()
  }

  moveUpdate(x: number, y: number) {
    let lastPoint = this.points[this.points.length - 1]
    this.points.push({x: x, y: y})
    this.drawPath.quadraticCurveTo((x + lastPoint.x) / 2, (y + lastPoint.y) / 2, x, y)
    this.drawCurrentPathModel()
  }

  moveEnd() {
    this.points = []
    this.drawPath = new Path2D()
  }

  clearPath() {
    this.points = []
    this.drawPath = new Path2D()
    this.context.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
  }

  canvasAreaChange(area: Area) {
    this.canvasHeight = area.height as number
    this.canvasWidth = area.width as number
  }

  private drawCurrentPathModel() {
    this.context.globalCompositeOperation = 'source-over'
    this.context.lineWidth = this.drawModel.lineWidth
    if (this.drawModel.pathType == DrawPathType.pen) {
      this.context.strokeStyle = this.drawModel.color
    } else {
      if (this.pattern) {
        this.context.strokeStyle = this.pattern
      }
    }
    this.context.lineJoin = 'round'
    this.context.stroke(this.drawPath)
  }

}
  • 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.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
分享
微博
QQ
微信
回复
2025-01-09 18:38:20


相关问题
图片进行画笔或者马赛克绘制
1947浏览 • 1回复 待解决
HarmonyOS 分享功能如何实现
952浏览 • 1回复 待解决
HarmonyOS 如何实现ImagePreview功能
682浏览 • 1回复 待解决
HarmonyOS 如何实现轮询功能
964浏览 • 1回复 待解决
HarmonyOS 曝光功能如何实现
900浏览 • 1回复 待解决
HarmonyOS 如何实现DeepLink功能
895浏览 • 1回复 待解决
HarmonyOS 如何实现直播功能
1005浏览 • 1回复 待解决
HarmonyOS 如何实现popupwindow功能
730浏览 • 1回复 待解决
HarmonyOS 如何实现文件选择功能
887浏览 • 1回复 待解决
HarmonyOS 如何实现搜索历史功能
852浏览 • 1回复 待解决
HarmonyOS 如何实现图片编辑功能
934浏览 • 1回复 待解决
HarmonyOS如何实现头像选择功能
1676浏览 • 1回复 待解决
HarmonyOS 如何实现长按点击功能
936浏览 • 1回复 待解决
定时提醒功能如何实现?
5910浏览 • 1回复 待解决
Grid如何实现拖拽功能
3419浏览 • 1回复 待解决
鸿蒙如何实现分享功能
19154浏览 • 2回复 待解决
HarmonyOS 如何实现手势密码功能
1444浏览 • 1回复 待解决
HarmonyOS如何实现粘贴板功能
1144浏览 • 1回复 待解决
HarmonyOS 防截屏功能如何实现
880浏览 • 1回复 待解决
HarmonyOS 如何实现语音助手的功能
1162浏览 • 1回复 待解决
HarmonyOS 如何实现锚点定位功能
662浏览 • 1回复 待解决
HarmonyOS 如何实现应用全局换肤功能
730浏览 • 1回复 待解决