uni-app經過canvas實現手寫簽名

分享一個uni-app實現手寫簽名的方法css

具體代碼以下:canvas

<template>
	<view >
		<view class="title">請在下面輸入簽名:</view>
		<canvas class="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove" @touchend="touchend"></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 = 4;
			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};
				
				/* **************************************************
					#因爲uni對canvas的實現有所不一樣,這裏須要把起點存起來
				 * **************************************************/
				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(){
				uni.canvasToTempFilePath({
				  canvasId: 'mycanvas',
				  success: function(res) {
				    let path = res.tempFilePath;
					uni.saveImageToPhotosAlbum({
						filePath:path
					})
				  } 
				})
			}
		},
	}
</script>

<style>
	.title{
		height:50upx;
		line-height: 50upx;
		font-size: 16px;
	}
	.mycanvas{
		width: 100%;
		height: calc(100vh - 200upx);
		background-color: #ECECEC;
	}
	.footer{
		font-size: 16px;
		height: 150upx;
		display: flex;
		justify-content: space-around;
		align-items: center;
	}
	.left,.right{
		line-height: 100upx;
		height: 100upx;
		width: 250upx;
		text-align: center;
		font-weight: bold;
		color: white;
		border-radius: 5upx;
	}
	.left{
		background: #007AFF;
	}
	.right{
		background:orange;
	}
</style>
相關文章
相關標籤/搜索