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 and requestAnimationFrame
</title>
58 <script src=
"resources/perf_test_helper.js"></script>
60 var measurementReady
= false;
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;
73 window
.onload = function () {
74 // Create the particles
75 for (var i
= 0; i
< maxParticles
; i
++)
76 particles
.push(new Particle());
78 // Start the animation
79 prevTime
= performance
.now();
81 measurementReady
= true;
86 var currTime
= performance
.now();
87 var timeDelta
= currTime
- prevTime
;
90 for (var particle
in particles
)
91 particles
[particle
].draw(timeDelta
);
96 requestAnimationFrame(animate
);
101 var angle
= Math
.PI
* 2 * PerfTestHelper
.random();
102 var velocity
= minVelocity
+ ((maxVelocity
- minVelocity
) * PerfTestHelper
.random());
103 var x
= stageWidth
/ 2 - particleRadius
;
104 var y
= stageHeight
/ 2 - particleRadius
;
105 var dx
= Math
.cos(angle
) * velocity
;
106 var dy
= Math
.sin(angle
) * velocity
;
108 // Create visual element for the particle
109 var domNode
= document
.createElement('span');
110 document
.body
.appendChild(domNode
);
112 // Set initial position to middle of screen
113 domNode
.style
.left
= x
+ "px";
114 domNode
.style
.top
= y
+ "px";
116 // Set colour of element
117 domNode
.style
.backgroundColor
= colors
[Math
.floor(PerfTestHelper
.random() * colors
.length
)];
121 document
.body
.removeChild(domNode
);
124 function draw(timeDelta
)
126 var timeDeltaSeconds
= timeDelta
/ 1000;
127 var testX
= x
+ (dx
* timeDeltaSeconds
);
128 var testY
= y
+ (dy
* timeDeltaSeconds
);
130 if (testX
< 0 || testX
> stageWidth
) {
136 if (testY
< 0 || testY
> stageHeight
) {
143 domNode
.style
.left
= x
+ "px";
144 domNode
.style
.top
= y
+ "px";
148 this.destroy
= destroy
;