<template>
	<view class="center">
		
			<canvas class="canvas" canvas-id="mycanvas" @touchstart.stop="touchstart" @touchmove.stop="touchmove" @touchend.stop="touchend">
				<text class="txt-font" v-if="showtext">请在此处书写签名</text>
			</canvas>
		
		<view class="btn flex">
			<view class="button" @click.stop="clear"><text class="text">重写</text></view>
			<view class="button" @click.stop="submit"><text class="text">确认</text></view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				type: '',
				showtext: true,
				ctx: '',//绘画图像
				points: [], //路径点集合
				signature: ''
			}
		},
		onLoad(option) {
			this.type = option.type;
			this.ctx = uni.createCanvasContext("mycanvas",this);
			//设置画笔样式
			this.ctx.lineWidth = 1;
			this.ctx.lineCap = "round"
			this.ctx.lineJoin = "round"
		},
		methods: {
			//触摸开始，获取到起点
			touchstart: function(e) {
				this.showtext = false;
				// #ifdef H5
				let startX = e.changedTouches[0].x;
				let startY = e.changedTouches[0].y;
				// #endif
				// #ifdef MP
				let startX = e.changedTouches[0].clientX;
				let startY = e.changedTouches[0].clientY;
				// #endif
				let startPoint = { X: startX, Y: startY };
				this.points.push(startPoint);
				//每次触摸开始，开启新的路径
				this.ctx.beginPath();
			},
			//触摸移动，获取到路径点
			touchmove: function(e) {
				// #ifdef H5
				let moveX = e.changedTouches[0].x;
				let moveY = e.changedTouches[0].y;
				// #endif
				// #ifdef MP
				let moveX = e.changedTouches[0].clientX;
				let moveY = e.changedTouches[0].clientY;
				// #endif
				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 that = this;
				let point1 ={
					Y:that.points[0].Y,
					X:that.points[0].X,
				}; 
				let point2 = {
					Y:that.points[1].Y,
					X:that.points[1].X,
				}; ;
				// console.log(that.points);
				that.points.shift();
				that.ctx.moveTo(Math.floor(point1.X), Math.floor(point1.Y));
				that.ctx.lineTo(Math.floor(point2.X), Math.floor(point2.Y));
				// console.log('开始渲染地址', point1, point2);
				that.ctx.setStrokeStyle('#333333');
				that.ctx.setLineWidth(20);
				that.ctx.stroke();
				that.ctx.draw(true);
			},
			clear: function() {
				let obj = this;
				uni.getSystemInfo({
					success: function(res) {
						let canvasw = res.windowWidth;
						let canvash = res.windowHeight;
						obj.ctx.clearRect(0, 0, canvasw, canvash);
						obj.ctx.draw(true);
					}
				})
				this.showtext = true;
			},
			submit(){
				let obj = this;
				console.log(this.type);
				uni.canvasToTempFilePath({
					canvasId: 'mycanvas',
					success: function(res) {
						
						console.log(res,'------');
						uni.setStorageSync('mycanvas',res.tempFilePath);
						uni.navigateTo({
							url: '/pages/contract/Qrcontract?type=' + obj.type
						})
					}
				})
			}
		},
	}
</script>
<style lang="scss">
	page{
		height: 100%;
	}
	
	.canvas {
		text-align: center;
		line-height: 700rpx;
		width: 100%;
		height: 700rpx;
		border: 1rpx solid #f0f0f0;
	}
	.btn {
		position: absolute;
		left: 0;
		right: 0;
		bottom: 0;
		width: 100%;
		justify-content: space-between;
		padding: 53rpx;
		margin-bottom: 0;
		.button {
			width: 100rpx;
			height: 40rpx;
			display: flex;
			align-items: center;
			justify-content: space-around;
			border: 1rpx solid #FE4242;
			background: #FFFFFF;
			border-radius: 37rpx;
			line-height: 40rpx;
			text-align: center;
			.text {
				line-height: 40rpx;
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #FF0000;
			}
		}
	}
</style>
