onLoad: function () {
var that=this
// 通过 SelectorQuery 获取 Canvas 节点
const query=wx.createSelectorQuery()
.select('#canvas')
.fields({
node: true,
size: true,
})
.exec(
res=>{
const canvas = res[0].node
this.ctx = canvas.getContext('2d')
// that.setData({
// ctx
// })
const dpr = wx.getSystemInfoSync().pixelRatio
canvas.width = res[0].width * dpr
canvas.height = res[0].height * dpr
this.ctx.scale(dpr, dpr)
this.render(canvas, this.ctx)
}
)
},
render(canvas, ctx) {
let that = this;
let img = canvas.createImage();//canvas 2d 通过此函数创建一个图片对象
img.onload = (e) => {
console.log('img loaded')
ctx.drawImage(img, 0, 0, 300, 300);
// wx.canvasToTempFilePath({
// canvas,
// success(res) {
// console.log(res.tempFilePath)
// that.setData({
// imgSrc:res.tempFilePath
// })
// }
// }, that)
}
img.onerror = (e) => {
console.error('err:', e)
}
img.src = './car.png'
},
touchStart:function(e){
/*画圆圈
this.ctx.arc(30,30,10,0,Math.PI*2,true)
this.ctx.stroke()
*/
this.x1=e.touches[0].x
this.y1=e.touches[0].y
this.ctx.beginPath()
},
touchMove:function(e){
//画点
var x2=e.touches[0].x
var y2=e.touches[0].y
this.ctx.arc(x2,y2,10,0,Math.PI*2,true)
// this.ctx.lineTo(x2, y2)
// this.ctx.lineWidth=5
// this.ctx.strokeStyle="red"
this.ctx.stroke()
this.x1=x2
this.y1=y2
/*画线
var x2=e.touches[0].x
var y2=e.touches[0].y
this.ctx.moveTo(this.x1,this.y1)
this.ctx.lineTo(x2, y2)
this.ctx.lineWidth=5
this.ctx.strokeStyle="red"
this.ctx.stroke()
this.x1=x2
this.y1=y2
*/
},
touchEnd:function(e){
不太明白你想要的实现什么效果