vue组件vue-esign实现电子签名


Posted in Vue.js onApril 21, 2022

vue vue-esign签字板demo

使用vue-esign让用户能够在手动签字并返回为base64或者file格式的文件流

安装

npm install vue-esign --save

在main.js中全局引用

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

Demo 

<template>
  <div class="esigns">
    <vue-esign
      ref="esign"
      style="
        width: 100%;
        height: 400px
      "
      :isCrop="isCrop"
      :lineWidth="lineWidth"
      :lineColor="lineColor"
      :bgColor.sync="bgColor"
    />
    <div class="btn">
      <van-button type="primary" @click="handleReset">重置</van-button>
      <van-button type="info" @click="handleGenerate">确定</van-button>
    </div>
  </div>
</template>
<script>
export default {
  name: "Esign",
  data() {
    return {
      lineWidth: 6,
      lineColor: "#000000",
      bgColor: "",
      resultImg: "",
      isCrop: false,
    };
  },
  methods: {
    handleReset() {
      // 清除
      this.$refs.esign.reset();
    },
    handleGenerate() {
      // 获取base64
      var _this = this;
      _this.$refs.esign
        .generate()
        .then((res) => {
          // 转成文件
          var file = _this.dataURLtoFile(res);
            console.log("file:",file )
          //调用接口
          uploadFile(file).then(({ data }) => {
           console.log("data:",data)
          });
        })
        .catch((err) => {
          _this.$toast(err); 
        });
    },
    // 将base64转换为file
    dataURLtoFile(dataurl) {
      let arr = dataurl.split(",");
      let mime = arr[0].match(/:(.*?);/)[1];
      let bytes = atob(arr[1]); // 解码base64
      let n = bytes.length;
      let ia = new Uint8Array(n);
      while (n--) {
        ia[n] = bytes.charCodeAt(n);
      }
      return new File([ia], "easign.jpg", { type: mime });
    },
  },
};
</script>
<style scoped>
.btn {
  display: flex;
  justify-content: space-around;
  margin-top: 10px;
}
</style>

vue移动端电子签名demo

HTML

<template>
    <div id='canvasBox'>
        <div ref="canvasBox">
             <canvas id="canvas" ref="canvas" height="150"></canvas>
        </div>
        <div class="row row-space-between">
          <button  @click="onClickCancle">取消</button>
          <button @click="clear">重签</button>
          <button @click="save">确认</button>
        </div>
        <!-- <img :src="singImgUrl" alt /> -->
    </div>
</template>

JS相关代码

<script>
var draw;
var preHandler = function(e) {
  e.preventDefault();
};
class Draw {
  constructor(el) {
    this.el = el;
    this.canvas = document.getElementById(this.el);
    this.cxt = this.canvas.getContext("2d");
    this.stage_info = canvas.getBoundingClientRect();
    this.path = {
      beginX: 0,
      beginY: 0,
      endX: 0,
      endY: 0
    };
  }
  init(btn) {
    var that = this;
    this.canvas.addEventListener("touchstart", function(event) {
      document.addEventListener("touchstart", preHandler, false);
      that.drawBegin(event);
    });
    this.canvas.addEventListener("touchend", function(event) {
      document.addEventListener("touchend", preHandler, false);
      that.drawEnd();
    });
    this.clear(btn);
  }
  drawBegin(e) {
    var that = this;
    window.getSelection()
      ? window.getSelection().removeAllRanges()
      : document.selection.empty();
    this.cxt.strokeStyle = "#BC4C2D";
    this.cxt.beginPath();
    this.cxt.moveTo(
      e.changedTouches[0].clientX - this.stage_info.left,
      e.changedTouches[0].clientY - this.stage_info.top
    );
    this.path.beginX = e.changedTouches[0].clientX - this.stage_info.left;
    this.path.beginY = e.changedTouches[0].clientY - this.stage_info.top;
    canvas.addEventListener("touchmove", function() {
      that.drawing(event);
    });
  }
  drawing(e) {
    this.cxt.lineTo(
      e.changedTouches[0].clientX - this.stage_info.left,
      e.changedTouches[0].clientY - this.stage_info.top
    );
    this.path.endX = e.changedTouches[0].clientX - this.stage_info.left;
    this.path.endY = e.changedTouches[0].clientY - this.stage_info.top;
    this.cxt.stroke();
  }
  drawEnd() {
    document.removeEventListener("touchstart", preHandler, false);
    document.removeEventListener("touchend", preHandler, false);
    document.removeEventListener("touchmove", preHandler, false);
    //canvas.ontouchmove = canvas.ontouchend = null
  }
  clear(btn) {
    this.base64Id = "";
    this.cxt.clearRect(0, 0, 500, 600);
  }
  save() {
    var blank = document.createElement("canvas"); //系统获取一个空canvas对象
    blank.width = canvas.width;
    blank.height = canvas.height;
    let flag = canvas.toDataURL("image/png") == blank.toDataURL(); //比较值相等则为空;
    if (flag) {
      return "0";
    } else {
      return canvas.toDataURL("image/png");
    }
  }
}
export default {
  data() {
    return {
      singImgUrl: ""
    };
  },
  methods: {
	 clear() {
        draw.clear();
        this.base64Id = "";
	 },
   	save() {
      var data = "";
      data = draw.save();
      if (data == "0") {
      		this.$toast("请先签名再点击确定哦~");
      } else {
	      this.singImgUrl = data;
	      ///data 就是得到的base64格式的签名图片,根据业务这里可上传到服务器
      }
      // 
    },
},
 mounted() {
 document.getElementById("canvasBox").addEventListener("touchmove", (e) => {
      e.preventDefault();
    });//阻止浏览器默认行为,防止签名浏览器下拉-------很重要
    this.base64Id = "";
    let _width = this.$refs.canvasBox.offsetWidth;
    this.$refs.canvas.width = _width; //适配移动端宽度给canvas
    draw = new Draw("canvas");
    draw.init();
  }
}
</script>

