Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / PerformanceTests / Animation / balls-svg.html
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.
5
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.
18
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.
30
31 This 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>
39 html {
40     height: 100%;
41 }
42
43 body {
44     width: 100%;
45     height: 100%;
46     overflow: hidden;
47     margin: 0;
48     padding: 0;
49 }
50
51 span {
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>
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"];
68 var animationDuration = 10;
69
70 var particles = [];
71
72 window.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({
80         description: 'Measures performance of SVG animation using animateTransform (animating on x and y).',
81         done: onCompletedRun,
82         unit: 'fps'
83     });
84
85     startTrackingFrameRate();
86 }
87
88 function addParticle()
89 {
90     var circle = document.createElementNS('http://www.w3.org/2000/svg','circle');
91     circle.setAttribute('r', particleRadius);
92     circle.setAttribute('fill', colors[Math.floor(Math.random() * colors.length)]);
93     addAnimation(circle);
94     svgContainer.appendChild(circle);
95 }
96
97 function addAnimation(target) {
98     var angle = Math.PI * 2 * PerfTestRunner.random();
99     var velocity = minVelocity + ((maxVelocity - minVelocity) * PerfTestRunner.random());
100     var x = stageWidth / 2 - particleRadius;
101     var y = stageHeight / 2 - particleRadius;
102     var dx = Math.cos(angle) * velocity;
103     var dy = Math.sin(angle) * velocity;
104     var prevX = x;
105     var prevY = y;
106
107     function detectCollision(lineX, normalX, lineY, normalY) {
108         var dtx = Infinity;
109         var dty = Infinity;
110         if (dx * normalX < 0)
111             dtx = (lineX - x) / dx;
112         if (dy * normalY < 0)
113             dty = (lineY - y) / dy;
114         var dt = Math.min(dtx, dty);
115         var hitX = (dtx < dty);
116         return {
117             dt: dt,
118             x: hitX ? lineX : x + (dx * dt),
119             y: hitX ? y + (dy * dt) : lineY,
120             dx: hitX ? -dx : dx,
121             dy: hitX ? dy : -dy,
122         };
123     }
124
125     var t = 0;
126     var prevT = t;
127     while (t < animationDuration) {
128         var collisionA = detectCollision(0, 1, 0, 1);
129         var collisionB = detectCollision(stageWidth, -1, stageHeight, -1);
130         var collision = collisionA.dt < collisionB.dt ? collisionA : collisionB;
131         if (t + collision.dt > animationDuration) {
132             var dt = animationDuration - t;
133             t = animationDuration;
134             x += dx * dt;
135             y += dy * dt;
136         } else {
137             t += collision.dt;
138             x = collision.x;
139             y = collision.y;
140             dx = collision.dx;
141             dy = collision.dy;
142         }
143         target.appendChild(generateTransition(prevT, prevX, prevY, t, x, y));
144         prevX = x;
145         prevY = y;
146         prevT = t;
147     }
148     if (target.lastChild) {
149         target.lastChild.setAttribute('repeatCount', '1000');
150     }
151 }
152
153 function generateTransition(prevT, prevX, prevY, t, x, y) {
154     var animation = document.createElementNS('http://www.w3.org/2000/svg','animateTransform');
155     animation.setAttribute('attributeName', 'transform');
156     animation.setAttribute('attributeType', 'xml');
157     animation.setAttribute('type', 'translate' );
158     animation.setAttribute('from', prevX + ' ' + prevY);
159     animation.setAttribute('to', x + ' ' + y);
160     animation.setAttribute('begin', prevT + 's');
161     animation.setAttribute('dur', (t - prevT) + 's');
162     return animation;
163 }
164
165
166 function onCompletedRun() {
167     stopTrackingFrameRate();
168     svgContainer.remove();
169 }
170 </script>
171 </head>
172 <svg ns="http://www.w3.org/2000/svg" version="1.1" id="svgContainer"></svg>
173 </html>