<template>
	<view class="container" @touchmove.prevent>
		<canvas class="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove"
			@touchend="touchend" :disable-scoll="true"></canvas>
		<view class="footer">
			<view class="left" @click="finish">
				上传签名
			</view>
			<view class="right" @click="clear">重签</view>
		</view>
	</view>
</template>

<script>
	var x = 20;
	var y = 20;
	export default {

		data() {
			return {
				ctx: '', //绘图图像
				points: [], //路径点集合,
			}
		},
		onLoad() {
			this.ctx = uni.createCanvasContext("mycanvas", this); //创建绘图对象
			//设置画笔样式
			this.ctx.lineWidth = 3;
			this.ctx.lineCap = "round"
			this.ctx.lineJoin = "round"
		},
		methods: {
			//触摸开始，获取到起点
			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) {
				e.preventDefault();
				// #ifdef H5
				window.event.returnValue = false;
				// #endif
				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() {
				console.log('提交签名');
				const _this = this;
				uni.canvasToTempFilePath({
					canvasId: 'mycanvas',
					success: function(res) {
						console.log(res)
						let path = res.tempFilePath;
						let res2 = getApp().globalData.request.upload('index/upload', path);
						res2.then(res => {
							console.log('h5res=', res);
							if (res.code == 0) {
								_this.$store.commit('setsignatureImgSrc', res.data);
								uni.navigateBack({
									delta: 1
								});
							}
						})
					},
					fail() {
						console.log('失败');
					},
					complete() {
						console.log('完成');
					}
				})
			}
		},
	}
</script>

<style scoped lang="scss">
	page {
		overflow: hidden
	}

	.container {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		/* #ifdef APP-PLUS || MP-WEIXIN */
		padding-top: 20rpx;
		/* #endif */
	}

	.mycanvas {
		width: 92%;
		height: 300rpx;
		background-color: #ECECEC;
		border-radius: 14rpx;
		margin: 0 auto;
		/* #ifdef H5 */
		margin-top: 100rpx;
		/* #endif */
	}

	.footer {
		width: 100%;
		font-size: 28rpx;
		height: 150rpx;
	}

	.left,
	.right {
		width: 90%;
		height: 88rpx;
		line-height: 88rpx;
		background: #008681;
		border-radius: 10rpx;
		color: #fff;
		text-align: center;
		margin: 20rpx auto;
	}
</style>
