4 <title>Skottie-WASM Perf</title>
5 <meta charset="utf-8" />
6 <meta http-equiv="X-UA-Compatible" content="IE=edge">
7 <meta name="viewport" content="width=device-width, initial-scale=1.0">
8 <script src="res/canvaskit.js" type="text/javascript" charset="utf-8"></script>
9 <style type="text/css" media="screen">
18 <canvas id=anim width=1000 height=1000 style="height: 1000px; width: 1000px;"></canvas>
20 <script type="text/javascript" charset="utf-8">
23 const LOTTIE_JSON_PATH = '/res/lottie.json';
24 const MAX_FRAMES = 25;
26 const MAX_SAMPLE_MS = 60*1000; // in case something takes a while, stop after 60 seconds.
28 const loadKit = CanvasKitInit({
29 locateFile: (file) => '/res/' + file,
32 const loadLottie = fetch(LOTTIE_JSON_PATH).then((resp) => {
36 Promise.all([loadKit, loadLottie]).then((values) => {
37 const [CanvasKit, json] = values;
39 const animation = CanvasKit.MakeManagedAnimation(json, null);
41 window._error = 'Could not process JSON';
46 if (window.location.hash.indexOf('gpu') !== -1) {
47 surface = CanvasKit.MakeWebGLCanvasSurface('anim');
49 window._error = 'Could not make GPU surface';
52 let c = document.getElementById('anim');
53 // If CanvasKit was unable to instantiate a WebGL context, it will fallback
54 // to CPU and add a ck-replaced class to the canvas element.
55 if (c.classList.contains('ck-replaced')) {
56 window._error = 'fell back to CPU';
60 surface = CanvasKit.MakeSWCanvasSurface('anim');
62 window._error = 'Could not make CPU surface';
66 const canvas = surface.getCanvas();
68 const t_rate = 1.0 / (MAX_FRAMES-1);
72 const damageRect = Float32Array.of(0, 0, 0, 0);
73 const bounds = CanvasKit.LTRBRect(0, 0, WIDTH, HEIGHT);
74 const start = performance.now();
76 const drawFrame = () => {
77 if ((performance.now() - start) > MAX_SAMPLE_MS) {
78 // This global variable signals we are done.
79 window._skottieDone = true;
82 if (frame >= MAX_FRAMES) {
83 // Reached the end of one loop.
85 if (loop == MAX_LOOPS) {
86 // This global variable signals we are done.
87 window._skottieDone = true;
90 // Reset frame and seek to restart the loop.
95 let damage = animation.seek(seek, damageRect);
96 if (damage[2] > damage[0] && damage[3] > damage[1]) {
97 animation.render(canvas, bounds);
100 console.log(`Used seek: ${seek}`);
103 window.requestAnimationFrame(drawFrame);
105 window.requestAnimationFrame(drawFrame);