uniapp使用canvas完成手写电子签名
效果
<template>
<view >
<canvas class="mycanvas" canvas-id="mycanvas" v-bind:style="{width: (width*2-20) + 'rpx', height: (height*2-150) + 'rpx' }" @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>
// 阻止了touchmove事件
//在PC端运行这段代码基本上没有问题了,但在微信公众号打开或是其他移动端浏览器打开时都会出现和浏览器原生滑动事件冲突的现象,
//例如微信公众号里写‘丨’的时候会造成浏览器页面下拉,所写笔画会出现中断严重影响用户体验,需要添加如下代码:
document.body.addEventListener('touchmove', function (e) {
e.preventDefault();
}, { passive: false });
//--------------------------------------
var x = 20;
var y =20;
export default{
data(){
return {
width:0,
height:0,
ctx: '', //绘图图像
points: [], //路径点集合
isEnd: false, // 是否结束签名
}
},
onLoad() {
var that = this
//获取系统信息
uni.getSystemInfo({
success: function (res) {
that.ctx = uni.createCanvasContext("mycanvas",that); //创建绘图对象
that.width = res.windowWidth
that.height = res.windowHeight
//设置画笔样式
that.ctx.lineWidth = 3;
that.ctx.lineCap = "round"
that.ctx.lineJoin = "round"
that.ctx.fillStyle="white"
that.ctx.fillRect(0, 0, res.windowWidth, res.windowHeight)
}
});
},
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
},
/* ***********************************************
# 绘制笔迹
# 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) {
that.isEnd = false
let canvasw = res.windowWidth;
let canvash = res.windowHeight;
that.ctx.clearRect(0, 0, canvasw, canvash);
that.ctx.draw(true);
},
})
that.ctx = uni.createCanvasContext("mycanvas",that); //创建绘图对象
//设置画笔样式
that.ctx.lineWidth = 3;
that.ctx.lineCap = "round"
that.ctx.lineJoin = "round"
that.ctx.fillStyle="white"
that.ctx.fillRect(0, 0, this.width, this.height)
},
//完成绘画并保存到本地
finish:function(){
let that = this
if(!this.isEnd){
uni.showToast({
title: '请先完成签名',
icon: "none",
duration: 1000,
mask: true
})
return
}
uni.canvasToTempFilePath({
canvasId: 'mycanvas',
success: function(res) {
//生成的图片路径
let path = res.tempFilePath;
}
})
}
},
}
</script>
<style>
.mycanvas{
border-radius: 20rpx;
background-color: white;
margin: auto;
}
.footer{
position: fixed;
width: 100%;
bottom: 0;
background:white;
border-top: 1rpx solid #d7d7d7;
font-size: 16px;
height: 150rpx;
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>