offscreencanvas.cancelanimationframe(number requestid)
基础库 或更高版本; 若版本较低,建议采取 兼容处理。
主体: 企业支付宝小程序 、 个人支付宝小程序
相关文档: canvas.requestanimationframe
取消一个由 offscreencanvas.requestanimationframe
注册的函数。
number requestid
offscreencanvas.requestanimationframe
返回的 id。
代码示例
.js
.axml
page({
onready() {
this.offcanvas = my.createoffscreencanvas()
this.offctx = this.offcanvas.getcontext('2d')
this.x = 0
this.y = 0
this.render()
this.reqid = -1
},
// 在离屏 canvas 内绘制内容
render() {
const { offcanvas, offctx, x, y } = this
offctx.clearrect(0, 0, offcanvas.width, offcanvas.height)
offctx.fillstyle = 'red'
offctx.fillrect(x, y, 50, 50)
this.x = ((x 0.5) % offcanvas.width)
this.y = ((y 0.5) % offcanvas.height)
this.reqid = offcanvas.requestanimationframe(this.render.bind(this))
},
oncanvasready() {
my.createselectorquery().select('#canvas').node().exec((res) => {
const canvas = res[0].node
const ctx = canvas.getcontext('2d')
setinterval(() => {
ctx.clearrect(0, 0, canvas.height, canvas.height)
ctx.drawimage(this.offcanvas, 0, 0)
// 按 60 fps 展示图像
if (this.x > 60) {
// 取消注册
this.offcanvas.cancelanimationframe(this.reqid)
}
}, 16)
})
},
})
<canvas id="canvas" type="2d" onready="oncanvasready">canvas>