renderingcontext offscreencanvas.getcontext(string contexttype)
主体: 企业支付宝小程序 、 个人支付宝小程序
offscreencanvas.getcontext 获取 offscreencanvas 的绘制上下文。
string contexttype
绘图上下文类型。
枚举值 | 描述 | 兼容性 |
---|---|---|
2d | 2d 类型上下文。 | - |
webgl | webgl 类型上下文。 | - |
renderingcontext
代码示例
// 创建离屏 canvas
const offcanvas = my.createoffscreencanvas();
const offctx = offcanvas.getcontext('2d');
// 用 离屏canvas 创建 image
const image = offcanvas.createimage();
image.src = 'https://img.alicdn.com/tfs/tb1gvvmj2bntkjjy0fdxxcppvxa-520-280.jpg';
image.onload = function() {
offcanvas.width = image.width;
offcanvas.height = image.height;
// 将 image 画到 离屏canvas 上
offctx.drawimage(image, 0, 0, offcanvas.width, offcanvas.height);
// 获取 离屏canvas 内像素信息
let data = offctx.getimagedata(0, 0, offcanvas.width, offcanvas.height);
console.log(data);
}