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 Transitions</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"];
69 var testRunning = true;
72 window.onload = function () {
73 PerfTestRunner.prepareToMeasureValuesAsync({
74 description: "Measure performance of CSS Transitions animating 'left' and 'top' with transition-property: 'all'.",
79 // Create the particles
80 for (var i = 0; i < maxParticles; i++) {
81 var particle = new Particle();
83 particles.push(particle);
86 startTrackingFrameRate();
91 var angle = Math.PI * 2 * PerfTestRunner.random();
92 var velocity = minVelocity + ((maxVelocity - minVelocity) * PerfTestRunner.random());
93 var x = stageWidth / 2 - particleRadius;
94 var y = stageHeight / 2 - particleRadius;
95 var dx = Math.cos(angle) * velocity;
96 var dy = Math.sin(angle) * velocity;
98 // Create visual element for the particle
99 var domNode = document.createElement('span');
100 document.body.appendChild(domNode);
102 // Set initial position to middle of screen
103 domNode.style.left = x + "px";
104 domNode.style.top = y + "px";
106 // Set colour of element
107 domNode.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
109 domNode.style.webkitTransitionProperty = 'all';
110 domNode.style.webkitTransitionTimingFunction = 'linear';
114 document.body.removeChild(domNode);
117 function detectCollision(lineX, normalX, lineY, normalY) {
120 if (dx * normalX < 0)
121 dtx = (lineX - x) / dx;
122 if (dy * normalY < 0)
123 dty = (lineY - y) / dy;
124 var dt = Math.min(dtx, dty);
125 var hitX = (dtx < dty);
128 x: hitX ? lineX : x + (dx * dt),
129 y: hitX ? y + (dy * dt) : lineY,
138 var collisionA = detectCollision(0, 1, 0, 1);
139 var collisionB = detectCollision(stageWidth, -1, stageHeight, -1);
140 var collision = collisionA.dt < collisionB.dt ? collisionA : collisionB;
141 domNode.style.webkitTransitionDuration = collision.dt + 's, ' + collision.dt + 's';
142 domNode.style.left = collision.x + 'px';
143 domNode.style.top = collision.y + 'px';
148 setTimeout(move, collision.dt * 1000);
152 // Set timeout is used to ensure the transition-property value gets a chance to register on the element.
157 this.destroy = destroy;
160 function onCompletedRun() {
161 stopTrackingFrameRate();
163 for (var i = 0; i < particles.length; i++)
164 particles[i].destroy();