用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>
相关推荐
LT101579744412 小时前
2026年在线兼容性测试工具推荐|零部署网页 / APP / 小程序实测对比
测试工具·小程序
码农客栈14 小时前
小程序学习(二十八)之“订单列表”
小程序
天丁o20 小时前
Spring Boot + uni-app 智慧考勤闭环 Demo:打卡记录、异常状态和日统计如何复用到企业系统
spring boot·uni-app·mybatis plus·企业管理系统·考勤系统
这是个栗子20 小时前
uni-app 微信小程序开发:常用事件指令(@xxx)(一)
微信小程序·小程序·uni-app
2601_962344621 天前
计算机毕业设计之基于大数据的投保数据的分析系统的设计与实现
大数据·人工智能·深度学习·机器学习·信息可视化·小程序·课程设计
黑黑的独立开发笔记2 天前
「 简记往来」第十五篇:小程序性能优化——首屏从2.5秒到1.2秒
性能优化·小程序·首屏优化·分包加载·setdata·简记往来
tcdos4 天前
不止扫码 — 微信生态深度融合(登录 + 支付 + 消息)
后端·微信小程序
小徐_23334 天前
Wot UI 2.2.0 发布:Button 新增 subtle,VideoPreview 预览体验继续增强
前端·微信小程序·uni-app
宸翰6 天前
解决 uni-app App 端 vue-i18n 占位符丢失:封装跨端可用的 tf 格式化方法
前端·vue.js·uni-app
时光足迹6 天前
uni-app 视频通话实战:康复师与患者视频问诊的 6 个致命 Bug 与解决方案
android·ios·uni-app