uniapp实现签名

小编能力有限,如有错误希望大佬指点(复制可直接使用以组件的形式)

①组件

主要是通过uniapp自带的画布组件,通过鼠标事件,最后转成base64实现

<template>
	<view>
		<view class="box" :style="{height:height}">
			<view class="top">
				<canvas class="canvas-box" @touchstart='touchstart' @touchmove="touchmove" @touchend="touchend"
					canvas-id="myCanvas">
				</canvas>
				<view>请在此处签名</view>
			</view>
			<view class=" bottom">
				<u-tag text="完成" mode="dark" @click="finish" />
				<u-tag text="重签" mode="dark" @click="clear" />
			</view>
		</view>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				ctx: '', //绘图图像
				points: [], //路径点集合
				height: '10px', //高度
				canvasShoww: false, //提示
				isDraw: false,
			}
		},
		created() {
			let that = this
			uni.getSystemInfo({
				success: function(res) {
					that.height = res.windowHeight + 'px';
				},
			});
		},
		mounted() {
			this.createCanvas()
		},
		methods: {
			//创建并显示画布
			createCanvas() {
				this.ctx = uni.createCanvasContext('myCanvas', this); //创建绘图对象
				//设置画笔样式
				this.ctx.lineWidth = 4;
				this.ctx.lineCap = 'round';
				this.ctx.lineJoin = 'round';
			},

			// 触摸开始
			touchstart(e) {
				this.isDraw = true
				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(e) {
				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() {
				let that = this;
				if (this.imageShow) {
					if (this.imageUrl) {
						this.imageUrl = '';
						this.imageShow = false;

					}
				} else {
					uni.getSystemInfo({
						success: function(res) {
							// 设置画笔样式
							let canvasw = res.windowWidth;
							let canvash = res.windowHeight;
							that.ctx.clearRect(0, 0, canvasw, canvash);
							that.ctx.draw(true);
						}
					});
				}
				this.createCanvas();
			},

			//完成绘画并保存到本地
			finish() {
				if (this.isDraw) {
					let that = this;
					uni.canvasToTempFilePath({
						canvasId: 'myCanvas',
						success: function(res) {
							that.imageShow = true;
							that.imageUrl = res.tempFilePath;
							that.$emit('change', res.tempFilePath);
						}
					}, );
					return
				}
				this.$u.func.showToast({
					title: '请签名',
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		width: 100%;
		display: flex;
		flex-direction: column;
		background-color: #fff;

		.top {
			height: 95%;
			margin: 50rpx;
			border: 1px solid #000;
			position: relative;



			.canvas-box {
				width: 100%;
				height: 100%;
			}
		}

		.bottom {
			height: 5%;
			display: flex;
			align-items: flex-start;
			justify-content: space-around;
		}
	}
</style>

②使用以及引入

<template>
	<view>
		<sign @change="change"></sign>
	</view>
</template>
<script>
	import sign from '@/components/sign/index.vue'//对应的组件存放地址
	export default {
		data() {
			return {

			}
		},
		components: {
			sign
		},
		methods: {
			// 用户签名数据
			change(e) {
				console.log(e)//获取到base64的图片信息,可实现上传到服务器
                 //可结合使用uni.uploadFile()实现上传
			},
		}
	}
</script>

猜你喜欢

转载自blog.csdn.net/A1123352950/article/details/132426543