Unreviewed, fix build failure
[WebKit-https.git] / PerformanceTests / Animation / svg-animation.html
1 <!DOCTYPE html>
2
3 <html>
4 <head>
5     <meta name="viewport" content="initial-scale=1.0">
6     <style>
7         body {
8             margin: 0;
9         }
10     </style>
11     <script>
12
13         var animationDuration = 2;
14
15         function randomInt(min, max)
16         {
17             return Math.round(this.random(min, max));
18         }
19
20         function random(min, max)
21         {
22             return (Math.random() * (max - min)) + min;
23         }
24
25         function randomColor()
26         {
27             var min = 32;
28             var max = 256 - 32;
29             return "#"
30                 + this.randomInt(min, max).toString(16)
31                 + this.randomInt(min, max).toString(16)
32                 + this.randomInt(min, max).toString(16);
33         }
34         
35         function Point(x, y)
36         {
37             this.x = x;
38             this.y = y;
39         }
40
41         Point.pointOnCircle = function(angle, radius)
42         {
43             return new Point(radius * Math.cos(angle), radius * Math.sin(angle));
44         }
45
46         Point.prototype =
47         {
48             add: function(other)
49             {
50                 if(isNaN(other.x))
51                     return new Point(this.x + other, this.y + other);
52                 return new Point(this.x + other.x, this.y + other.y);
53             },
54
55             subtract: function(other)
56             {
57                 if(isNaN(other.x))
58                     return new Point(this.x - other, this.y - other);
59                 return new Point(this.x - other.x, this.y - other.y);
60             },
61
62             move: function(angle, velocity, timeDelta)
63             {
64                 return this.add(Point.pointOnCircle(angle, velocity * (timeDelta / 1000)));
65             },
66
67             multiply: function(other)
68             {
69                 if(isNaN(other.x))
70                     return new Point(this.x * other, this.y * other);
71                 return new Point(this.x * other.x, this.y * other.y);
72             },
73
74             length: function() {
75                 return Math.sqrt( this.x * this.x + this.y * this.y );
76             },
77
78             normalize: function() {
79                 var l = Math.sqrt( this.x * this.x + this.y * this.y );
80                 this.x /= l;
81                 this.y /= l;
82                 return this;
83             }
84         }
85         
86         function createAnimation(attribute, delay, duration)
87         {
88             var animation = document.createElementNS("http://www.w3.org/2000/svg", "animate");
89             animation.setAttribute('attributeName', attribute);
90             animation.setAttribute('values', '0; 300; 0');
91             animation.setAttribute('keyTimes', '0; 0.5; 1');
92             animation.setAttribute('begin', delay + 's');
93             animation.setAttribute('dur', duration + 's');
94             animation.setAttribute('fill', 'remove');
95             animation.setAttribute('repeatCount', 'indefinite');
96             return animation;
97         }
98
99         function Particle(maxPosition)
100         {
101             this.element = document.createElementNS("http://www.w3.org/2000/svg", "circle");
102             this.element.setAttribute('cx', '0');
103             this.element.setAttribute('cy', '0');
104             this.element.setAttribute('r', '10');
105             this.element.setAttribute('style', 'fill: ' + randomColor());
106             this.element.className = 'particle';
107
108             var slop = 0.2;
109             var delay = '-' + random(0, animationDuration);
110             var xDuration = random(animationDuration - slop, animationDuration + slop);
111             var yDuration = random(animationDuration - slop, animationDuration + slop);
112
113             this.element.appendChild(createAnimation('cx', delay, xDuration));
114             this.element.appendChild(createAnimation('cy', delay, yDuration));
115
116             this.maxPosition = maxPosition;
117             this.reset();
118             this.move();
119         }
120
121         Particle.prototype =
122         {
123             reset: function()
124             {
125                 this.size = new Point(20, 20);
126                 this.maxLocation = this.maxPosition.subtract(this.size);
127                 this.position = new Point(0, 0);
128             },
129
130             move: function()
131             {
132                 this.element.setAttribute('cx', this.position.x);
133                 this.element.setAttribute('cy', this.position.y);
134             }
135         }
136
137         var numParticles = 20;
138         var particles = [];
139
140         function makeParticles()
141         {
142             var stage = document.getElementById('stage');
143             var maxPosition = new Point(320, 320);
144             for (var i = 0; i < numParticles; ++i) {
145                 particles.push(new Particle(maxPosition));
146                 stage.appendChild(particles[i].element);
147             }
148         }
149                 
150         function setupAnimation()
151         {
152             makeParticles();
153         }
154         
155         window.addEventListener('load', setupAnimation, false);
156     </script>
157 </head>
158 <body>
159
160     <svg width="320" height="320"
161          viewBox="0 0 320 320"
162          xmlns="http://www.w3.org/2000/svg">
163         <g id="stage">
164             <rect x="0" y="0" width="320" height="320" style="stroke: black; fill: none;"/>
165         </g>
166     </svg>
167
168 </body>
169 </html>