【uni-app】uni-app实现手写签名效果:

文章目录

        • 一、效果:
        • 二、实现:
        • 三、扩展:


一、效果:

【uni-app】uni-app实现手写签名效果:_第1张图片

二、实现:
<template>
	<view class="signature">
		<canvas class="signature-canvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove"
			@touchend="touchend"></canvas>
		<view class="signature-button">
			<button type="warn" @click="clear">重写</button>
			<button type="primary" @click="finish">保存</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				ctx: '', //绘图图像
				points: [], //路径点集合 
				signature: '', //签名后台图片地址
			}
		},
		onLoad() {
			this.createCanvas()
		},
		methods: {
			//创建并显示画布
			createCanvas: function() {
				this.ctx = uni.createCanvasContext("mycanvas", this); //创建绘图对象
				this.ctx.setStrokeStyle('red')
				//设置画笔样式
				this.ctx.lineWidth = 4;
				this.ctx.lineCap = "round"
				this.ctx.lineJoin = "round"
			},
			//触摸开始,获取到起点
			touchstart: function(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: function(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: function() {
				this.points = [];
			},
			/* ***********************************************
			#   绘制笔迹
			#	1.为保证笔迹实时显示,必须在移动的同时绘制笔迹
			#	2.为保证笔迹连续,每次从路径集合中区两个点作为起点(moveTo)和终点(lineTo)
			#	3.将上一次的终点作为下一次绘制的起点(即清除第一个点)
			************************************************ */
			draw: function() {
				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: function() {
				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);
					},
				})
			},
			//完成绘画并保存到本地
			finish: function() {
				let that = this;
				uni.canvasToTempFilePath({
					canvasId: 'mycanvas',
					success: function(res) {
						console.log(res)

						//上传到服务器
						// that.api.uploadFile({
						// 	url: 'user/upload/one',
						// 	filePath: res.tempFilePath,
						// 	name: 'file',
						// 	success: (uploadFileRes) => {
						// 		console.log(uploadFileRes)
						// 		that.signature = uploadFileRes.data.url;
						// 		that.clear();
						// 		that.showCanvas = false;
						// 	}
						// })

						//保存到本地
						// let path = res.tempFilePath;
						// uni.saveImageToPhotosAlbum({
						// 	filePath: path,
						// 	success: function() {
						// 		uni.showToast({
						// 			title: "保存成功"
						// 		})
						// 	},
						// 	fail: function() {
						// 		uni.showToast({
						// 			title: "保存失败"
						// 		})
						// 	}
						// })
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.signature {
		padding: 0 10px;

		&-canvas {
			width: 100%;
			height: 360px;
			box-sizing: border-box;
			border: 1px solid lightgrey;
		}

		&-button {
			width: 100%;
			margin-top: 10px;
			display: flex;

			button {
				flex: 1;
				border-radius: 0 !important;
			}
		}
	}
</style>
三、扩展:

https://uniapp.dcloud.net.cn/api/canvas/CanvasContext.html

【uni-app】uni-app实现手写签名效果:_第2张图片

你可能感兴趣的:(小程序端,uni-app)