CSS 自行美化,相信大家都没得问题。


Tags in this post...

Vue.js 相关文章推荐
深入了解Vue3模板编译原理
Nov 19 Vue.js
在Vue中使用CSS3实现内容无缝滚动的示例代码
Nov 27 Vue.js
详解Vue中的自定义指令
Dec 07 Vue.js
Vue如何跨组件传递Slot的实现
Dec 14 Vue.js
Vue实现指令式动态追加小球动画组件的步骤
Dec 18 Vue.js
Vue实现一种简单的无限循环滚动动画的示例
Jan 10 Vue.js
vue 页面跳转的实现方式
Jan 12 Vue.js
基于VUE实现简单的学生信息管理系统
Jan 13 Vue.js
vue使用refs获取嵌套组件中的值过程
Mar 31 Vue.js
vue3使用vuedraggable实现拖拽功能
Apr 06 Vue.js
Vue router配置与使用分析讲解
Dec 24 Vue.js
vue动态绑定style样式
Apr 20 #Vue.js
Vue OpenLayer测距功能的实现
vue 给数组添加新对象并赋值
Apr 20 #Vue.js
vue 数字翻牌器动态加载数据
Apr 20 #Vue.js
vue3.0 数字翻牌组件的使用方法详解
Apr 20 #Vue.js
vue封装数字翻牌器
Apr 20 #Vue.js
vue特效之翻牌动画
Apr 20 #Vue.js
You might like
php实现的获取网站备案信息查询代码(360)
2013/09/23 PHP
php图片缩放实现方法
2014/02/20 PHP
PHP Trait代码复用类与多继承实现方法详解
2019/06/17 PHP
Yii 框架使用数据库(databases)的方法示例
2020/05/19 PHP
slice函数的用法 之不错的应用
2006/12/29 Javascript
jquery中animate动画积累的解决方法
2013/10/05 Javascript
NodeJS中Buffer模块详解
2015/01/07 NodeJs
jQuery中andSelf()方法用法实例
2015/01/08 Javascript
详解JavaScript中this关键字的用法
2016/05/26 Javascript
javascript cookie基础应用之记录用户名的方法
2016/09/20 Javascript
Jil,高效的json序列化和反序列化库
2017/02/15 Javascript
bootstrap栅格系统示例代码分享
2017/05/22 Javascript
vue+vuex+axios实现登录、注册页权限拦截
2018/03/09 Javascript
关于jquery中attr()和prop()方法的区别
2018/05/28 jQuery
js实现删除li标签一行内容
2019/04/16 Javascript
vue项目中锚点定位替代方式
2019/11/13 Javascript
详解如何使用React Hooks请求数据并渲染
2020/10/18 Javascript
比较详细Python正则表达式操作指南(re使用)
2008/09/06 Python
Python中处理时间的几种方法小结
2015/04/09 Python
Python判断以什么结尾以什么开头的实例
2018/10/27 Python
django 中QuerySet特性功能详解
2019/07/25 Python
python opencv图片编码为h264文件的实例
2019/12/12 Python
浅谈pytorch、cuda、python的版本对齐问题
2020/01/15 Python
python 通过邮件控制实现远程控制电脑操作
2020/03/16 Python
python Tornado框架的使用示例
2020/10/19 Python
python request 模块详细介绍
2020/11/10 Python
python通用数据库操作工具 pydbclib的使用简介
2020/12/21 Python
Mistine官方海外旗舰店:泰国国民彩妆品牌
2016/12/28 全球购物
“型”走纽约上东区:Sam Edelman
2017/04/02 全球购物
幼儿园母亲节活动方案
2014/03/10 职场文书
积极贯彻学习两会精神总结
2014/03/17 职场文书
委托书范文
2014/04/02 职场文书
说明书范文
2014/05/07 职场文书
年会邀请函范文
2015/01/30 职场文书
小学生组织委员竞选稿
2015/11/21 职场文书
python利用pandas分析学生期末成绩实例代码
2021/07/09 Python