- 先进行签名项配置
createCanvas() {
var canvas = document.getElementById('mycanvas')
canvas.width = this.config.width
canvas.height = this.config.height
canvas.style.border = '1px solid #ccc'
this.ctx = canvas.getContext('2d')
this.ctx.fillStyle = '#fff'
this.ctx.fillRect(
0,
0,
this.config.width,
this.config.height
);
},
- 默认设置
ctx: undefined,
config: {
width: 300,
height: 200,
lineWidth: 5,
strokeStyle: '#000',
lineCap: 'round',
lineJoin: 'round'
},
client: {
offsetX: 0,
offsetY: 0,
endX: 0,
endY: 0
},
- 页面dom
<canvas id="mycanvas" @mousedown="mousedown"></canvas>
- 执行画图
mousedown(e) {
const { offsetX, offsetY, pageX, pageY } = e
this.client.offsetX = offsetX
this.client.offsetY = offsetY
this.client.endX = pageX
this.client.endY = pageY
this.ctx.beginPath()
this.ctx.lineWidth = this.config.lineWidth
this.ctx.strokeStyle = this.config.strokeStyle
this.ctx.lineCap = this.config.lineCap
this.ctx.lineJoin = this.config.lineJoin
this.ctx.moveTo(this.client.offsetX, this.client.offsetY)
this.ctx.stroke()
window.addEventListener("mousemove", this.mousemove)
},
mousemove(e) {
const { offsetX, offsetY } = e
let h = document.getElementById('mycanvas').getBoundingClientRect().top
let w = document.getElementById('mycanvas').getBoundingClientRect().left
this.ctx.lineTo(e.clientX - w, e.clientY - h)
console.log(offsetX, offsetY);
this.ctx.stroke()
window.addEventListener("mouseup", this.mouseup)
},
mouseup() {
this.ctx.closePath()
window.removeEventListener("mousemove", this.mousemove)
},
clearcanvas() {
this.ctx.clearRect(0, 0, this.config.width, this.config.height)
this.sendbackForm.signUrl = undefined
},
- 补充说明
如果是手机签名
<canvas
class="mycanvas"
canvas-id="mycanvas"
@touchstart="touchstart"
@touchmove="touchmove"
@touchend="touchend"
></canvas>
createCanvas() {
this.ctx = uni.createCanvasContext("mycanvas", this);
this.ctx.lineWidth = 4;
this.ctx.strokeStyle = "#3366FF";
this.ctx.lineCap = "round";
this.ctx.lineJoin = "round";
},
touchstart(e) {
let startX = e.changedTouches[0].x;
let startY = e.changedTouches[0].y;
let startPoint = {
X: startX,
Y: startY,
};
this.points.push(startPoint);
this.ctx.beginPath();
},
touchmove(e) {
let moveX = e.changedTouches[0].x;
let moveY = e.changedTouches[0].y;
let movePoint = {
X: moveX,
Y: moveY,
};
this.points.push(movePoint);
let len = this.points.length;
if (len >= 2) {
this.draw();
}
},
touchend() {
this.points = [];
},
draw() {
let point1 = this.points[0];
let point2 = this.points[1];
this.points.shift();
this.ctx.moveTo(point1.X, point1.Y);
this.ctx.lineTo(point2.X, point2.Y);
this.ctx.stroke();
this.ctx.draw(true);
this.hasSign = true;
},
clear() {
this.hasSign = false;
let _this = this;
uni.getSystemInfo({
success: function (res) {
let canvasw = res.windowWidth;
let canvash = res.windowHeight;
_this.ctx.clearRect(0, 0, canvasw, canvash);
_this.ctx.draw(true);
},
});
},
finish() {
if (!this.hasSign) {
uni.showToast({
title: "签名为空不能保存",
icon: "none",
duration: 2000,
});
return;
}
let _this = this;
uni.canvasToTempFilePath({
canvasId: "mycanvas",
fileType: "png",
success: function (res) {
const header = uni.getStorageSync("token");
let a = uni.uploadFile({
url: baseCfg.baseURL + "/management/comm/file/upload",
filePath: res.tempFilePath,
name: "attachments",
header: {
Authorization: `Bearer ${header}`,
},
success: (res) => {
const json = JSON.parse(res.data);
json.data.forEach((item) => {
formValue.value.commonInfo.diagramAuthorSign = item.id;
});
_this.showSign = false;
},
});
},
});
},