页面
<template>
<view class="new_file" >
<view class="popupBox" >
<view class="popupTopBox">签字板</view>
<canvas class="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove" @touchend="touchend"></canvas>
<view style="margin-top: 0rpx;">
<view class="determineBtn" @click="confirm">确定</view>
<view class="determineBtn2" @click="cancel">取消</view>
</view>
</view>
</view>
</template>
函数方法
//创建并显示签名画布
autograph(ss) {
this.stop();
this.showAutograph = true;
if(ss){
this.signatory = ss
}
//创建绘图对象
this.ctx = uni.createCanvasContext('mycanvas', this);
//设置画笔样式
this.ctx.lineWidth = 4;
this.ctx.lineCap = 'round';
this.ctx.lineJoin = 'round';
},
//触摸开始,获取到起点
touchstart(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(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() {
this.points = [];
},
draw() {
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() {
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);
}
});
this.move();
},
//关闭并清空画布
cancel() {
this.showAutograph = false;
this.clear();
},
//完成绘画并保存到本地
confirm() {
let that = this;
uni.canvasToTempFilePath({
canvasId: 'mycanvas',
success: function(res) {
if(that.signatory == '本人签名'){
that.myName = res.tempFilePath;
}else{
that.education = res.tempFilePath;
}
that.cancel();
//that.baseTransferPicture(res.tempFilePath);
}
});
},
css样式
.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;
}
.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;
}
page {
font-family: Source Han Sans CN;
}
.new_file {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
.buttONbOX {
position: fixed;
left: 50%;
top: 50%;
}
.popupBox {
position: fixed;
left: 50%;
bottom: 188upx;
transform: translate(-50%, 0);
width: 720upx;
height: 855upx;
background: #ffffff;
border-radius: 16upx;
.popupTopBox {
width: 100%;
height: 80upx;
background: #00aee7;
border-radius: 16upx 16upx 0px 0px;
text-align: center;
line-height: 80upx;
font-size: 32upx;
color: #ffffff;
}
.mycanvas {
width: 100%;
height: 821upx;
}
.determineBtn {
width: 280upx;
height: 69upx;
background: #00aee7;
border-radius: 35upx;
margin-left: 5%;
font-size: 30upx;
color: #ffffff;
text-align: center;
line-height: 69upx;
}
.determineBtn2 {
width: 280upx;
height: 69upx;
background: #ea0000;
border-radius: 35upx;
margin-left: 55%;
margin-top: -69rpx;
font-size: 30upx;
color: #ffffff;
text-align: center;
line-height: 69upx;
}
}
.closeIcon {
position: fixed;
left: 50%;
bottom: 92upx;
transform: translate(-50%, 0);
width: 50upx;
height: 50upx;
image {
width: 100%;
height: 100%;
}
}
}