vue使用canvas实现手写电子签名;vue使用vue-esign插件实现手写电子签名;H5使用画布签名

功能:
1.兼容 PC 和 Mobile;
2.画布自适应屏幕大小变化(窗口缩放、屏幕旋转时画布无需重置,自动校正坐标偏移);
3.自定义画布尺寸(导出图尺寸),画笔粗细、颜色,画布背景色;
4.支持裁剪 (针对需求:有的签字需要裁剪掉四周空白)。
5.导出图片格式为 base64
6.原博地址,本文在原博基础修改,代码在博尾。

签名效果:
在这里插入图片描述

一、安装插件

npm install vue-esign --save

二、在.vue页面引入使用

import Vue from "vue"
import vueEsign from 'vue-esign'
Vue.use(vueEsign)

三、注意事项

1.必须设置 ref ,用来调用组件的两个内置方法,清空画布 reset() 和 导出图片generate()
2.画布默认是宽高800*300,且宽度是不会超过父元素的宽度的

属性类型默认值说明
widthNumber800画布宽度,即导出图片的宽度
heightNumber300画布高度,即导出图片的高度
lineWidthNumber4画笔粗细
lineColorString#000000画笔颜色
bgColorString画布背景色,为空时画布背景透明,支持多种格式 ‘#ccc’,’#E5A1A1’,‘rgb(229, 161, 161)’,‘rgba(0,0,0,.6)’,‘red’
isCropBooleanfalse是否裁剪,在画布设定尺寸基础上裁掉四周空白部分
isClearBgColorBooleantrue清空画布reset时,是否清空背景色bgColor

四、以下代码可直接复制(有效的话点赞支持一波吧)

<template>
  <div style="margintop:30px;">

    <div>
      <div style="margin-bottom:20px;">
        <span>画笔粗细:</span>
        <el-select style="width:100px;" v-model="lineWidth" placeholder="请选择">
          <el-option :label="1" :value="1"></el-option>
          <el-option :label="3" :value="3"></el-option>
          <el-option :label="6" :value="6"></el-option>
        </el-select>

        <span>画笔颜色:</span>
        <!-- input颜色回显必须要六位的颜色值 -->
        <input v-model="lineColor" type="color" placeholder="" placeholder-class="input-placeholder" />

        <span>画布背景:</span>
        <input v-model="bgColor" type="color" placeholder="" placeholder-class="input-placeholder" />

        <span>是否裁剪:</span>
        <input v-model="isCrop" type="checkbox" name="">

      </div>

      <vue-esign style="border: 1px solid #ddd;" ref="esign" :width="canWidth" :height="canHeight" :isCrop="isCrop" :lineWidth="lineWidth" :lineColor="lineColor" :bgColor.sync="bgColor" :isClearBgColor="isClearBgColor" />
      <button @click="handleReset">清空画板</button>
      <button @click="handleGenerate">生成图片</button>
    </div>

    <div>
      <img style="float:left;" :src="resultImg" alt="">
    </div>

  </div>
</template>

<script>
import Vue from "vue"
import vueEsign from 'vue-esign'
Vue.use(vueEsign)

export default {
  data () {
    return {
      canWidth: 800,//画布宽度--是不会超出父元素的宽度的--设置也不行
      canHeight: 300,

      lineWidth: 3,//画笔粗细
      lineColor: '#000000',//画笔颜色
      bgColor: '#ffffff',//画布背景
      isCrop: false,//是否裁剪
      isClearBgColor: true,//是否清空背景色

      resultImg: '',//生成签名图片-base64
    }
  },
  methods: {
    handleReset () {
      // console.log(this.$refs.esign.$el)
      // console.log(this.$refs.esign)

      this.$refs.esign.reset()//清空画布内容

      this.lineWidth = 3
      this.lineColor = '#000000'
      this.bgColor = '#ffffff'
      this.isCrop = false
      this.resultImg = ''
    },
    handleGenerate () {
      this.$refs.esign.generate().then(res => {
        console.log('图片的base64地址', res)
        console.log(this.$refs.esign)
        this.resultImg = res
      }).catch(err => {
        console.log('画布没有签字时', err)
        alert('请先完成签字!') // 画布没有签字时会执行这里 'Not Signned'
      })
    }
  }

}
</script>

<style>
</style>
  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,可以先在template中添加一个canvas标签,然后钩子函数中获取该标签并设置画布大小和绘制笔刷等属性。接着可以监听鼠标或触摸事件,在相应的事件处理函数中获取鼠标或触摸点坐标,并在画布上绘制路径。最后可以添加清空按钮,通过清空画布达到重新签名的效果。具体代码实现可以参考以下示例: ```html <template> <div> <canvas ref="canvas" @mousedown="startDrawing" @touchstart="startDrawing" @mousemove="drawing" @touchmove="drawing" @mouseup="stopDrawing" @touchend="stopDrawing" :width="width" :height="height" style="border: 1px solid #ccc;"></canvas> <button @click="clear">清空</button> </div> </template> <script> export default { data() { return { ctx: null, isDrawing: false, lineWidth: 2, strokeStyle: '#333', width: 300, height: 150 } }, mounted() { const canvas = this.$refs.canvas this.ctx = canvas.getContext('2d') this.ctx.lineJoin = 'round' this.ctx.lineCap = 'round' this.ctx.lineWidth = this.lineWidth this.ctx.strokeStyle = this.strokeStyle }, methods: { startDrawing(event) { event.preventDefault() this.isDrawing = true this.ctx.beginPath() const pos = this.getMousePosition(event) this.ctx.moveTo(pos.x, pos.y) }, drawing(event) { event.preventDefault() if (this.isDrawing) { const pos = this.getMousePosition(event) this.ctx.lineTo(pos.x, pos.y) this.ctx.stroke() } }, stopDrawing(event) { event.preventDefault() this.isDrawing = false }, clear() { this.ctx.clearRect(0, 0, this.width, this.height) }, getMousePosition(event) { const canvas = event.target const rect = canvas.getBoundingClientRect() const scaleX = canvas.width / rect.width const scaleY = canvas.height / rect.height return { x: (event.clientX - rect.left) * scaleX, y: (event.clientY - rect.top) * scaleY } } } } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值