示例代码(旧的接口)
index.wxml
<view class="container">
<view class="operating">
<text bindtap="ceshi">确定</text>
<text bindtap="clearSignature">清空</text>
</view>
<canvas canvas-id="firstCanvas" style=" width:{{canvasWidth+'px'}};height:{{canvasHeight+'px'}}" disable-scroll="true" bindtouchstart="uploadScaleStart" bindtouchmove="uploadScaleMove"
bindtouchend="uploadScaleEnd" bindtap="mouseDown">
</canvas>
</view>
index.wxss
canvas {
box-sizing: border-box;
border: 1px dashed black;
}
.operating{
width: 60px;
display: flex;
flex-direction: column;
height: 100%;
}
.operating>text{
transform:rotate(90deg);
margin-top: 25px;
}
index.js
const app = getApp()
let content = null;
var touchs = [];
Page({
data: {
canvasWidth: 0,
canvasHeight: 0
},
onLoad: function () {
this.setData({
canvasWidth: wx.getSystemInfoSync().windowWidth - 80,
canvasHeight: wx.getSystemInfoSync().windowHeight - 100
})
},
ceshi() {
wx.navigateTo({
url: '../logs/logs',
})
},
onShow: function () {
content = wx.createCanvasContext('firstCanvas')
content.setStrokeStyle("#00ff00")
content.setLineWidth(5)
content.setLineCap('round')
content.setLineJoin('round')
},
draw(touchs) {
let point1 = touchs[0]
let point2 = touchs[1]
touchs.shift()
content.moveTo(point1.x, point1.y)
content.lineTo(point2.x, point2.y)
content.stroke()
content.draw(true)
},
uploadScaleStart: function (event) {
let point = {
x: event.changedTouches[0].x,
y: event.changedTouches[0].y
}
touchs.push(point)
},
uploadScaleMove: function (e) {
let point = {
x: e.touches[0].x,
y: e.touches[0].y
}
touchs.push(point)
if (touchs.length >= 2) {
this.draw(touchs)
}
},
uploadScaleEnd: function (e) {
for (let i = 0; i < touchs.length; i++) {
touchs.pop()
}
},
clearSignature() {
console.log(233)
content.clearRect(0, 0, this.data.canvasWidth, this.data.canvasHeight)
content.draw(true)
},
saveClick() {
var that = this
wx.canvasToTempFilePath({
canvasId: 'firstCanvas',
success: function (res) {
console.log(res.tempFilePath)
that.setData({
signImage: res.tempFilePath
})
}
})
}
})
示例代码二(使用2d)
index.wxml
<view class="container">
<view class="operating">
<text bindtap="ceshi">确定</text>
<text bindtap="clearSignature">清空</text>
<text bindtap="saveClick">生成图片</text>
</view>
<canvas type="2d" id="firstCanvas" style=" width:{{canvasWidth+'px'}};height:{{canvasHeight+'px'}}" disable-scroll="true" bindtouchstart="uploadScaleStart" bindtouchmove="uploadScaleMove"
bindtouchend="uploadScaleEnd" bindtap="mouseDown">
</canvas>
</view>
index.wxss
canvas {
box-sizing: border-box;
border: 1px dashed black;
}
.operating{
width: 60px;
display: flex;
flex-direction: column;
height: 100%;
}
.operating>text{
transform:rotate(90deg);
margin-top: 25px;
}
index.js
const app = getApp()
let content = null;
let canvas = null
var touchs = [];
Page({
data: {
canvasWidth: 0,
canvasHeight: 0
},
onLoad: function () {
this.setData({
canvasWidth: wx.getSystemInfoSync().windowWidth - 80,
canvasHeight: wx.getSystemInfoSync().windowHeight - 100
})
},
ceshi() {
wx.navigateTo({
url: '../logs/logs',
})
},
onShow: function () {
const query = wx.createSelectorQuery()
query.select('#firstCanvas')
.fields({
node: true,
size: true
})
.exec((res) => {
console.log(res)
canvas = res[0].node
content = canvas.getContext('2d')
content.fillStyle = "#00ff00"
content.lineWidth = 3
content.lineCap = 'round'
content.lineJoin = 'round'
content.shadowBlur = 1;
content.shadowColor = '#000';
const dpr = wx.getSystemInfoSync().pixelRatio
canvas.width = res[0].width * dpr
canvas.height = res[0].height * dpr
content.scale(dpr, dpr)
})
},
draw(touchs) {
let point1 = touchs[0]
let point2 = touchs[1]
touchs.shift()
content.moveTo(point1.x, point1.y)
content.lineTo(point2.x, point2.y)
content.stroke()
},
uploadScaleStart: function (event) {
let point = {
x: event.changedTouches[0].x,
y: event.changedTouches[0].y
}
touchs.push(point)
content.beginPath();
},
uploadScaleMove: function (e) {
let point = {
x: e.touches[0].x,
y: e.touches[0].y
}
touchs.push(point)
if (touchs.length >= 2) {
this.draw(touchs)
}
},
uploadScaleEnd: function (e) {
for (let i = 0; i < touchs.length; i++) {
touchs.pop()
}
content.closePath();
},
clearSignature() {
console.log(233)
content.clearRect(0, 0, this.data.canvasWidth, this.data.canvasHeight)
content.draw(true)
},
saveClick() {
var imgBase64 = canvas.toDataURL();
console.log(imgBase64);
}
})