1. 程式人生 > 其它 >uniapp h5端手寫板簽字版

uniapp h5端手寫板簽字版

技術標籤:javascriptcanvasvuecanvasweb app

<template>
 <view class="">
 	<!-- <button @tap="createCanvas"  v-show="!showCanvas">簽名</button> -->
	<!-- <image id='signatureImg' :src='signImage' v-show="!showCanvas"></image> -->
 	<!-- <view class="signature" v-show="showCanvas"> -->
	<view class="signature">
 		<canvas class="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove" @touchend="touchend"></canvas>
 		<view class="footer">
			<view class="close" @click="goBack">返回</view>
 			<view class="right" @click="clear">清除</view>
			<view class="left" @click="signDone">完成</view>
			<!-- <view class="left" @click="finish">儲存</view> -->
 			<!-- <view class="close" @click="close">瀏覽</view> -->
 		</view>
		 <!-- <image id='signatureImg' class="signatureImg" :src='signImage'></image> -->
 	</view>
 </view>
</template>

<script>
	var x = 20;
	var y =20;

export default {
	onLoad() {
		this.createCanvas();
	},
	data() {
		return {
			showCanvas:false,
			ctx:'',         //繪圖影象
			points:[],       //路徑點集合 
			signature:'',
			signImage: ''
		}
	},
	methods: {
		//關閉並清空畫布
		close:function(){
			this.showCanvas = false;
			this.clear();
			
		},
		//建立並顯示畫布
		createCanvas:function(){
			this.showCanvas = true;
			
			this.ctx = uni.createCanvasContext("mycanvas",this);   //建立繪圖物件
			
			//設定畫筆樣式
			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=[];
			this.finish()
		},
		
		/* ***********************************************
		#   繪製筆跡
		#	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);
					that.finish();
				},
			})
		},
		//完成繪畫並儲存到本地
		finish:function(){
			let that = this;
			uni.canvasToTempFilePath({
			  canvasId: 'mycanvas',
			  success: function(res) {
				// console.log(res.tempFilePath)
				// console.log(res)
				//設定儲存的圖片
				that.signImage = res.tempFilePath;
				that.$store.commit('setImg', {
					sign_img: res.tempFilePath,
				})
				//上傳到伺服器
				// 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,
				}) */
			  } 
			})
		},
		goBack() {
			let url = '/pages/index/index'
			uni.navigateTo({
				url: url
			})
		},
		signDone() {
			let url = '/pages/contract/contract'
			uni.navigateTo({
				url: url
			})
		}
	}
}
</script>

<style>
	.signature {position: fixed; top: 10px;left: 2%;z-index: 999;width:96%;}
	/* .signature {margin-left: 2%; width:96%;} */
	page{
		background: #fff;
	}
	.container {
		padding: 20rpx 0 120rpx 0;
		box-sizing: border-box;
	}
	.title{
		height:50upx;
		line-height: 50upx;
		font-size: 16px;
	}
	.mycanvas{
		width: 100%;
		height: calc(100vh - 280upx);
		/* height: 42vh; */
		margin-top: 80rpx;
		background-color: #ECECEC;
	}
	.footer{
		font-size: 14px;
		height: 150upx;
		display: flex;
		justify-content: space-around;
		align-items: center;
	}
	.left,.right,.close{
		line-height: 100upx;
		height: 100upx;
		width: 220upx;
		text-align: center;
		font-weight: bold;
		color: white;
		border-radius: 5upx;
	}
	.left{
		background: #007AFF;
	}
	.right{
		background:orange;
	}
	.close {
		background:#A3A3A3;
	}
	.signatureImg {
		width: 100%;
		height: 42vh;
		border-radius: 20rpx;
		border: 2rpx solid #ddd;
		background-color: #fff;
	}
</style>