uni-app简单实现手写签名

发布时间:2023-03-16 19:00

本次项目要求实现手写签名功能,首先百度了一下,度娘还是很强大的,有 vue 的,也有 uniapp 的,我这边用的是 uniapp,有个博主确实帮到了我,特此附上他(节节竹)的链接,方便各位看源码

https://blog.csdn.net/qq_32289849/article/details/106571184

废话不多说,上项目要求
\"uni-app简单实现手写签名_第1张图片\")
(1)html 此处模态框用的 uview,兼容性好一点,图片路径不必在意,用的七牛云上的图片

<view class=\"xsh-start\" @click=\"autograph\">签名</view>
//签名弹框
<u-modal v-model=\"showAutograph\" border-radius=\"14\" width=\"605rpx\" :show-title=\"false\" :confirm-style=\"{\'font-size\':\'29rpx\',\'color\': \'#341DB7\'}\"
		 :show-cancel-button=\'true\' :cancel-style=\"{\'font-size\':\'29rpx\',\'color\': \'#666666\'}\" @confirm=\"confirm\" @cancel=\"cancel\">
	<view class=\"x-modal\">
		<view class=\"x-m-title\">
			<text>请在框内进行签名</text>
			<view class=\"xm-t-clear\" @click=\"clear\">
				<image :src=\"`${configUrl}/clear.png`\" mode=\"\"></image>
				<text>清除</text>
			</view>
		</view>
		<view class=\"x-m-con\">
			//重点在此,画布
			<canvas class=\"mycanvas\" canvas-id=\"mycanvas\" @touchstart=\"touchstart\" @touchmove=\"touchmove\" @touchend=\"touchend\"></canvas>
		</view>
	</view>
</u-modal>

(2)js

<script>
	import config from \'../../common/config.js\' //封装文件,用来获取七牛云地址
	export default {
		data() {
			return {
				configUrl: config.STATIC_URL, //七牛云地址
				showAutograph: false, //签名弹框是否显示
				ctx: \'\', //绘图图像
				points: [], //路径点集合 
				signature: \'\'
			}
		},
		methods: {
			//创建并显示签名画布
			autograph() {
				this.showAutograph = true;
				//创建绘图对象
				this.ctx = uni.createCanvasContext(\"mycanvas\", this);
				//设置画笔样式
				this.ctx.lineWidth = 4;
				this.ctx.lineCap = \"round\"
				this.ctx.lineJoin = \"round\"
			},
			//触摸开始,获取到起点
			touchstart(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(); //绘制路径
				}
			},
			// 触摸结束,将未绘制的点清空防止对后续路径产生干扰
			touchend() {
				this.points = [];
			},
			/* ***********************************************
			#   绘制笔迹
			#	1.为保证笔迹实时显示,必须在移动的同时绘制笔迹
			#	2.为保证笔迹连续,每次从路径集合中区两个点作为起点(moveTo)和终点(lineTo)
			#	3.将上一次的终点作为下一次绘制的起点(即清除第一个点)
			************************************************ */
			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() {
				let that = this;
				uni.getSystemInfo({
					success: function(res) {
						let canvasw = res.windowWidth;
						let canvash = res.windowHeight;
						that.ctx.clearRect(0, 0, canvasw, canvash);
						that.ctx.draw(true);
					},
				})
			},
			//关闭并清空画布
			cancel() {
				this.showAutograph = false;
				this.clear();
			},
			//完成绘画并保存到本地
			confirm() {
				let that = this;
				uni.canvasToTempFilePath({
					canvasId: \'mycanvas\',
					success: function(res) {
						console.log(res.tempFilePath) 
						//图片格式为base64,如果不是可上传七牛云,之后请求签名接口即可
						that.$api.api.h5.sign({
							\"merSignName\": res.tempFilePath,
							\"workId\": that.workId
						}).then(res=>{
							if(res.result.code==\'000000\') {
								//走到这里就签名成功了
								uni.redirectTo({
									url:\'/pages/workOrder/workOrder\'
								})
							}else{
								that.$u.toast(res.result.message)
							}
						})
					}
				})
			},
		}
	}
</script>

(3)css 样式仅供参考哦,需求不一定一样

@mixin playcenter {
	display: flex;
	align-items: center;
	justify-content: center;
}
.xsh-start {
	width: 105rpx;
	height: 105rpx;
	background: #FFFFFF;
	border-radius: 50%;
	font-size: 29rpx;
	color: #4135EB;
	@include playcenter;
	flex-wrap: wrap;
}
.x-modal {
	width: 100%;
	.x-m-title {
		width: 100%;
		height: 90rpx;
		padding: 0 38rpx 0 31rpx;
		box-sizing: border-box;
		font-size: 29rpx;
		color: #333333;
		border-bottom: 1px dashed #999;
		@include playcenter;
		justify-content: space-between;
		.xm-t-clear {
			font-size: 25rpx;
			color: #341DB7;
			@include playcenter;
			>image {
				width: 28rpx;
				height: 28rpx;
				display: block;
				margin-right: 8rpx;
			}
		}
	}
	.x-m-con {
		width: 100%;
		padding: 0 31rpx 18rpx;
		margin-top: 5rpx;
		box-sizing: border-box;
	}
}

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号