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 var testing = true; |
| 47 |
| 48 var Particle = function() { |
| 49 this.element = document.createElement('div'); |
| 50 this.element.className = 'ball'; |
| 51 this.element.style.backgroundColor = colors[Math.floor(Perf.random() * colors.
length)]; |
| 52 document.body.appendChild(this.element); |
| 53 }; |
| 54 |
| 55 Particle.prototype.generateAnimation = function(duration) { |
| 56 var keyframes = []; |
| 57 |
| 58 var angle = Math.PI * 2 * Perf.random(); |
| 59 var velocity = minVelocity + ((maxVelocity - minVelocity) * Perf.random()); |
| 60 var x = stageWidth / 2; |
| 61 var y = stageHeight / 2; |
| 62 var dx = Math.cos(angle) * velocity; |
| 63 var dy = Math.sin(angle) * velocity; |
| 64 |
| 65 var nextCollision = function(lineX, normalX, lineY, normalY) { |
| 66 var dtx = Infinity; |
| 67 var dty = Infinity; |
| 68 if (dx * normalX < 0) |
| 69 dtx = (lineX - x) / dx; |
| 70 if (dy * normalY < 0) |
| 71 dty = (lineY - y) / dy; |
| 72 var dt = Math.min(dtx, dty); |
| 73 var hitX = (dtx < dty); |
| 74 return { |
| 75 dt: dt, |
| 76 x: hitX ? lineX : x + (dx * dt), |
| 77 y: hitX ? y + (dy * dt) : lineY, |
| 78 dx: hitX ? -dx : dx, |
| 79 dy: hitX ? dy : -dy, |
| 80 }; |
| 81 }; |
| 82 |
| 83 var t = 0; |
| 84 keyframes.push(this.createKeyframe(0, x, y)); |
| 85 while (t < duration) { |
| 86 var collisionA = nextCollision(0, 1, 0, 1); |
| 87 var collisionB = nextCollision(stageWidth, -1, stageHeight, -1); |
| 88 var collision = collisionA.dt < collisionB.dt ? collisionA : collisionB; |
| 89 if (t + collision.dt > duration) { |
| 90 var dt = duration - t; |
| 91 t = duration; |
| 92 x += dx * dt; |
| 93 y += dy * dt; |
| 94 } else { |
| 95 t += collision.dt; |
| 96 x = collision.x; |
| 97 y = collision.y; |
| 98 dx = collision.dx; |
| 99 dy = collision.dy; |
| 100 } |
| 101 keyframes.push(this.createKeyframe(t / duration, x, y)); |
| 102 } |
| 103 |
| 104 return new Animation(this.element, keyframes, duration); |
| 105 }; |
| 106 |
| 107 Particle.prototype.createKeyframe = function(offset, x, y) { |
| 108 return { |
| 109 composite: 'add', |
| 110 offset: offset, |
| 111 left: x + 'px', |
| 112 top: y + 'px', |
| 113 }; |
| 114 }; |
| 115 |
| 116 Particle.prototype.destroy = function() { |
| 117 document.body.removeChild(this.element); |
| 118 }; |
| 119 |
| 120 var cleanUp = function() { |
| 121 player.source = null; |
| 122 for (var i = 0; i < particles.length; i++) { |
| 123 particles[i].destroy(); |
| 124 } |
| 125 particles = []; |
| 126 }; |
| 127 |
| 128 var raf = window.requestAnimationFrame || |
| 129 window.webkitRequestAnimationFrame || |
| 130 window.mozRequestAnimationFrame || |
| 131 function(callback) { setTimeout(callback, 1000 / 60); }; |
| 132 |
| 133 var shake = 0; |
| 134 var shakeParticles = function() { |
| 135 shake = 20 - shake; |
| 136 for (var i = 0; i < particles.length; i++) { |
| 137 particles[i].element.style.setProperty('left', shake + 'px'); |
| 138 particles[i].element.style.setProperty('top', shake + 'px'); |
| 139 } |
| 140 if (testing) { |
| 141 raf(shakeParticles); |
| 142 } |
| 143 }; |
| 144 |
| 145 window.addEventListener('load', function () { |
| 146 var spacing = document.createElement('div'); |
| 147 spacing.style.display = 'inline-block'; |
| 148 spacing.style.width = '600px'; |
| 149 document.body.appendChild(spacing); |
| 150 |
| 151 var parGroup = new ParGroup([], {iterations: Infinity, direction: 'alternate'}
); |
| 152 for (var i = 0; i < particleCount; i++) { |
| 153 var particle = new Particle(); |
| 154 parGroup.append(particle.generateAnimation(animationDuration)); |
| 155 particles.push(particle); |
| 156 } |
| 157 player = document.timeline.play(parGroup); |
| 158 |
| 159 raf(shakeParticles); |
| 160 |
| 161 Perf.oncomplete = cleanUp; |
| 162 Perf.start(); |
| 163 }); |
| 164 |
| 165 </script> |
OLD | NEW |