vue如何利用canvas做一个电子签名功能?下面web建站小编给大家简单介绍一下现实方法!
pc端我们需要用到:mousedown、mousemove、mouseup,手机端我们需要用到:touchstart、touchmove、touchend。
先定义一个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
jquery如何利用mousedown、mousemove和mouseup等事件来实现手势识别
上面是“vue如何利用canvas做一个电子签名功能?”的全面内容,想了解更多关于 vuejs 内容,请继续关注web建站教程。
当前网址:https://ipkd.cn/webs_2893.html
workflows工作流
图生图生成动漫效果ComfyUI工作流
一张由表情符号组成的照片ComfyUI工作流
令人着迷的一只老虎ComfyUI工作流
一个漂亮的混血女孩ComfyUI工作流
3D几何打印人体模型ComfyUI工作流
一位老人安详地坐在云层中钓鱼
一个穿过泥泞雷区的士兵ComfyUI工作流
一个孤独的身影在未来主义城市
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

css3卡片动态滑动效果
利用js+css3做一个小鱼游泳特效
iframe开发admin后台
3d空间行走效果
css3动画loading效果
js+css3做一个灯泡开灯关灯效果
HTML5 Canvas 刻度尺
3D彩色卡片










