2 Copyright (c) 2012 Cameron Adams. All rights reserved.
3 Copyright (c) 2012 Code Aurora Forum. All rights reserved.
4 Copyright (C) 2013 Google Inc. All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following disclaimer
13 in the documentation and/or other materials provided with the
15 * Neither the name of Code Aurora Forum Inc., Google Inc. nor the
16 names of its contributors may be used to endorse or promote products
17 derived from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 This test is based on code written by Cameron Adams and imported from
32 http://themaninblue.com/experiment/AnimationBenchmark/html
37 <title>Benchmark - Balls Animation using CSS Animations</title>
58 <script src="../resources/runner.js"></script>
59 <script src="resources/framerate.js"></script>
62 var stageHeight = 600;
63 var maxParticles = 2500;
65 var maxVelocity = 500;
66 var particleRadius = 6;
67 var colors = ["#cc0000", "#ffcc00", "#aaff00", "#0099cc", "#194c99", "#661999"];
68 var animationDuration = 10;
71 var allParticleKeyframes = [];
73 window.onload = function () {
74 PerfTestRunner.prepareToMeasureValuesAsync({
75 description: "Measure performance of animating 'left' and 'top' by calling Element.animate() (from the Web Animations API), pausing the player, and updating the player's currentTime using requestAnimationFrame.",
80 // Create the particles
81 for (var i = 0; i < maxParticles; i++) {
82 generateParticleKeyframes(i);
83 var particle = new Particle(i);
85 particles.push(particle);
88 startTrackingFrameRate();
91 function generateParticleKeyframes(index) {
92 allParticleKeyframes[index] = [];
94 var angle = Math.PI * 2 * PerfTestRunner.random();
95 var velocity = minVelocity + ((maxVelocity - minVelocity) * PerfTestRunner.random());
96 var x = stageWidth / 2 - particleRadius;
97 var y = stageHeight / 2 - particleRadius;
98 var dx = Math.cos(angle) * velocity;
99 var dy = Math.sin(angle) * velocity;
101 function detectCollision(lineX, normalX, lineY, normalY) {
104 if (dx * normalX < 0)
105 dtx = (lineX - x) / dx;
106 if (dy * normalY < 0)
107 dty = (lineY - y) / dy;
108 var dt = Math.min(dtx, dty);
109 var hitX = (dtx < dty);
112 x: hitX ? lineX : x + (dx * dt),
113 y: hitX ? y + (dy * dt) : lineY,
120 allParticleKeyframes[index].push(generateKeyframe(0, x, y));
121 while (t < animationDuration) {
122 var collisionA = detectCollision(0, 1, 0, 1);
123 var collisionB = detectCollision(stageWidth, -1, stageHeight, -1);
124 var collision = collisionA.dt < collisionB.dt ? collisionA : collisionB;
125 if (t + collision.dt > animationDuration) {
126 var dt = animationDuration - t;
127 t = animationDuration;
137 allParticleKeyframes[index].push(generateKeyframe(t, x, y));
141 function generateKeyframe(t, x, y) {
142 return {left: x + "px", top: y + "px", offset: t / animationDuration };
145 function Particle(index)
147 // Create visual element for the particle.
148 var domNode = document.createElement('span');
149 document.body.appendChild(domNode);
151 // Set colour of element.
152 domNode.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
155 var player = domNode.animate(allParticleKeyframes[index], {easing: "linear", iterations: Infinity, direction: "alternate", duration: animationDuration * 1000});
157 this.player = player;
162 document.body.removeChild(domNode);
166 this.destroy = destroy;
171 function advance(timestamp)
175 for (var x in particles) {
176 particles[x].player.currentTime = timestamp - start;
178 if (particles.length !== 0)
179 requestAnimationFrame(advance);
182 requestAnimationFrame(advance);
184 function onCompletedRun() {
186 stopTrackingFrameRate();
188 for (var i = 0; i < particles.length; i++)
189 particles[i].destroy();
193 <style id="keyframes"></style>