Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program canvas canvas


May 18, 2021 WeChat Mini Program Development Document


Table of contents


Base library 1.0.0 starts to support, and low versions need to be compatible.

Canvas. 2 .9.0 supports a new set of Canvas 2D interfaces (type properties required) and same-layer rendering, with the original interface no longer maintained. Related api: Get canvas instances.

Property Type The default Required Description The lowest version
type string Whether Specify canvas type, support 2d (2.9.0) and webgl (2.7.0) 2.7.0
canvas-id string Whether A unique identifier for the canvas component, which is no longer required if a type is specified 1.0.0
disable-scroll boolean false Whether Screen scrolling and pull-down refreshes are prohibited when moving in canvas and there are binding gesture events 1.0.0
bindtouchstart eventhandle Whether The finger touch action begins 1.0.0
bindtouchmove eventhandle Whether Move your finger when you touch it 1.0.0
bindtouchend eventhandle Whether The finger touch action ends 1.0.0
bindtouchcancel eventhandle Whether Finger touch movements are interrupted, such as a call alert, and a window is played 1.0.0
bindlongtap eventhandle Whether Triggering a finger after you press and hold 500ms triggers a long press event and moving does not trigger a scroll on the screen 1.0.0
binderror eventhandle Whether When an error occurs, the error event is triggered, and the detail is s.errMsg. 1.0.0

Bug & Tip

  1. tip: Canvas label default width 300px, height 150px
  2. tip: Canvas-id on the same page is not repeatable, and if you use a canvas-id that has already appeared, the canvas label's corresponding canvas will be hidden and no longer working properly
  3. TIP: Please pay attention Native components use restrictions
  4. TIP: The GPU hardware acceleration is turned off in the developer tool, and "Hardware Acceleration" improves WebGL rendering performance in the settings of the developer tool.
  5. Tip: WebGL supports the canvas for transparent background by getContext ('WebGL', {alpha: true})
  6. Tip: Canvas 2D (New Interface) Need to explicitly set the canvas wide (default 300x150)
  7. BUG: Avoid setting too much as high, there will be Crash issues under Android

Canvas 2D sample code

Preview effect in the developer tool

  <!-- canvas.wxml -->
  <canvas type="2d" id="myCanvas"></canvas>
// canvas.js
Page({
  onReady() {
    const query = wx.createSelectorQuery()
    query.select('#myCanvas')
      .fields({ node: true, size: true })
      .exec((res) => {
        const canvas = res[0].node
        const ctx = canvas.getContext('2d')

        const dpr = wx.getSystemInfoSync().pixelRatio
        canvas.width = res[0].width * dpr
        canvas.height = res[0].height * dpr
        ctx.scale(dpr, dpr)

        ctx.fillRect(0, 0, 100, 100)
      })
  }
})

WebGL sample code

Preview the effect in the developer tool

  <!-- canvas.wxml -->
  <canvas type="webgl" id="myCanvas"></canvas>
// canvas.js
Page({
  onReady() {
    const query = wx.createSelectorQuery()
    query.select('#myCanvas').node().exec((res) => {
      const canvas = res[0].node
      const gl = canvas.getContext('webgl')
      gl.clearColor(1, 0, 1, 1)
      gl.clear(gl.COLOR_BUFFER_BIT)
    })
  }
})

Sample code (old interface)

Preview the effect in the developer tool Download

<!-- canvas.wxml -->
<canvas style="width: 300px; height: 200px;" canvas-id="firstCanvas"></canvas>
<!-- 当使用绝对定位时,文档流后边的 canvas 的显示层级高于前边的 canvas -->
<canvas style="width: 400px; height: 500px;" canvas-id="secondCanvas"></canvas>
<!-- 因为 canvas-id 与前一个 canvas 重复,该 canvas 不会显示,并会发送一个错误事件到 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) {
    // 使用 wx.createContext 获取绘图上下文 context
    var 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()
  }
})