WXML:
<view>
<canvas type="2d" style="width: 400px; height: 400px;" canvas-id="canvasId"></canvas>
<button bindtap="draw">绘制</button>
</view>
JS:
const app = getApp()
Page({
data: {},
onLoad: function () {
},
draw(){
console.log(111)
const ctx = wx.createCanvasContext('canvasId')
ctx.rect(10, 10, 100, 30)
ctx.setStrokeStyle('yellow')
ctx.stroke()
ctx.draw()
}
})
代码执行了没有效果是什么情况?如下:
新版和旧版的区别
type指定后,canvas-id属性就没用了,所以wx.createCanvasContext()就获取不到画布了,然后你写的代码都失效了
- - !额。。。把canvas元素的属性type="2d"删掉就行了,什么鬼