十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
基础库 1.0.0 开始支持,低版本需做 兼容处理。
成都创新互联公司网站建设服务商,为中小企业提供网站设计制作、成都网站制作服务,网站设计,网站托管运营等一站式综合服务型公司,专业打造企业形象网站,让您在众多竞争对手中脱颖而出成都创新互联公司。
画布。2.9.0 起支持一套新 Canvas 2D 接口(需指定 type 属性),同时支持同层渲染,原有接口不再维护。相关api:获取 canvas 实例。
| 属性 | 类型 | 默认值 | 必填 | 说明 | 最低版本 | 
|---|---|---|---|---|---|
| type | string | 否 | 指定 canvas 类型,支持 2d (2.9.0) 和 webgl (2.7.0) | 2.7.0 | |
| canvas-id | string | 否 | canvas 组件的唯一标识符,若指定了 type 则无需再指定该属性 | 1.0.0 | |
| disable-scroll | boolean | false | 否 | 当在 canvas 中移动时且有绑定手势事件时,禁止屏幕滚动以及下拉刷新 | 1.0.0 | 
| bindtouchstart | eventhandle | 否 | 手指触摸动作开始 | 1.0.0 | |
| bindtouchmove | eventhandle | 否 | 手指触摸后移动 | 1.0.0 | |
| bindtouchend | eventhandle | 否 | 手指触摸动作结束 | 1.0.0 | |
| bindtouchcancel | eventhandle | 否 | 手指触摸动作被打断,如来电提醒,弹窗 | 1.0.0 | |
| bindlongtap | eventhandle | 否 | 手指长按 500ms 之后触发,触发了长按事件后进行移动不会触发屏幕的滚动 | 1.0.0 | |
| binderror | eventhandle | 否 | 当发生错误时触发 error 事件,detail = {errMsg} | 1.0.0 | 
在开发者工具中预览效果
  
  
// 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)
      })
  }
})
在开发者工具中预览效果
  
  
// 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)
    })
  }
})
在开发者工具中预览效果 下载
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()
  }
})