<template>
<view class="container">
<view class="sigh-btns">
<button class="btn" @tap="handleReset">重写</button>
<button class="btn" @tap="handleConfirm">确认</button>
</view>
<view class="sign-box">
<canvas class="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove"
@touchend="touchend"></canvas>
</view>
<image :src="tesImg" mode=""></image>
</view>
</template>
<script>
export default {
onLoad() {
this.ctx = uni.createCanvasContext("mycanvas", this);
this.ctx.lineWidth = 4;
this.ctx.lineCap = "round"
this.ctx.lineJoin = "round"
},
data() {
return {
ctx: '',
points: [],
tempPoint: [],
tesImg: ""
};
},
methods: {
touchstart(e) {
console.log(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();
}
this.tempPoint.push(movePoint);
},
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)
},
handleReset() {
uni.getSystemInfo({
success: (res) => {
let canvasw = res.windowWidth;
let canvash = res.windowHeight;
this.ctx.clearRect(0, 0, canvasw, canvash);
this.ctx.draw(true);
},
})
this.tempPoint = [];
},
handleConfirm() {
if (this.tempPoint.length == 0) {
uni.showToast({
title: '请先签名',
icon: 'none',
duration: 2000
});
return;
}
uni.canvasToTempFilePath({
canvasId: 'mycanvas',
success: (res) => {
console.log("保存的信息", res);
this.clearImageEdgeBlank(res.tempFilePath)
.then(path => {
this.tesImg = path
})
}
})
},
clearImageEdgeBlank(url, padding = 0) {
return new Promise((resolve, reject) => {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
const image = new Image();
image.onload = draw;
image.src = url;
image.crossOrigin = "Anonymous";
function draw() {
canvas.width = image.width;
canvas.height = image.height;
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const {
data,
width,
height
} = imageData;
let startX = width,
startY = height,
endX = 0,
endY = 0;
for (let col = 0; col < width; col++) {
for (let row = 0; row < height; row++) {
const pxStartIndex = (row * width + col) * 4;
const pxData = {
r: data[pxStartIndex],
g: data[pxStartIndex + 1],
b: data[pxStartIndex + 2],
a: data[pxStartIndex + 3]
};
const colorExist = pxData.a !== 0;
if (colorExist) {
startX = Math.min(col, startX);
endX = Math.max(col, startX);
startY = Math.min(row, startY);
endY = Math.max(row, endY);
}
}
}
endX += 1;
endY += 1;
startX -= padding;
startY -= padding;
endX += padding;
endY += padding;
const cropCanvas = document.createElement("canvas");
const cropCtx = cropCanvas.getContext("2d");
cropCanvas.width = endX - startX;
cropCanvas.height = endY - startY;
cropCtx.drawImage(
image,
startX,
startY,
cropCanvas.width,
cropCanvas.height,
0,
0,
cropCanvas.width,
cropCanvas.height
);
resolve(cropCanvas.toDataURL());
}
});
}
}
}
</script>
<style lang="scss" scoped>
.mycanvas {
height: 600rpx;
width: 100%;
background-color: #fff;
}
</style>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了