vue3实现手写签名并且保存到本地

<template>
    <canvas ref="canvas" id="canvas" width="600" height="500" class="pink"></canvas>
    <el-button type="primary" @click="savePath">保存</el-button>
</template>
<script setup>
  import {ref,onMounted} from 'vue'
  const canvas = ref(null)
  let ctx = null
  let isDown = false
  onMounted(()=>{
    init()
  })

  const init = () => {
    ctx = canvas.value.getContext('2d')
    ctx.lineWidth = 2; //直线的宽度状态设置
    ctx.fillStyle = "#ccc"; //直线的颜色状态设置
    ctx.strokeStyle = "#000"; //直线的颜色状态设置
    ctx.lineCap = 'round';
    ctx.fillRect(0, 0, 600, 500);
    draw()
  }
  
  const draw = () => {
    canvas.value.addEventListener('mousedown', (e) => {
      isDown = true
      ctx.beginPath()
      ctx.moveTo(e.offsetX, e.offsetY);
    })
    canvas.value.addEventListener('mousemove', (e) => {
      if (isDown) {
          ctx.lineTo(e.offsetX, e.offsetY);
          ctx.stroke()
      }
    })
    document.documentElement.addEventListener('mouseup', (e) => {
      isDown = false
    })
  }

  const savePath = () => {
    const url = canvas.value.toDataURL("image/png")
    const a = document.createElement('a')
    document.body.appendChild(a)
    a.href = url
    a.download = 'canvas'
    a.click()
  }
</script>
<style lang="scss" scoped>
.pink{
  background-color: pink;
  margin-top:10px
}
</style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Vue移动端实现横屏手写签名,可以使用HTML5的Canvas元素结合touch事件来实现。具体步骤如下: 1. 在Vue组件中引入Canvas元素,并为其设置样式。 2. 监听touch事件,获取手写路径的坐标,并使用Canvas绘制。 3. 提供保存和重签功能,可以使用Canvas的toDataURL()方法将签名转为图片格式保存,并清空Canvas重新开始签名。 下面是一个简单的示例代码,供你参考: ``` <template> <div class="signature"> <canvas ref="canvas" class="canvas"></canvas> <div class="btns"> <button @click="save">保存</button> <button @click="clear">重签</button> </div> </div> </template> <script> export default { data() { return { canvas: null, ctx: null, drawing: false, lastX: null, lastY: null, lineWidth: 2, }; }, mounted() { this.canvas = this.$refs.canvas; this.ctx = this.canvas.getContext('2d'); this.canvas.width = window.innerWidth; this.canvas.height = window.innerHeight; this.ctx.strokeStyle = '#000000'; this.ctx.lineWidth = this.lineWidth; this.ctx.lineJoin = 'round'; this.ctx.lineCap = 'round'; this.canvas.addEventListener('touchstart', this.startDrawing); this.canvas.addEventListener('touchmove', this.draw); this.canvas.addEventListener('touchend', this.stopDrawing); }, methods: { startDrawing(e) { this.drawing = true; this.lastX = e.touches[0].clientX; this.lastY = e.touches[0].clientY; }, draw(e) { if (!this.drawing) return; const x = e.touches[0].clientX; const y = e.touches[0].clientY; this.ctx.beginPath(); this.ctx.moveTo(this.lastX, this.lastY); this.ctx.lineTo(x, y); this.ctx.stroke(); this.lastX = x; this.lastY = y; }, stopDrawing() { this.drawing = false; }, save() { const dataURL = this.canvas.toDataURL(); // 将dataURL保存到本地或上传到服务器 }, clear() { this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); }, }, }; </script> <style scoped> .signature { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: #ffffff; } .canvas { width: 100%; height: 100%; } .btns { position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); } button { margin-right: 10px; background-color: #000000; color: #ffffff; border: none; padding: 10px 20px; border-radius: 5px; font-size: 16px; cursor: pointer; } </style> ``` 注意,由于移动端横屏显示,需要在head标签中添加以下meta标签: ``` <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, orientation=landscape"> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值