offscreencanvas.createimage()
小程序开发者工具: 不支持。 ide 模拟器暂不支持调试,请以真机调试结果为准。
主体: 企业支付宝小程序 、 个人支付宝小程序
.js
.axml
page({
oncanvasready() {
const self = this;
my.createselectorquery().select('#canvas').node().exec((res) => {
const canvas = res[0].node;
const ctx = canvas.getcontext('2d');
self.canvas = canvas;
self.ctx = ctx;
self.draw();
});
},
draw() {
const ctx = this.ctx;
// 创建离屏 canvas
const offcanvas = my.createoffscreencanvas();
const offctx = offcanvas.getcontext('2d');
// 用 离屏canvas 创建 image
const image = offcanvas.createimage();
image.onload = function() {
offcanvas.width = image.width;
offcanvas.height = image.height;
// 将 image 画到 离屏canvas 上
offctx.drawimage(image, 0, 0, offcanvas.width, offcanvas.height);
// 将 离屏canvas 中的部分内容画到 canvas 上
ctx.drawimage(offcanvas, 50, 50, 100, 100, 0, 0, 100, 100);
}
image.src = 'https://img.alicdn.com/tfs/tb1gvvmj2bntkjjy0fdxxcppvxa-520-280.jpg';
},
});
<canvas id="canvas" type="2d" onready="oncanvasready"></canvas>