<template>
<div class="signatureBox">
<div class="signature-title">签名</div>
<div ref="canvasHW" class="canvasBox">
<canvas
ref="canvasF"
@touchstart="touchStart"
@touchmove="touchMove"
@touchend="touchEnd"
@mousedown="mouseDown"
@mousemove="mouseMove"
@mouseup="mouseUp"
></canvas>
</div>
<div class="signature-btn">
<van-button class="btn" type="info" @click.native.prevent="handleGoBack()">返回</van-button>
<van-button class="btn" type="info" @click.native.prevent="handleOverwrite()">重签</van-button>
<van-button class="btn" type="info" @click.native.prevent="handleSubmit()">确认</van-button>
</div>
</div>
</template>
<script>
export default {
name: "Signature",
data() {
return {
points: [],
canvasTxt: null,
stage_info: [],
startX: 0,
startY: 0,
moveY: 0,
moveX: 0,
isDown: false,
strokeStyle: "#000",
lineWidth: 2
};
},
mounted() {
this.initCanvas();
},
methods: {
initCanvas() {
let canvas = this.$refs.canvasF;
// 获取画布的高度
canvas.height = this.$refs.canvasHW.offsetHeight - 20;
// 获取画布的宽度
canvas.width = this.$refs.canvasHW.offsetWidth - 20;
// 创建 context 对象
this.canvasTxt = canvas.getContext("2d");
this.stage_info = canvas.getBoundingClientRect();
},
// 电脑设备事件
mouseDown(ev) {
ev = ev || event;
ev.preventDefault();
if (ev) {
let obj = {
x: ev.offsetX,
y: ev.offsetY
};
this.startX = obj.x;
this.startY = obj.y;
this.canvasTxt.beginPath();
this.canvasTxt.moveTo(this.startX, this.startY);
this.canvasTxt.lineTo(obj.x, obj.y);
this.canvasTxt.stroke();
this.canvasTxt.closePath();
this.points.push(obj);
this.isDown = true;
}
},
// 移动设备事件
touchStart(ev) {
ev = ev || event;
ev.preventDefault();
if (ev.touches.length == 1) {
let obj = {
x: ev.targetTouches[0].clientX - this.stage_info.left,
y: ev.targetTouches[0].clientY - this.stage_info.top
};
this.startX = obj.x;
this.startY = obj.y;
this.canvasTxt.beginPath();
this.canvasTxt.moveTo(this.startX, this.startY);
this.canvasTxt.lineTo(obj.x, obj.y);
this.canvasTxt.stroke();
this.canvasTxt.closePath();
this.points.push(obj);
}
},
// 电脑设备事件
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.strokeStyle = this.strokeStyle;
this.canvasTxt.lineWidth = this.lineWidth;
this.canvasTxt.beginPath();
this.canvasTxt.moveTo(this.startX, this.startY);
this.canvasTxt.lineTo(obj.x, obj.y);
this.canvasTxt.stroke();
this.canvasTxt.closePath();
this.startY = obj.y;
this.startX = obj.x;
this.points.push(obj);
}
},
// 移动设备事件
touchMove(ev) {
ev = ev || event;
ev.preventDefault();
if (ev.touches.length == 1) {
let obj = {
x: ev.targetTouches[0].clientX - this.stage_info.left,
y: ev.targetTouches[0].clientY - this.stage_info.top
};
this.moveY = obj.y;
this.moveX = obj.x;
// 设置线条颜色
this.canvasTxt.strokeStyle = this.strokeStyle;
// 设置线条宽度
this.canvasTxt.lineWidth = this.lineWidth;
// 绘制开始路径
this.canvasTxt.beginPath();
// 定义线条开始坐标
this.canvasTxt.moveTo(this.startX, this.startY);
// 定义线条结束坐标
this.canvasTxt.lineTo(obj.x, obj.y);
// 绘制线条
this.canvasTxt.stroke();
this.canvasTxt.closePath();
this.startY = obj.y;
this.startX = obj.x;
this.points.push(obj);
}
},
// 电脑设备事件
mouseUp(ev) {
ev = ev || event;
ev.preventDefault();
if (ev) {
let obj = {
x: ev.offsetX,
y: ev.offsetY
};
this.canvasTxt.beginPath();
this.canvasTxt.moveTo(this.startX, this.startY);
this.canvasTxt.lineTo(obj.x, obj.y);
this.canvasTxt.stroke();
this.canvasTxt.closePath();
this.points.push(obj);
this.points.push({ x: -1, y: -1 });
this.isDown = false;
}
},
// 移动设备事件
touchEnd(ev) {
ev = ev || event;
ev.preventDefault();
if (ev.touches.length == 1) {
let obj = {
x: ev.targetTouches[0].clientX - this.stage_info.left,
y: ev.targetTouches[0].clientY - this.stage_info.top
};
this.canvasTxt.beginPath();
this.canvasTxt.moveTo(this.startX, this.startY);
this.canvasTxt.lineTo(obj.x, obj.y);
this.canvasTxt.stroke();
this.canvasTxt.closePath();
this.points.push(obj);
this.points.push({ x: -1, y: -1 });
}
},
// 返回
handleGoBack() {
this.handleOverwrite();
this.$emit("back");
},
// 重写
handleOverwrite() {
this.canvasTxt.clearRect(
0,
0,
this.$refs.canvasF.width,
this.$refs.canvasF.height
);
this.points = [];
},
// 提交
handleSubmit() {
if (this.points.length < 50) {
return;
}
this.$emit("content", this.$refs.canvasF.toDataURL());
}
}
};
</script>
<style lang="less" scoped>
.signatureBox {
position: fixed;
bottom: 0;
left: 0;
right: 0;
background-color: #fff;
.signature-title {
text-align: center;
font-size: 14px;
padding: 10px 0;
background-color: #f26226;
color: #fff;
}
.canvasBox {
padding: 10px;
height: 220px;
canvas {
border: 1px solid #f26226;
}
}
.signature-btn {
padding: 0 10px 10px;
display: flex;
justify-content: center;
box-sizing: border-box;
.btn {
margin-top: 10px;
margin-bottom: 10px;
background-color: #f26226;
border-color: #f26226;
display: inline-block;
width: 40%;
margin-right: 10px;
height: 30px;
line-height: 30px;
padding: 0;
&:last-child {
margin-right: 0;
}
}
}
}
</style>
版权声明:本文为idjiang原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。