From c4a9ee8497a880b0e446e7c2fa11994697947dec Mon Sep 17 00:00:00 2001 From: Matchu Date: Thu, 24 Sep 2020 06:04:19 -0700 Subject: [PATCH] a bit more perf for static assets --- src/app/components/OutfitCanvas.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/app/components/OutfitCanvas.js b/src/app/components/OutfitCanvas.js index 775109b..514e716 100644 --- a/src/app/components/OutfitCanvas.js +++ b/src/app/components/OutfitCanvas.js @@ -257,15 +257,29 @@ export function OutfitCanvasMovie({ librarySrc, zIndex }) { return; } movieClip = new LibraryMovieClipConstructor(); + + // For actual animated movies, we cache their appearance then update + // every time they advance a frame, so that they aren't recomputing + // things while we perform 60FPS fade transitions. + // + // For static assets, we go even further: we cache their appearance once, + // then never touch it again, even disabling the entire tick event for + // its entire remaining lifetime! (This is a surprisingly good perf win: + // static assets are often complex with a big sprite tree, and not having + // to walk it has a measurable impact on simulated low-power CPUs.) movieClip.cache( 0, 0, library.properties.width, library.properties.height ); - movieClip.on("tick", () => { - movieClip.updateCache(); - }); + if (createjsNodeHasAnimations(movieClip)) { + movieClip.on("tick", () => { + movieClip.updateCache(); + }); + } else { + movieClip.tickEnabled = false; + } // We're gonna fade in! Wait for the first frame to draw, to make the // timing smooth, but yeah here we go! @@ -449,4 +463,11 @@ function loadScriptTag(src) { }); } +function createjsNodeHasAnimations(createjsNode) { + return ( + createjsNode.totalFrames > 1 || + (createjsNode.children || []).some(createjsNodeHasAnimations) + ); +} + export default OutfitCanvas;