uniapp手写签名
HTML
<template>
<view >
<canvas class="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove" @touchend="touchend"></canvas>
<view class="footer">
<view class="left" @click="finish" style="width: 100%;">确定</view>
</view>
</view>
</template>
js
<script>
var x = 20;
var y =20;
export default{
data(){
return {
ctx: '',
points: [],
isEnd: false,
}
},
created() {
this.ctx = uni.createCanvasContext("mycanvas",this);
this.ctx.lineWidth = 4;
this.ctx.lineCap = "round"
this.ctx.lineJoin = "round"
this.ctx.fillStyle="#fff"
this.ctx.fillRect(0,0,300,300);
},
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){
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.isEnd = true
},
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
if(!this.isEnd){
uni.showToast({
title: '请先完成签名',
icon: "none",
duration: 1500,
mask: true
})
return
}
uni.canvasToTempFilePath({
canvasId: 'mycanvas',
success: function(res) {
console.log(res)
that.$emit('signDone',res.tempFilePath)
}
})
}
},
}
</script>
CSS
<style>
.title{
height:50upx;
line-height: 50upx;
font-size: 16px;
}
.mycanvas{
width: 100%;
height: 400upx;
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>