blob: 9fc499b7c5cbcfbc1ba649d339bb1b1f6a2fec1a [file] [log] [blame]
Ravi Mistry6ac37952019-06-19 11:26:00 -04001<!DOCTYPE html>
2<html>
3<head>
Kevin Lubickdf4e3ab2020-06-19 11:11:03 -04004 <title>Lottie-Web 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/lottie.js" type="text/javascript" charset="utf-8"></script>
9 <style type="text/css" media="screen">
10 body {
11 margin: 0;
12 padding: 0;
13 }
14 </style>
Ravi Mistry6ac37952019-06-19 11:26:00 -040015</head>
16<body>
17 <main>
Ravi Mistryb2be4102019-07-08 18:48:12 +000018 <div style="width:1000px;height:1000px" class=anim></div>
Ravi Mistry6ac37952019-06-19 11:26:00 -040019 </main>
20 <script type="text/javascript" charset="utf-8">
21 (function () {
22 const PATH = '/res/lottie.json';
Ravi Mistryb2be4102019-07-08 18:48:12 +000023 const RENDERER = 'svg';
Ravi Mistry37a4bed2019-07-11 17:48:41 +000024 const MAX_FRAMES = 25;
Ravi Mistry3959dc22019-08-15 09:11:30 -040025 const MAX_LOOPS = 5;
Ravi Mistry6ac37952019-06-19 11:26:00 -040026
Ravi Mistryb2be4102019-07-08 18:48:12 +000027 // Get total number of frames of the animation from the hash.
28 const hash = window.location.hash;
29 const totalFrames = hash.slice(1);
30 console.log("Lottie has " + totalFrames + "total frames");
Ravi Mistry199ff682019-06-27 23:22:42 +000031
Ravi Mistryb2be4102019-07-08 18:48:12 +000032 // Load the animation with autoplay false. We will control which
33 // frame to seek to and then will measure performance.
34 let anim = lottie.loadAnimation({
Ravi Mistry6ac37952019-06-19 11:26:00 -040035 container: document.querySelector('.anim'),
36 renderer: RENDERER,
37 loop: false,
Ravi Mistryb2be4102019-07-08 18:48:12 +000038 autoplay: false,
Ravi Mistry6ac37952019-06-19 11:26:00 -040039 path: PATH,
40 rendererSettings: {
41 preserveAspectRatio:'xMidYMid meet'
42 },
43 });
44
Ravi Mistry37a4bed2019-07-11 17:48:41 +000045 const t_rate = 1.0 / (MAX_FRAMES - 1);
Ravi Mistryb2be4102019-07-08 18:48:12 +000046 let frame = 0;
Ravi Mistry37a4bed2019-07-11 17:48:41 +000047 let loop = 0;
Ravi Mistryb2be4102019-07-08 18:48:12 +000048 const drawFrame = () => {
Ravi Mistry37a4bed2019-07-11 17:48:41 +000049 if (frame >= MAX_FRAMES) {
50 // Reached the end of one loop.
51 loop++;
52 if (loop == MAX_LOOPS) {
53 // These are global variables to talk with puppeteer.
54 window._lottieWebDone = true;
55 return;
56 }
57 // Reset frame to restart the loop.
58 frame = 0;
Ravi Mistryb2be4102019-07-08 18:48:12 +000059 }
Ravi Mistry199ff682019-06-27 23:22:42 +000060
Ravi Mistryb2be4102019-07-08 18:48:12 +000061 let t1 = Math.max(Math.min(t_rate * frame, 1.0), 0.0);
62 let seekToFrame = totalFrames * t1;
63 if (seekToFrame >= totalFrames-1) {
64 // bodymovin player sometimes draws blank when requesting
65 // to draw the very last frame. Subtracting a small value
66 // seems to fix this and make it draw the last frame.
67 seekToFrame -= .001;
68 }
Ravi Mistry6ac37952019-06-19 11:26:00 -040069
Ravi Mistryb2be4102019-07-08 18:48:12 +000070 anim.goToAndStop(seekToFrame, true /* isFrame */);
Ravi Mistry37a4bed2019-07-11 17:48:41 +000071 console.log("Used seek: " + (seekToFrame/totalFrames));
Ravi Mistryb2be4102019-07-08 18:48:12 +000072 frame++;
Ravi Mistry6ac37952019-06-19 11:26:00 -040073 window.requestAnimationFrame(drawFrame);
Ravi Mistryb2be4102019-07-08 18:48:12 +000074 };
75 window.requestAnimationFrame(drawFrame);
Ravi Mistry6ac37952019-06-19 11:26:00 -040076 })();
77 </script>
78</body>
79</html>