Property | Type | Default value | Description |
type | string | 2d | Supported canvas type: 2d only. |
canvas-id | string | - | Unique identifier of the canvas component. |
disable-scroll | boolean | false | Prevents screen scrolling and pull-to-refresh when moving within the canvas and gesture events are bound. |
bindtouchstart | eventhandle | - | Triggered when a finger touch action starts. |
bindtouchmove | eventhandle | - | Triggered when a finger moves after touching. |
bindtouchend | eventhandle | - | Triggered when a finger touch action ends. |
bindtouchcancel | eventhandle | - | Triggered when a finger touch action is interrupted, such as by an incoming call or popup. |
bindlongtap | eventhandle | - | Triggered after a finger long-presses for 500 ms; moving after triggering the long-press event will not cause the screen to scroll. |
binderror | eventhandle | - | Triggered when an error occurs. detail = {errMsg: 'something wrong'} |
<canvas type="2d" id="myCanvas"></canvas>
Page({onReady() {const query = wx.createSelectorQuery()query.select('#myCanvas').fields({ node: true, size: true }).exec((res) => {const canvas = res[0].nodeconst ctx = canvas.getContext('2d')const dpr = wx.getSystemInfoSync().pixelRatiocanvas.width = res[0].width * dprcanvas.height = res[0].height * dprctx.scale(dpr, dpr)ctx.fillRect(0, 0, 100, 100)})}})
<!-- canvas.wxml --><canvas style="width: 300px; height: 200px;" canvas-id="firstCanvas"></canvas><!-- When using absolute positioning, the canvas at the back of the document flow is displayed at a higher level than the canvas at the front --><canvas style="width: 400px; height: 500px;" canvas-id="secondCanvas"></canvas><!-- Because the canvas-id is the same as the previous canvas, the canvas will not be displayed and an error event will be sent to AppService --><canvas style="width: 400px; height: 500px;" canvas-id="secondCanvas" binderror="canvasIdErrorCallback"></canvas>
Page({canvasIdErrorCallback: function (e) {console.error(e.detail.errMsg)},onReady: function (e) {// Uses wx.createContext to get the Canvas contextvar context = wx.createCanvasContext('firstCanvas')context.setStrokeStyle("#00ff00")context.setLineWidth(5)context.rect(0, 0, 200, 200)context.stroke()context.setStrokeStyle("#ff0000")context.setLineWidth(2)context.moveTo(160, 100)context.arc(100, 100, 60, 0, 2 * Math.PI, true)context.moveTo(140, 100)context.arc(100, 100, 40, 0, Math.PI, false)context.moveTo(85, 80)context.arc(80, 80, 5, 0, 2 * Math.PI, true)context.moveTo(125, 80)context.arc(120, 80, 5, 0, 2 * Math.PI, true)context.stroke()context.draw()}})
Was this page helpful?