HarmonyOS 如何实现马赛克功能

HarmonyOS
2天前
浏览
收藏 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)
  }

}
分享
微博
QQ
微信
回复
2天前
相关问题
图片进行画笔或者马赛克绘制
1172浏览 • 1回复 待解决
HarmonyOS 如何实现轮询功能
208浏览 • 1回复 待解决
HarmonyOS 分享功能如何实现
337浏览 • 1回复 待解决
HarmonyOS 如何实现ImagePreview功能
29浏览 • 1回复 待解决
HarmonyOS 如何实现DeepLink功能
223浏览 • 1回复 待解决
HarmonyOS 曝光功能如何实现
208浏览 • 1回复 待解决
HarmonyOS 如何实现直播功能
202浏览 • 1回复 待解决
HarmonyOS 如何实现popupwindow功能
38浏览 • 1回复 待解决
HarmonyOS 如何实现图片编辑功能
264浏览 • 1回复 待解决
HarmonyOS 如何实现文件选择功能
188浏览 • 1回复 待解决
HarmonyOS如何实现头像选择功能
781浏览 • 1回复 待解决
HarmonyOS 如何实现搜索历史功能
214浏览 • 1回复 待解决
HarmonyOS 如何实现长按点击功能
241浏览 • 1回复 待解决
鸿蒙如何实现分享功能
17949浏览 • 2回复 待解决
Grid如何实现拖拽功能
2776浏览 • 1回复 待解决
定时提醒功能如何实现?
5231浏览 • 1回复 待解决
HarmonyOS 防截屏功能如何实现
166浏览 • 1回复 待解决
HarmonyOS如何实现粘贴板功能
523浏览 • 1回复 待解决
HarmonyOS 如何实现手势密码功能
731浏览 • 1回复 待解决
HarmonyOS 如何实现语音助手的功能
464浏览 • 1回复 待解决
HarmonyOS 如何实现应用全局换肤功能
19浏览 • 1回复 待解决
HarmonyOS 如何实现锚点定位功能
129浏览 • 1回复 待解决
HarmonyOS 如何实现页面的继承功能
202浏览 • 1回复 待解决