Home | History | Annotate | Download | only in Animation
      1 <!--
      2 Copyright (c) 2012 Cameron Adams. All rights reserved.
      3 Copyright (c) 2012 Code Aurora Forum. All rights reserved.
      4 Copyright (C) 2013 Google Inc. All rights reserved.
      5 
      6 Redistribution and use in source and binary forms, with or without
      7 modification, are permitted provided that the following conditions are
      8 met:
      9     * Redistributions of source code must retain the above copyright
     10 notice, this list of conditions and the following disclaimer.
     11     * Redistributions in binary form must reproduce the above
     12 copyright notice, this list of conditions and the following disclaimer
     13 in the documentation and/or other materials provided with the
     14 distribution.
     15     * Neither the name of Code Aurora Forum Inc., Google Inc. nor the
     16 names of its contributors may be used to endorse or promote products
     17 derived from this software without specific prior written permission.
     18 
     19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30 
     31 This test is based on code written by Cameron Adams and imported from
     32   http://themaninblue.com/experiment/AnimationBenchmark/html
     33 -->
     34 
     35 <!doctype html>
     36 <head>
     37 <title>Benchmark - Balls Animation using CSS Animations</title>
     38 <style>
     39 html {
     40     height: 100%;
     41 }
     42 
     43 body {
     44     width: 100%;
     45     height: 100%;
     46     overflow: hidden;
     47     margin: 0;
     48     padding: 0;
     49 }
     50 
     51 span {
     52     position: absolute;
     53     width: 12px;
     54     height: 12px;
     55     border-radius: 6px;
     56 }
     57 </style>
     58 <script src="../resources/runner.js"></script>
     59 <script src="resources/framerate.js"></script>
     60 <script>
     61 var stageWidth = 600;
     62 var stageHeight = 600;
     63 var maxParticles = 2500;
     64 var minVelocity = 50;
     65 var maxVelocity = 500;
     66 var particleRadius = 6;
     67 var colors = ["#cc0000", "#ffcc00", "#aaff00", "#0099cc", "#194c99", "#661999"];
     68 var animationDuration = 10;
     69 
     70 var particles = [];
     71 var allParticleKeyframes = [];
     72 
     73 window.onload = function () {
     74     PerfTestRunner.prepareToMeasureValuesAsync({done: onCompletedRun, unit: 'fps'});
     75 
     76     // Create the particles
     77     for (var i = 0; i < maxParticles; i++) {
     78         generateParticleKeyframes(i);
     79         var particle = new Particle(i);
     80         particle.start();
     81         particles.push(particle);
     82     }
     83 
     84     startTrackingFrameRate();
     85 }
     86 
     87 function generateParticleKeyframes(index) {
     88     allParticleKeyframes[index] = [];
     89 
     90     var angle = Math.PI * 2 * PerfTestRunner.random();
     91     var velocity = minVelocity + ((maxVelocity - minVelocity) * PerfTestRunner.random());
     92     var x = stageWidth / 2 - particleRadius;
     93     var y = stageHeight / 2 - particleRadius;
     94     var dx = Math.cos(angle) * velocity;
     95     var dy = Math.sin(angle) * velocity;
     96 
     97     function detectCollision(lineX, normalX, lineY, normalY) {
     98         var dtx = Infinity;
     99         var dty = Infinity;
    100         if (dx * normalX < 0)
    101             dtx = (lineX - x) / dx;
    102         if (dy * normalY < 0)
    103             dty = (lineY - y) / dy;
    104         var dt = Math.min(dtx, dty);
    105         var hitX = (dtx < dty);
    106         return {
    107             dt: dt,
    108             x: hitX ? lineX : x + (dx * dt),
    109             y: hitX ? y + (dy * dt) : lineY,
    110             dx: hitX ? -dx : dx,
    111             dy: hitX ? dy : -dy,
    112         };
    113     }
    114 
    115     var t = 0;
    116     allParticleKeyframes[index].push(generateKeyframe(0, x, y));
    117     while (t < animationDuration) {
    118         var collisionA = detectCollision(0, 1, 0, 1);
    119         var collisionB = detectCollision(stageWidth, -1, stageHeight, -1);
    120         var collision = collisionA.dt < collisionB.dt ? collisionA : collisionB;
    121         if (t + collision.dt > animationDuration) {
    122             var dt = animationDuration - t;
    123             t = animationDuration;
    124             x += dx * dt;
    125             y += dy * dt;
    126         } else {
    127             t += collision.dt;
    128             x = collision.x;
    129             y = collision.y;
    130             dx = collision.dx;
    131             dy = collision.dy;
    132         }
    133         allParticleKeyframes[index].push(generateKeyframe(t, x, y));
    134     }
    135 
    136     return keyframes;
    137 }
    138 
    139 function generateKeyframe(t, x, y) {
    140     return {left: x + "px", top: y + "px", offset: t / animationDuration };
    141 }
    142 
    143 function Particle(index)
    144 {
    145     // Create visual element for the particle.
    146     var domNode = document.createElement('span');
    147     document.body.appendChild(domNode);
    148 
    149     // Set colour of element.
    150     domNode.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
    151 
    152     function start() {
    153         var player = domNode.animate(allParticleKeyframes[index], {easing: "linear", iterations: Infinity, direction: "alternate", duration: animationDuration});
    154         player.pause();
    155         this.player = player;
    156     }
    157 
    158     function destroy()
    159     {
    160         document.body.removeChild(domNode);
    161     }
    162 
    163     this.start = start;
    164     this.destroy = destroy;
    165 }
    166 
    167 var start = null;
    168 
    169 function advance(timestamp)
    170 {
    171     if (start == null)
    172         start = timestamp;
    173     for (var x in particles) {
    174         particles[x].player.currentTime = (timestamp - start) / 1000;
    175     }
    176     if (particles.length !== 0)
    177         requestAnimationFrame(advance);
    178 }
    179 
    180 requestAnimationFrame(advance);
    181 
    182 function onCompletedRun() {
    183     testRunning = false;
    184     stopTrackingFrameRate();
    185 
    186     for (var i = 0; i < particles.length; i++)
    187         particles[i].destroy();
    188     particles = [];
    189 }
    190 </script>
    191 <style id="keyframes"></style>
    192 </head>
    193 </html>
    194