• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!--
2Copyright (c) 2012 Cameron Adams. All rights reserved.
3Copyright (c) 2012 Code Aurora Forum. All rights reserved.
4Copyright (C) 2013 Google Inc. All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9    * Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11    * Redistributions in binary form must reproduce the above
12copyright notice, this list of conditions and the following disclaimer
13in the documentation and/or other materials provided with the
14distribution.
15    * Neither the name of Code Aurora Forum Inc., Google Inc. nor the
16names of its contributors may be used to endorse or promote products
17derived from this software without specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31This test is based on code written by Cameron Adams and imported from
32  http://themaninblue.com/experiment/AnimationBenchmark/html
33-->
34
35<!doctype html>
36<head>
37<title>Benchmark - Balls Animation using SVG Animations</title>
38<style>
39html {
40    height: 100%;
41}
42
43body {
44    width: 100%;
45    height: 100%;
46    overflow: hidden;
47    margin: 0;
48    padding: 0;
49}
50
51span {
52    position: absolute;
53    width: 12px;
54    height: 12px;
55    border-radius: 6px;
56}
57</style>
58<script src="../resources/runner.js"></script>
59<script src="resources/framerate.js"></script>
60<script>
61var stageWidth = 600;
62var stageHeight = 600;
63var maxParticles = 2500;
64var minVelocity = 50;
65var maxVelocity = 500;
66var particleRadius = 6;
67var colors = ["#cc0000", "#ffcc00", "#aaff00", "#0099cc", "#194c99", "#661999"];
68var animationDuration = 10;
69
70var particles = [];
71
72window.onload = function () {
73    svgContainer.setAttribute('width', stageWidth);
74    svgContainer.setAttribute('height', stageHeight);
75
76    for (var i = 0; i < maxParticles; i++)
77        addParticle();
78
79    PerfTestRunner.prepareToMeasureValuesAsync({done: onCompletedRun, unit: 'fps'});
80
81    startTrackingFrameRate();
82}
83
84function addParticle()
85{
86    var circle = document.createElementNS('http://www.w3.org/2000/svg','circle');
87    circle.setAttribute('r', particleRadius);
88    circle.setAttribute('fill', colors[Math.floor(Math.random() * colors.length)]);
89    addAnimation(circle);
90    svgContainer.appendChild(circle);
91}
92
93function addAnimation(target) {
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;
100    var prevX = x;
101    var prevY = y;
102
103    function detectCollision(lineX, normalX, lineY, normalY) {
104        var dtx = Infinity;
105        var dty = Infinity;
106        if (dx * normalX < 0)
107            dtx = (lineX - x) / dx;
108        if (dy * normalY < 0)
109            dty = (lineY - y) / dy;
110        var dt = Math.min(dtx, dty);
111        var hitX = (dtx < dty);
112        return {
113            dt: dt,
114            x: hitX ? lineX : x + (dx * dt),
115            y: hitX ? y + (dy * dt) : lineY,
116            dx: hitX ? -dx : dx,
117            dy: hitX ? dy : -dy,
118        };
119    }
120
121    var t = 0;
122    var prevT = t;
123    while (t < animationDuration) {
124        var collisionA = detectCollision(0, 1, 0, 1);
125        var collisionB = detectCollision(stageWidth, -1, stageHeight, -1);
126        var collision = collisionA.dt < collisionB.dt ? collisionA : collisionB;
127        if (t + collision.dt > animationDuration) {
128            var dt = animationDuration - t;
129            t = animationDuration;
130            x += dx * dt;
131            y += dy * dt;
132        } else {
133            t += collision.dt;
134            x = collision.x;
135            y = collision.y;
136            dx = collision.dx;
137            dy = collision.dy;
138        }
139        target.appendChild(generateTransition(prevT, prevX, prevY, t, x, y));
140        prevX = x;
141        prevY = y;
142        prevT = t;
143    }
144    if (target.lastChild) {
145        target.lastChild.setAttribute('repeatCount', '1000');
146    }
147}
148
149function generateTransition(prevT, prevX, prevY, t, x, y) {
150    var animation = document.createElementNS('http://www.w3.org/2000/svg','animateTransform');
151    animation.setAttribute('attributeName', 'transform');
152    animation.setAttribute('attributeType', 'xml');
153    animation.setAttribute('type', 'translate' );
154    animation.setAttribute('from', prevX + ' ' + prevY);
155    animation.setAttribute('to', x + ' ' + y);
156    animation.setAttribute('begin', prevT + 's');
157    animation.setAttribute('dur', (t - prevT) + 's');
158    return animation;
159}
160
161
162function onCompletedRun() {
163    stopTrackingFrameRate();
164    svgContainer.remove();
165}
166</script>
167</head>
168<svg ns="http://www.w3.org/2000/svg" version="1.1" id="svgContainer"></svg>
169</html>
170