# canvas
基礎(chǔ)庫 1.0.0 開始支持,低版本需做兼容處理。
微信 Windows 版:支持
微信 Mac 版:支持
微信 鴻蒙 OS 版:支持
渲染框架支持情況:Skyline (使用最新 Nightly 工具調(diào)試)、WebView
# 功能描述
畫布。2.9.0 起支持一套新 Canvas 2D 接口(需指定 type 屬性),同時支持同層渲染,原有接口不再維護。舊版本可參考 舊版畫布遷移指南 進行遷移。
# 屬性說明
| 屬性 | 類型 | 默認(rèn)值 | 必填 | 說明 | 最低版本 |
|---|---|---|---|---|---|
| type | string | 否 | 指定 canvas 類型,支持 2d (2.9.0) 和 webgl (2.7.0) | 2.7.0 | |
| canvas-id | string | 否 | canvas 組件的唯一標(biāo)識符,若指定了 type 則無需再指定該屬性 | 1.0.0 | |
| disable-scroll | boolean | false | 否 | 當(dāng)在 canvas 中移動時且有綁定手勢事件時,禁止屏幕滾動以及下拉刷新 | 1.0.0 |
| bindtouchstart | eventhandle | 否 | 手指觸摸動作開始 | 1.0.0 | |
| bindtouchmove | eventhandle | 否 | 手指觸摸后移動 | 1.0.0 | |
| bindtouchend | eventhandle | 否 | 手指觸摸動作結(jié)束 | 1.0.0 | |
| bindtouchcancel | eventhandle | 否 | 手指觸摸動作被打斷,如來電提醒,彈窗 | 1.0.0 | |
| bindlongtap | eventhandle | 否 | 手指長按 500ms 之后觸發(fā),觸發(fā)了長按事件后進行移動不會觸發(fā)屏幕的滾動 | 1.0.0 | |
| binderror | eventhandle | 否 | 當(dāng)發(fā)生錯誤時觸發(fā) error 事件,detail = {errMsg} | 1.0.0 |
# Bug & Tip
tip:canvas 標(biāo)簽?zāi)J(rèn)寬度300px、高度150pxtip:同一頁面中的 canvas-id 不可重復(fù),如果使用一個已經(jīng)出現(xiàn)過的 canvas-id,該 canvas 標(biāo)簽對應(yīng)的畫布將被隱藏并不再正常工作tip:請注意原生組件使用限制tip:開發(fā)者工具中默認(rèn)關(guān)閉了 GPU 硬件加速,可在開發(fā)者工具的設(shè)置中開啟“硬件加速”提高 WebGL 的渲染性能tip: WebGL 支持通過 getContext('webgl', { alpha: true }) 獲取透明背景的畫布tip: WebGL 暫不支持真機調(diào)試,建議使用真機預(yù)覽tip: Canvas 2D(新接口)需要顯式設(shè)置畫布寬高,默認(rèn):300*150,最大:1365*1365bug: 避免設(shè)置過大的寬高,在安卓下會有crash的問題tip: iOS 暫不支持 pointer-eventstip: 在 mac 或 windows 小程序下,若當(dāng)前組件所在的頁面或全局開啟了enablePassiveEvent配置項,該內(nèi)置組件可能會出現(xiàn)非預(yù)期表現(xiàn)(詳情參考 enablePassiveEvent 文檔)tip: 鴻蒙 OS 下暫不支持外接紋理
# Canvas 2D 示例代碼
<!-- 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 示例代碼
<!-- 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)
})
}
})
# 示例代碼(舊的接口)
<!-- canvas.wxml -->
<canvas style="width: 300px; height: 200px;" canvas-id="firstCanvas"></canvas>
<!-- 當(dāng)使用絕對定位時,文檔流后邊的 canvas 的顯示層級高于前邊的 canvas -->
<canvas style="width: 400px; height: 500px;" canvas-id="secondCanvas"></canvas>
<!-- 因為 canvas-id 與前一個 canvas 重復(fù),該 canvas 不會顯示,并會發(fā)送一個錯誤事件到 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()
}
})