用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>
相关推荐
游戏开发爱好者81 小时前
iOS 上架要求全解析,App Store 审核标准、开发者准备事项与开心上架(Appuploader)跨平台免 Mac 实战指南
android·macos·ios·小程序·uni-app·iphone·webview
00后程序员张1 小时前
混淆 iOS 类名与变量名的实战指南,多工具组合把混淆做成工程能力(混淆 iOS 类名变量名/IPA 成品混淆Ipa/Guard CLI 实操)
android·ios·小程序·https·uni-app·iphone·webview
码起来呗4 小时前
基于Spring Boot的乡村拼车小程序的设计与实现-项目分享
spring boot·后端·小程序
2501_916007475 小时前
iOS文件管理工具深度剖析,从系统沙盒到跨平台文件操作的多工具协同实践
android·macos·ios·小程序·uni-app·cocoa·iphone
shykevin5 小时前
uni-app x开发商城系统,扩展组件uni-ui实现底部商品导航
uni-app
wapchief6 小时前
微信小程序camera相机帧转图片base64
微信小程序·小程序
QuantumLeap丶6 小时前
《uni-app跨平台开发完全指南》- 05 - 基础组件使用
vue.js·微信小程序·uni-app
发财北6 小时前
全屋智能家居定制小程序
小程序
2501_915918417 小时前
Flutter 加固方案对比与实战,多工具组合的跨平台安全体系(Flutter App 加固/IPA 成品混淆/Ipa Guard CLI/自动化安全流程)
安全·flutter·ios·小程序·uni-app·自动化·iphone
泽_浪里白条8 小时前
UniApp + Vue3 开发微信小程序数字人:TTS PCM 音频流与 SVGA 动画同步实战
微信小程序