OLD | NEW |
| (Empty) |
1 <!-- | |
2 Copyright 2013 Google Inc. All Rights Reserved. | |
3 | |
4 Licensed under the Apache License, Version 2.0 (the "License"); | |
5 you may not use this file except in compliance with the License. | |
6 You may obtain a copy of the License at | |
7 | |
8 http://www.apache.org/licenses/LICENSE-2.0 | |
9 | |
10 Unless required by applicable law or agreed to in writing, software | |
11 distributed under the License is distributed on an "AS IS" BASIS, | |
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 See the License for the specific language governing permissions and | |
14 limitations under the License. | |
15 | |
16 This test is based on code written by Cameron Adams and imported from | |
17 http://themaninblue.com/experiment/AnimationBenchmark/html | |
18 --> | |
19 <!doctype html> | |
20 <head> | |
21 <style> | |
22 .ball { | |
23 position: absolute; | |
24 width: 12px; | |
25 height: 12px; | |
26 border-radius: 100%; | |
27 } | |
28 </style> | |
29 </head> | |
30 <script src="perf.js"></script> | |
31 <script src="../../web-animations.js"></script> | |
32 <script> | |
33 'use strict'; | |
34 | |
35 var stageWidth = 600; | |
36 var stageHeight = 600; | |
37 var particleCount = 2500; | |
38 var minVelocity = 50; | |
39 var maxVelocity = 500; | |
40 var particleRadius = 6; | |
41 var colors = ['#cc0000', '#ffcc00', '#aaff00', '#0099cc', '#194c99', '#661999']; | |
42 var animationDuration = 10; | |
43 | |
44 var particles = []; | |
45 var player; | |
46 | |
47 var Particle = function() { | |
48 this.element = document.createElement('div'); | |
49 this.element.className = 'ball'; | |
50 this.element.style.backgroundColor = colors[Math.floor(Perf.random() * colors.
length)]; | |
51 this.element.style.left = (stageWidth / 2 - particleRadius) + 'px'; | |
52 this.element.style.top = (stageHeight / 2 - particleRadius) + 'px'; | |
53 document.body.appendChild(this.element); | |
54 }; | |
55 | |
56 Particle.prototype.generateAnimation = function(duration) { | |
57 var keyframes = []; | |
58 | |
59 var angle = Math.PI * 2 * Perf.random(); | |
60 var velocity = minVelocity + ((maxVelocity - minVelocity) * Perf.random()); | |
61 var x = 0; | |
62 var y = 0; | |
63 var dx = Math.cos(angle) * velocity; | |
64 var dy = Math.sin(angle) * velocity; | |
65 | |
66 var nextCollision = function(lineX, normalX, lineY, normalY) { | |
67 var dtx = Infinity; | |
68 var dty = Infinity; | |
69 if (dx * normalX < 0) | |
70 dtx = (lineX - x) / dx; | |
71 if (dy * normalY < 0) | |
72 dty = (lineY - y) / dy; | |
73 var dt = Math.min(dtx, dty); | |
74 var hitX = (dtx < dty); | |
75 return { | |
76 dt: dt, | |
77 x: hitX ? lineX : x + (dx * dt), | |
78 y: hitX ? y + (dy * dt) : lineY, | |
79 dx: hitX ? -dx : dx, | |
80 dy: hitX ? dy : -dy, | |
81 }; | |
82 }; | |
83 | |
84 var t = 0; | |
85 keyframes.push(this.createKeyframe(0, x, y)); | |
86 while (t < duration) { | |
87 var collisionA = nextCollision(-stageWidth / 2, 1, -stageHeight / 2, 1); | |
88 var collisionB = nextCollision(stageWidth / 2, -1, stageHeight / 2, -1); | |
89 var collision = collisionA.dt < collisionB.dt ? collisionA : collisionB; | |
90 if (t + collision.dt > duration) { | |
91 var dt = duration - t; | |
92 t = duration; | |
93 x += dx * dt; | |
94 y += dy * dt; | |
95 } else { | |
96 t += collision.dt; | |
97 x = collision.x; | |
98 y = collision.y; | |
99 dx = collision.dx; | |
100 dy = collision.dy; | |
101 } | |
102 keyframes.push(this.createKeyframe(t / duration, x, y)); | |
103 } | |
104 | |
105 return new Animation(this.element, keyframes, duration); | |
106 }; | |
107 | |
108 Particle.prototype.createKeyframe = function(offset, x, y) { | |
109 return { | |
110 composite: 'add', | |
111 offset: offset, | |
112 left: x + 'px', | |
113 top: y + 'px', | |
114 }; | |
115 }; | |
116 | |
117 Particle.prototype.destroy = function() { | |
118 document.body.removeChild(this.element); | |
119 }; | |
120 | |
121 function cleanUp() { | |
122 player.source = null; | |
123 for (var i = 0; i < particles.length; i++) { | |
124 particles[i].destroy(); | |
125 } | |
126 particles = []; | |
127 } | |
128 | |
129 window.addEventListener('load', function () { | |
130 var spacing = document.createElement('div'); | |
131 spacing.style.display = 'inline-block'; | |
132 spacing.style.width = '600px'; | |
133 document.body.appendChild(spacing); | |
134 | |
135 var animationGroup = new AnimationGroup([], {iterations: Infinity, direction:
'alternate'}); | |
136 for (var i = 0; i < particleCount; i++) { | |
137 var particle = new Particle(); | |
138 animationGroup.append(particle.generateAnimation(animationDuration)); | |
139 particles.push(particle); | |
140 } | |
141 player = document.timeline.play(animationGroup); | |
142 | |
143 Perf.oncomplete = cleanUp; | |
144 Perf.start(); | |
145 }); | |
146 | |
147 </script> | |
OLD | NEW |