<view class = "canvas-area">
<canvas canvas-id = "myCanvas" class = "myCanvas" disable-scroll = "false" bindtouchstart = "touchStart" bindtouchmove = "touchMove" />
</view>
<view>
<view class = "box box1" bindtap = "penSelect" data-param = "5"></view>
<view class = "box box2" bindtap = "colorSelect" data-param = "#c03"></view>
</view>
let ctx;
Page({
data:{
pen:{
color:'#000',
lineWidth: 5
}
},
onload:function (options) {
ctx = wx.createCanvasContext('myCanvas')
ctx.setStrokeStyle(this.data.pen.color)
ctx.setLineWidth(this.data.pen.lineWidth)
ctx.setLineCap('round')
ctx.setLineJoin('round')
},
touchStart(e){
ctx.moveTo(e.touches[0].x,e.touches[0].y)
},
touchMove(e){
let x = e.touches[0].x
let y = e.touches[0].y
ctx.lineTo(x,y)
ctx.stroke()
ctx.draw(true)
ctx.moveTo(x,y)
},
penSelect(e){
console.log(e)
this.setData({
'pen.lineWith':e.target.dataset.param
})
},
colorSelect(e){
this.setData({
'pen.color':e.target.dataset.param
})
}
})