web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. seo教程
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站

vue如何利用canvas做一个电子签名功能?

798 ℃
           

vue如何利用canvas做一个电子签名功能?下面web建站小编给大家简单介绍一下现实方法!

pc端我们需要用到:mousedownmousemovemouseup,手机端我们需要用到:touchstarttouchmovetouchend

先定义一个canvas

<canvas
  @touchstart="touchStart"
  @touchmove="touchMove"
  @touchend="touchEnd"
  ref="canvasA"
  @mousedown="mouseDown"
  @mousemove="mouseMove"
  @mouseup="mouseUp"
></canvas>

data参数

data() {
  return {
    points: [],
    canvasTxt: null,
    startX: 0,
    startY: 0,
    moveY: 0,
    moveX: 0,
    endY: 0,
    endX: 0,
    w: null,
    h: null,
    isDown: false,
    color: "#000",
    linewidth: 3,
    isDraw: false //签名标记
  };
},

在mounted中获取画布

mounted() {
  let canvas = this.$refs.canvasA;
  canvas.height = this.$refs.canvasHW.offsetHeight - 100;
  canvas.width = this.$refs.canvasHW.offsetWidth - 10;
  this.canvasTxt = canvas.getContext("2d");
  this.canvasTxt.strokeStyle = this.color;
  this.canvasTxt.lineWidth = this.linewidth;
  }

pc端事件

mouseDown

mouseDown(ev) {
  ev = ev || event;
  ev.preventDefault();

  let obj = {
  x: ev.offsetX,
  y: ev.offsetY
  };
  this.startX = obj.x;
  this.startY = obj.y;
  this.canvasTxt.beginPath();//开始作画
  this.points.push(obj);//记录点
  this.isDown = true;
},

mouseMove

mouseMove(ev) {
  ev = ev || event;
  ev.preventDefault();
  if (this.isDown) {
    let obj = {
	x: ev.offsetX,
	y: ev.offsetY
    };
    this.moveY = obj.y;
    this.moveX = obj.x;
    this.canvasTxt.moveTo(this.startX, this.startY); //移动画笔
    this.canvasTxt.lineTo(obj.x, obj.y); //创建线条
    this.canvasTxt.stroke(); //画线
    this.startY = obj.y;
    this.startX = obj.x;
    this.points.push(obj); //记录点
  }
},

mouseUp

mouseUp(ev) {
  ev = ev || event;
  ev.preventDefault();
  if (1) {
  let obj = {
    x: ev.offsetX,
    y: ev.offsetY
  };
  this.canvasTxt.closePath(); //收笔
  this.points.push(obj); //记录点
  this.points.push({
    x: -1,
    y: -1
  });
  this.isDown = false;
  }
},

手机端事件

touchStart

touchStart(ev) {
ev = ev || event;
ev.preventDefault();
  if (ev.touches.length == 1) {
  this.isDraw = true; //签名标记
  let obj = {
    x: ev.targetTouches[0].clientX,
    y: ev.targetTouches[0].clientY - (document.body.offsetHeight * 0.5 + this.$refs.canvasHW.offsetHeight * 0.1)
  }; 
  //y的计算值中:document.body.offsetHeight*0.5代表的是除了整个画板signatureBox剩余的高,
  //this.$refs.canvasHW.offsetHeight*0.1是画板中标题的高
  this.startX = obj.x;
  this.startY = obj.y;
  this.canvasTxt.beginPath();//开始作画
  this.points.push(obj);//记录点
  }
},

touchMove

touchMove(ev) {
  ev = ev || event;
  ev.preventDefault();
  if (ev.touches.length == 1) {
  let obj = {
    x: ev.targetTouches[0].clientX,
    y:
    ev.targetTouches[0].clientY -
    (document.body.offsetHeight * 0.5 +
  this.$refs.canvasHW.offsetHeight * 0.1)
  };
  this.moveY = obj.y;
  this.moveX = obj.x;
  this.canvasTxt.moveTo(this.startX, this.startY);//移动画笔
  this.canvasTxt.lineTo(obj.x, obj.y);//创建线条
  this.canvasTxt.stroke();//画线
  this.startY = obj.y;
  this.startX = obj.x;
  this.points.push(obj);//记录点
  }
},

touchEnd

touchEnd(ev) {
  ev = ev || event;
  ev.preventDefault();
  if (ev.touches.length == 1) {
  let obj = {
    x: ev.targetTouches[0].clientX,
    y:
    ev.targetTouches[0].clientY -
    (document.body.offsetHeight * 0.5 +
  this.$refs.canvasHW.offsetHeight * 0.1)
  };
  this.canvasTxt.closePath();//收笔
  this.points.push(obj);//记录点
  this.points.push({ x: -1, y: -1 });//记录点
  }
},

重写事件

overwrite() {
  this.canvasTxt.clearRect(
  0,
  0,
  this.$refs.canvasF.width,
  this.$refs.canvasF.height
  );
  this.points = [];
  this.isDraw = false; //签名标记
},

一款用于在网页上生成五彩纸屑特效的JavaScript库——Canvas Confetti

利用js做一个炫酷音乐背景效果

jquery如何利用mousedown、mousemove和mouseup等事件来实现手势识别

js+canvas实现粒子特效(跟随鼠标动)

html5+canvas如何做一个彩色六角菱形背景

标签: canvas, mousedown, mousemove, mouseup, touchend, touchmove, touchstart, 电子签名

上面是“vue如何利用canvas做一个电子签名功能?”的全面内容,想了解更多关于 vuejs 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_2893.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > vuejs
本文共计2826个字,预计阅读时长19分钟
生活小工具,收录了80多款小工具
上一篇: 推荐一款简单高效漂亮的多平台笔记应用——Zoho Notebook
下一篇: 推荐2款充满科技感的艺术字体——小新黑体、小新潮酷体
x 打工人ai神器