fix: 优化 save 的调用次数

This commit is contained in:
unanmed 2024-11-06 21:06:59 +08:00
parent c79bcaeaa2
commit 724eabd49a
3 changed files with 0 additions and 7 deletions

View File

@ -269,9 +269,7 @@ export abstract class RenderItem<E extends ERenderItemEvent = ERenderItemEvent>
if (this.cacheDirty) { if (this.cacheDirty) {
const { canvas } = this.cache; const { canvas } = this.cache;
ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.save();
this.render(this.cache, tran); this.render(this.cache, tran);
ctx.restore();
this.cacheDirty = false; this.cacheDirty = false;
} }

View File

@ -103,18 +103,15 @@ export class Animate extends Sprite {
super('absolute', false, true); super('absolute', false, true);
this.setRenderFn((canvas, transform) => { this.setRenderFn((canvas, transform) => {
const { ctx } = canvas;
if ( if (
this.absoluteAnimates.size === 0 && this.absoluteAnimates.size === 0 &&
this.staticAnimates.size === 0 this.staticAnimates.size === 0
) { ) {
return; return;
} }
ctx.save();
this.drawAnimates(this.absoluteAnimates, canvas); this.drawAnimates(this.absoluteAnimates, canvas);
transformCanvas(canvas, transform); transformCanvas(canvas, transform);
this.drawAnimates(this.staticAnimates, canvas); this.drawAnimates(this.staticAnimates, canvas);
ctx.restore();
}); });
this.delegation = this.delegateTicker(time => { this.delegation = this.delegateTicker(time => {

View File

@ -33,9 +33,7 @@ export class Sprite<
canvas: MotaOffscreenCanvas2D, canvas: MotaOffscreenCanvas2D,
transform: Transform transform: Transform
): void { ): void {
canvas.ctx.save();
this.renderFn(canvas, transform); this.renderFn(canvas, transform);
canvas.ctx.restore();
} }
setRenderFn(fn: RenderFunction) { setRenderFn(fn: RenderFunction) {