用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>
相关推荐
克里斯蒂亚诺更新13 小时前
微信小程序使用vant4 weapp自定义菜单 但是弹出层却被菜单遮挡的解决办法
微信小程序·小程序·notepad++
静Yu13 小时前
从一个九宫格素材小程序,看轻量工具产品该如何优化体验
前端·微信小程序
小羊Yveesss17 小时前
2026年微信小程序制作工具怎么选?
微信小程序·小程序
河北清兮网络科技18 小时前
深度解析:2026石家庄短视频APP开发真实成本、隐性开销与避坑方案
大数据·小程序·app·短剧app·广告联盟
微擎应用19 小时前
宠物门店系统 - 连锁宠物医院多门店小程序+公众号双端管理系统
小程序·宠物
脱脱克克20 小时前
使用 TRAE / VS Code + DeepSeek V4 开发微信小程序、网页
微信小程序·ai编程·环境配置
2501_9159214320 小时前
uni-app 上架 iOS 的完整流程(无需依赖 Mac)
android·macos·ios·小程序·uni-app·iphone·webview
前端 贾公子20 小时前
使用 wxappUnpacker 工具进行 MAC 微信小程序反编译
macos·微信小程序·小程序
CRMEB系统商城1 天前
CRMEB多商户系统(Java)v2.3公测版发布
java·开发语言·人工智能·小程序·开源·php
于先生吖2 天前
前后端分离二手商城开发,质检登记、回收回款整套业务源码部署教程
java·开发语言·uni-app