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