用uniapp在微信小程序实现画板(电子签名)功能,使用canvas实现功能

效果:

功能:实现重签 退出 保存 等功能

解决的问题: 电子签名画布抖动问题解

注意: 保存的时候上传到自己的服务器地址,后端返回图片地址

代码:

javascript 复制代码
<template>
	<view>
		<view class="signature" v-show="showCanvas">
			<canvas class="mycanvas" canvas-id="drawCanvas" id="drawCanvas" @touchstart="touchstart"
				@touchmove="touchmove" @touchend="touchend" @touchcancel="touchend" disable-scroll></canvas>
		</view>
		<view class="footer">
			<view class="invite left" @tap="finish">保存</view>
			<view class="invite close" @click="close">退出</view>
			<view class="invite right" @click="clear">重签</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				height: "",
				showCanvas: true,
				ctx: '', // 绘图图像
				points: [], // 路径点集合
				signature: '',
				canvasWidth: 0,
				canvasHeight: 0
			}
		},
		created() {
			const res = uni.getSystemInfoSync();
			this.height = res.windowHeight;
			this.canvasWidth = res.windowWidth;
			this.canvasHeight = res.windowHeight - 100; // 估算高度,保留底部按钮空间
		},
		methods: {
			close() {
				this.clear();
				uni.navigateBack({
					delta: 1,
				});
			},
			createCanvas() {
				this.showCanvas = true;
				this.ctx = uni.createCanvasContext("drawCanvas", this);
				this.ctx.lineWidth = 4;
				this.ctx.lineCap = "round";
				this.ctx.lineJoin = "round";
			},
			touchstart(e) {
				e.preventDefault();
				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) {
				e.preventDefault(); // 阻止默认滚动行为
				let moveX = e.changedTouches[0].x;
				let moveY = e.changedTouches[0].y;
				let movePoint = {
					X: moveX,
					Y: moveY
				};
				this.points.push(movePoint);
				if (this.points.length >= 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);
			},
			clear() {
				this.ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight);
				this.ctx.draw(true);
			},
			finish() {
				let that = this;
				uni.canvasToTempFilePath({
					destWidth: 112.5,
					destHeight: 180,
					canvasId: 'drawCanvas',
					success: function(res) {
						console.log("res", res);
						let path = res.tempFilePath;
						that.$emit("store", path);
					},
					fail(res) {
						console.log("err", res);
					}
				}, this);
			}
		},
		mounted() {
			this.createCanvas();
		}
	}
</script>

<style scoped>
	.signature {
		z-index: 0;
		width: 100vw;
		touch-action: none;
		/* 禁用默认触摸行为 */
	}

	page {
		background: #fff;
	}

	.mycanvas {
		width: 750rpx;
		height: calc(100vh - 200upx);
		touch-action: none;
		/* 禁用默认触摸行为 */
	}

	.footer {
		display: flex;
		flex-direction: row-reverse;
		position: fixed;
		bottom: 0;
		width: 100%;
		padding: 10px 0;
		margin-bottom: 20px;
	}

	.invite {
		width: 72px;
		height: 32px;
		font-size: 12px;
		text-align: center;
		line-height: 32px;
		color: #fff;
		border-radius: 3px;
		background-color: #1e7061;
		margin: 0 10px;
	}

	.left {
		background: #316f60;
	}

	.right {
		background: #8c8c8c;
	}

	.close {
		background: #cd6666;
	}
</style>
相关推荐
土土哥V_araolin3 小时前
双迹美业模式系统开发(现成源码)
小程序·个人开发·零售
2501_9339072110 小时前
南京本凡科技,怎么选择专业小程序开发才能提升品牌竞争力?
科技·微信小程序·小程序
笨笨狗吞噬者10 小时前
维护 uniapp 小程序端近一年,我想拉一个开发者交流群
前端·程序员·uni-app
心无旁骛~11 小时前
【Claude Code开发】AI记账助手(miaozhang)微信小程序开发与部署完整指南
人工智能·微信小程序·notepad++
2501_9339072113 小时前
本凡科技提供的宁波小程序开发服务全面解决方案
科技·微信小程序·小程序
阿珊和她的猫13 小时前
微信小程序静默授权异步问题的处理方案
微信小程序·状态模式·notepad++
左师佑图13 小时前
微信小程序集成 Day.js 插件的完整解决方案
微信小程序·小程序
2501_9339072113 小时前
本凡科技提供宁波小程序服务与定制解决方案
科技·微信小程序·小程序
计算机徐师兄14 小时前
Java基于SpringBoot的运动健康小程序【附源码、文档说明】
spring boot·小程序·运动健康·java运动健康小程序·运动健康小程序·java运动健康微信小程序·运动健康微信小程序
Xpower 1714 小时前
OpenClaw实战:从零开发电商小程序(2)
人工智能·语言模型·小程序·gateway