[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / tools / perf / page_sets / tough_animation_cases / balls_javascript_canvas.html
blob0662839ce57e6e431bdeb840c536eb4a8c6c6cfc
1 <!--
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
8 met:
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
14 distribution.
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
33 -->
35 <!doctype html>
36 <head>
37 <title>Benchmark - Balls Animation using HTML5 Canvas</title>
38 <style>
39 html {
40 height: 100%;
43 body {
44 width: 100%;
45 height: 100%;
46 overflow: hidden;
47 margin: 0;
48 padding: 0;
51 span {
52 position: absolute;
53 width: 12px;
54 height: 12px;
55 border-radius: 6px;
57 </style>
58 <script src="resources/perf_test_helper.js"></script>
59 <script>
60 var measurementReady = false;
61 var stageWidth = 600;
62 var stageHeight = 600;
63 var maxParticles = 2500;
64 var minVelocity = 50;
65 var maxVelocity = 500;
66 var particleRadius = 6;
67 var colors = ["#cc0000", "#ffcc00", "#aaff00", "#0099cc", "#194c99", "#661999"];
69 var canvas;
70 var context;
71 var testRunning = true;
72 var particles = [];
73 var prevTime;
75 window.onload = function () {
76 // Prepare the canvas.
77 canvas = document.querySelector('canvas');
78 canvas.width = stageWidth;
79 canvas.height = stageHeight;
80 context = canvas.getContext('2d');
82 // Create the particles
83 for (var i = 0; i < maxParticles; i++)
84 particles.push(new Particle());
86 // Start the animation
87 prevTime = performance.now();
88 animate();
89 measurementReady = true;
92 function animate()
94 var currTime = performance.now();
95 var timeDelta = currTime - prevTime;
97 context.clearRect(0, 0, stageWidth, stageHeight);
99 // Draw each particle
100 for (var particle in particles)
101 particles[particle].draw(timeDelta);
103 prevTime = currTime;
105 if (testRunning)
106 requestAnimationFrame(animate);
109 function Particle()
111 var angle = Math.PI * 2 * PerfTestHelper.random();
112 var velocity = minVelocity + ((maxVelocity - minVelocity) * PerfTestHelper.random());
113 var x = stageWidth / 2 - particleRadius;
114 var y = stageHeight / 2 - particleRadius;
115 var dx = Math.cos(angle) * velocity;
116 var dy = Math.sin(angle) * velocity;
118 // Set colour of element
119 var color = colors[Math.floor(PerfTestHelper.random() * colors.length)];
121 function draw(timeDelta)
123 var timeDeltaSeconds = timeDelta / 1000;
124 var testX = x + (dx * timeDeltaSeconds);
125 var testY = y + (dy * timeDeltaSeconds);
127 if (testX < 0 || testX > stageWidth) {
128 dx = -dx;
129 x += dx;
130 } else {
131 x = testX;
133 if (testY < 0 || testY > stageHeight) {
134 dy = -dy;
135 y += dy;
136 } else {
137 y = testY;
140 context.fillStyle = color;
141 context.beginPath();
142 context.arc(x, y, particleRadius, 0, Math.PI * 2);
143 context.fill();
146 this.draw = draw;
148 </script>
149 </head>
150 <canvas></canvas>
151 </html>