HarmonyOS 司机签字页

HarmonyOS
2024-12-25 11:15:11
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang
@Entry
@Component
struct Demo3 {
  context: CanvasRenderingContext2D =
    new CanvasRenderingContext2D(new RenderingContextSettings(true)) //签字区域
  guessContext: CanvasRenderingContext2D =
    new CanvasRenderingContext2D(new RenderingContextSettings(true)) //自动生成图片画布
  drawIng: boolean = false
  lastX: number = 0
  lastY: number = 0
  gLastX: number = 0
  gLastY: number = 0
  pointList: PointClass[] = []
  timer: number = -1
  @State imgStr: string = ""

  drawLine(x: number, y: number) {
    this.context.moveTo(this.lastX,
      this.lastY) // 先将线移动到上一个点
    this.context.lineTo(x, y)
    this.lastX = x // 将当前内容的x记录
    this.lastY = y // 将当前的y记录
    this.context.stroke()
  }

  drawGuess() {
    if (this.pointList.length && this.timer === -1) {
      this.timer = setInterval(() => {
        if (this.pointList.length === 0) {
          clearInterval(this.timer)
          this.timer = -1
          return
        }
        let pt1: PointClass = this.pointList.shift() as PointClass
        this.guessLine(pt1)
      }, 100)
    }
  }

  guessLine(p: PointClass) {
    if (p.reset) {
      this.guessContext.closePath()
      this.guessContext.beginPath()
      this.gLastX = p.x
      this.gLastY = p.y
    } else {
      this.guessContext.moveTo(this.gLastX,
        this.gLastY) // 先将线移动到上一个点
      this.guessContext.lineTo(p.x, p.y)
      this.gLastX = p.x // 将当前内容的x记录
      this.gLastY = p.y // 将当前的y记录
      this.guessContext.stroke()
    }
  }

  transFile() {
  }

  build() {
    Scroll() {
      Column({ space: 20 }) {
        Canvas(this.context)
          .width(360)
          .height(300)
          .backgroundColor(Color.Grey)
          .onTouch((event: TouchEvent) => {
            if (event.type === TouchType.Down) {
              this.lastX = event.touches[0].x
              this.lastY = event.touches[0].y
              this.drawIng = true
              this.context.beginPath()
              this.pointList.push({ x: this.lastX, y: this.lastY, reset: true })
            }
            if (event.type === TouchType.Move) {
              if (this.drawIng) {
                this.pointList.push({ x: event.touches[0].x, y: event.touches[0].y, reset: false })
                this.drawLine(event.touches[0].x, event.touches[0].y)
              }
            }
            if (event.type === TouchType.Up) {
              this.drawIng = false
              this.context.closePath()
            } // 开始模仿
            this.drawGuess()
          })
          .onReady(() => {
            this.context.lineWidth = 4
            this.context.strokeStyle = "blue"
          })
        Canvas(this.guessContext).width(360).height(300).backgroundColor(Color.Red).onReady(() => {
          this.guessContext.lineWidth = 4
          this.guessContext.strokeStyle = "#fff"
        })
        Row({ space: 20 }) {
          Button("清屏").onClick(() => {
            this.context.clearRect(0, 0, 360, 300)
            this.guessContext.clearRect(0, 0, 360, 300)
            this.pointList = []
          })
          Button("存储图片").onClick(() => {
            this.imgStr = this.context.toDataURL("image/jpeg")
            console.info('图片开始存储' + this.imgStr)
          })
        }

        if (this.imgStr) {
          Image(this.imgStr).width(360).height(300)
        }
      }
    }
  }
}

class PointClass {
  x: number = 0
  y: number = 0
  reset?: boolean = false
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-25 14:28:51
相关问题
想跟着做APP,让老司机带带我
10023浏览 • 6回复 待解决
跳转设置返回存在白屏
2605浏览 • 1回复 待解决
HarmonyOS 引导demo
892浏览 • 1回复 待解决
HarmonyOS Navigation透明demo
716浏览 • 1回复 待解决
HarmonyOS 启动设置问题
1474浏览 • 1回复 待解决
如何跳转到APP设置和权限管理
1744浏览 • 1回复 待解决
ArkTs在Page内,如何关闭当前Page
3941浏览 • 1回复 待解决
HarmonyOS 如何跳转权限设置
700浏览 • 1回复 待解决
HarmonyOS如何设置应用启动
1750浏览 • 1回复 待解决
HarmonyOS 启动广告跳转问题
862浏览 • 1回复 待解决
HarmonyOS 启动背景图适配
961浏览 • 1回复 待解决
HarmonyOS 启动的闪屏怎么配置
759浏览 • 1回复 待解决
HarmonyOS 是否有个人信息示例
775浏览 • 1回复 待解决
HarmonyOS navigation跳转页面为空白
850浏览 • 1回复 待解决
HarmonyOS 如何跳转设置的通知管理
647浏览 • 1回复 待解决
HarmonyOS 跳转华为应用商店详情
1226浏览 • 1回复 待解决