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