开发
API

OffscreenCanvas.getContext

基础库 1.78.0 开始支持本方法,低版本需做兼容处理。这是一个同步方法。

语法

OffscreenCanvas.getContext(string contextType)

参数说明

string contextType

canvas 类型,可取值 2dwebgl

返回值

RenderingContext

代码示例

<!-- index.html -->
<button type="primary" size="default" bindtap="createOffscreenCanvas">createOffscreenCanvas</button>
<canvas id="myCanvas" type="2d"></canvas>
let canvas, canvasCtx, animateId, offcanvas, offctx;
Page({
  onReady() {
    tt.createSelectorQuery()
      .select("#myCanvas")
      .node()
      .exec((res) => {
        canvas = res[0].node;
        canvasCtx = canvas.getContext("2d");
      });
  },
  createOffscreenCanvas() {
    offcanvas = tt.createOffscreenCanvas();
    offctx = offcanvas.getContext("2d");

    offcanvas.width = 100;
    offcanvas.height = 100;
    offctx.rect(0, 0, 100, 100);
    offctx.fillStyle = "red";
    offctx.fill();

    canvasCtx.drawImage(offcanvas, 0, 0);
  },
});

Bug & Tip

  • Tip:开发者工具暂不支持此能力,请用真机扫码调试。
点击纠错
评价此篇文档