canvas
功能描述
画布。2.9.0 起支持一套新 Canvas 2D 接口(需指定 type 属性),同时支持同层渲染,原有接口不再维护。旧版本可参考 旧版画布迁移指南 进行迁移。
属性说明
| 属性 | 类型 | 默认值 | 必填 | 说明 | 最低版本 |
|---|---|---|---|---|---|
| type | string | 否 | 指定 canvas 类型,支持 2d (2.9.0) 和 webgl (2.7.0) | ||
| canvas-id | string | 否 | canvas 组件的唯一标识符,若指定了 type 则无需再指定该属性 | ||
| disable-scroll | boolean | false | 否 | 当在 canvas 中移动时且有绑定手势事件时,禁止屏幕滚动以及下拉刷新 | |
| bindtouchstart | eventhandle | 否 | 手指触摸动作开始 | ||
| bindtouchmove | eventhandle | 否 | 手指触摸后移动 | ||
| bindtouchend | eventhandle | 否 | 手指触摸动作结束 | ||
| bindtouchcancel | eventhandle | 否 | 手指触摸动作被打断,如来电提醒,弹窗 | ||
| bindlongtap | eventhandle | 否 | 手指长按 500ms 之后触发,触发了长按事件后进行移动不会触发屏幕的滚动 | ||
| binderror | eventhandle | 否 | 当发生错误时触发 error 事件,detail = {errMsg} |
Bug & Tip
tip:canvas 标签默认宽度 300px、高度 150pxtip:同一页面中的 canvas-id 不可重复,如果使用一个已经出现过的 canvas-id,该 canvas 标签对应的画布将被隐藏并不再正常工作tip:请注意原生组件使用限制tip:开发者工具中默认关闭了 GPU 硬件加速,可在开发者工具的设置中开启“硬件加速”提高 WebGL 的渲染性能tip: WebGL 支持通过 getContext('webgl', { alpha: true }) 获取透明背景的画布tip: WebGL 暂不支持真机调试,建议使用真机预览tip: Canvas 2D(新接口)需要显式设置画布宽高,默认:300*150,最大:1365*1365bug: 避免设置过大的宽高,在安卓下会有 crash 的问题tip: iOS 暂不支持 pointer-eventstip: 在 mac 或 windows 小程序下,若当前组件所在的页面或全局开启了enablePassiveEvent配置项,该内置组件可能会出现非预期表现(详情参考 enablePassiveEvent 文档)tip: 鸿蒙 OS 下暂不支持外接纹理
Canvas 2D 示例代码
<!-- canvas.blml -->
<canvas type="2d" id="myCanvas"></canvas>
// canvas.js
Page({
onReady() {
const query = bl.createSelectorQuery();
query
.select("#myCanvas")
.fields({ node: true, size: true })
.exec((res) => {
const canvas = res[0].node;
const ctx = canvas.getContext("2d");
const dpr = bl.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.blml -->
<canvas type="webgl" id="myCanvas"></canvas>
// canvas.js
Page({
onReady() {
const query = bl.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.blml -->
<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) {
// 使用 bl.createContext 获取绘图上下文 context
var context = bl.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();
},
});