canvas 签名组件,vue签名组件,手写板

本文介绍了使用Vue.js开发的一个手写签名功能,通过监听touch事件实时绘制路径,提供获取签名图片数据和重置画布的方法。
摘要由CSDN通过智能技术生成

演示

方法:

方法名参数返回值描述
getSignImage-string获取签名图片数据,直接设置给src
resetCanvas--清空画布,重新手写

核心实现

通过监听触摸事件,获取手指滑动的坐标,然后通过canvas的moveTo,lineTo方法绘制路径。

const draw = (x: number, y: number) => {
  context.value?.beginPath()
  context.value?.moveTo(lastPaint.x, lastPaint.y)
  context.value?.lineTo(x, y)
  context.value?.stroke()
}

源码

<template>
  <div ref="canvas_container" class="canvas_container">
    <canvas
      id="canvas"
      ref="canvas"
      width="100%"
      height="100%"
      @touchstart="touchStart"
      @touchmove="touchMove"
      @touchend="touchEnd"
    />
  </div>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
const canvas = ref<HTMLCanvasElement>()
const context = ref<CanvasRenderingContext2D>()
let top = 0
let left = 0
let isDrawing = false
const lastPaint = { x: 0, y: 0 }
const startPaint = { x: 0, y: 0 }
const endPaint = { x: 0, y: 0 }

const initCanvas = () => {
  const boundRect = document
    .getElementsByClassName('canvas_container')[0]
    .getBoundingClientRect()
  console.log(boundRect)
  top = boundRect.top
  left = boundRect.left
  canvas.value.width = boundRect.width
  canvas.value.height = boundRect.height
  startPaint.x = boundRect.width
  startPaint.y = boundRect.height
  context.value = canvas.value?.getContext('2d')
  context.value!.lineWidth = 2
  context.value!.lineCap = 'round'
  context.value!.lineJoin = 'round'
  context.value!.strokeStyle = '#000000'
}

onMounted(() => {
  initCanvas()
})
const draw = (x: number, y: number) => {
  context.value?.beginPath()
  context.value?.moveTo(lastPaint.x, lastPaint.y)
  context.value?.lineTo(x, y)
  context.value?.stroke()
}

const touchStart = (e: TouchEvent) => {
  console.log('mousemove', e)
  if (e.targetTouches.length > 1) return
  const x = e.targetTouches[0].clientX - left
  const y = e.targetTouches[0].clientY - top
  lastPaint.x = x
  lastPaint.y = y
  setPaint(x, y)
  isDrawing = true
}

const touchMove = (e: TouchEvent) => {
  if (e.targetTouches.length > 1) return
  if (!isDrawing) return
  const x = e.targetTouches[0].clientX - left
  const y = e.targetTouches[0].clientY - top
  draw(x, y)
  lastPaint.x = x
  lastPaint.y = y
  setPaint(x, y)
}
const touchEnd = (e: TouchEvent) => {
  isDrawing = false
}

const setPaint = (x: number, y: number) => {
  if (x < startPaint.x) {
    startPaint.x = x
  }
  if (y < startPaint.y) {
    startPaint.y = y
  }
  if (x > endPaint.x) {
    endPaint.x = x
  }
  if (y > endPaint.y) {
    endPaint.y = y
  }
}
/**
 * 获取到签名图片数据
 */
const getSignImage = () => {
  console.log('getSignImage', startPaint, endPaint)
  const canvas2 = document.createElement('canvas')
  canvas2.width = endPaint.x - startPaint.x
  canvas2.height = endPaint.y - startPaint.y
  canvas2
    .getContext('2d')!
    .drawImage(
      canvas.value,
      startPaint.x,
      startPaint.y,
      endPaint.x - startPaint.x,
      endPaint.y - startPaint.y,
      0,
      0,
      endPaint.x - startPaint.x,
      endPaint.y - startPaint.y
    )
  return canvas2.toDataURL('image/png')
}

/**
 * 清空画布
 */
const resetCanvas = () => {
  context.value?.clearRect(0, 0, canvas.value.width, canvas.value.height)
  startPaint.x = canvas.value.width
  startPaint.y = canvas.value.height
  endPaint.x = 0
  endPaint.y = 0
}

defineExpose({ getSignImage, resetCanvas })
</script>
<style lang="scss" scoped>
.canvas_container {
  position: relative;
  width: 100%;
  height: 100%;
}
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

truemi.73

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值