1.在pages.json
{
"path": "pages/index/autograph",
"style": {
"navigationBarTitleText": "手动签名",
"pageOrientation": "landscape",
"disableScroll": true
}
}
2.页面
<template>
<view >
<canvas class="mycanvas" style="width: 100vw; height: 75vh;" :disable-scroll="true" type="2d" id="myCanvas" @touchstart="touchstart" @touchmove="touchmove" @touchend="touchend"></canvas>
<view class="footer">
<view class="left" @click="finish">取消</view>
<view class="left" @click="clear">清除</view>
<view class="right" @click="finish">完成</view>
</view>
</view>
</template>
<script>
export default{
data(){
return {
windowWidth: 0,
ctx:'', //绘图图像
points:[],//路径点集合
oldPoints: [], //用于判断是否是空图片
}
},
onLoad() {
const systemInfo = uni.getSystemInfoSync()
this.windowWidth = systemInfo.windowWidth
const query = uni.createSelectorQuery()
let that = this
query.select('#myCanvas')
.fields({ node: true, size: true })
.exec((res) => {
console.log("res",res);
const canvas = res[0].node
this.canvas = res[0].node
that.ctx = canvas.getContext('2d')
that.ctx.lineWidth = 4 / 750 * this.windowWidth;
that.ctx.lineCap = "round" //设置线
that.ctx.lineJoin = "round"
canvas.width = res[0].width
canvas.height = res[0].height
})
},
methods:{
//触摸开始,获取到起点
touchstart(e){
let startPoint={
X: e.changedTouches[0].x,
Y: e.changedTouches[0].y
}
// 由于uni对canvas的实现有所不同,这里需要把起点存起来
this.points.push(startPoint);
//每次触摸开始,开启新的路径
this.ctx.beginPath();
},
//触摸移动,获取到路径点
touchmove(e){
let movePoint={
X: e.changedTouches[0].x,
Y: e.changedTouches[0].y
}
this.points.push(movePoint); //存点
this.oldPoints = JSON.parse(JSON.stringify(this.points))
let len = this.points.length;
if(len>=2){
this.draw(); //绘制路径
}
},
// 触摸结束,将未绘制的点清空防止对后续路径产生干扰
touchend(){
this.points=[];
},
/* ***********************************************
# 绘制笔迹
# 1.为保证笔迹实时显示,必须在移动的同时绘制笔迹
# 2.为保证笔迹连续,每次从路径集合中区两个点作为起点(moveTo)和终点(lineTo)
# 3.将上一次的终点作为下一次绘制的起点(即清除第一个点)
************************************************ */
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()
},
//清空画布
clear(){
let that = this;
uni.getSystemInfo({
success: function(res) {
console.log("res===",res);
let canvasw = res.windowWidth;
let canvash = res.windowHeight;
that.ctx.clearRect(0, 0, canvasw, canvash);
},
})
this.oldPoints = []
},
//完成绘画
finish(){
let that= this
if (that.oldPoints.length == 0) {
uni.showToast({
title: '请进行签名!',
icon: 'none'
})
return
}
uni.canvasToTempFilePath({
canvas: that.canvas,
fileType: 'png',
quality: 1, //图片质量
success: function(res) {
let path = res.tempFilePath;
console.log("图片",path);
//保存
// uni.saveImageToPhotosAlbum({
// filePath:path
// })
} ,
fail: (err) => {
console.log('生成图片失败:', err)
}
})
},
},
}
</script>
<style>
.mycanvas{
width: 100%;
height: calc(100vh - 200rpx);
background-color: #ECECEC;
}
.footer{
font-size: 16px;
height: 70rpx;
display: flex;
justify-content: space-around;
align-items: center;
}
.left,.right{
line-height: 43px;
height: 40rpx;
width: 99rpx;
text-align: center;
font-weight: bold;
color: white;
border-radius: 38rpx;
}
.right{
background: #007AFF;
}
.left{
background:#efefef;
color: #333;
}
</style>