<!DOCTYPE html>
<meta name="viewport" content="width=device-width, user-scalable=no">
<link rel="stylesheet" type="text/css" href="resources/tablet.css">
<script src="resources/perf_test_helper.js"></script>
<style>
target {
-webkit-animation: 1000ms linear;
}
</style>
<container id="container"></container>
<script>
var N = PerfTestHelper.getN(1000);
var duration = 1000;
var targets = [];
var styles = [];
for (var i = 0; i < N; i++) {
var target = document.createElement('target');
container.appendChild(target);
targets.push(target);
}
function addNewAnimationStyle(step) {
// Clean up overwritten styles.
var i = 0;
for (; i < styles.length && styles[i].lastStep < step; i++) {
styles[i].style.remove();
}
styles.splice(0, i);
var style = document.createElement('style');
var animationName = 'anim' + step;
style.textContent = '@-webkit-keyframes ' + animationName + ' { 0% { opacity: 0; } 100% { opacity: 1; } }';
container.appendChild(style);
styles.push({
style: style,
lastStep: step + N,
});
return animationName;
}
function startAnimations(startingTargets, startStep, endStep) {
var newAnimationName = addNewAnimationStyle(startStep, endStep);
startingTargets.forEach(function(startInfo) {
startInfo.target.style.animationName = newAnimationName;
startInfo.target.style.animationDelay = -startInfo.fastForward + 'ms';
});
}
requestAnimationFrame(function(time) {
var startTime = time - duration;
var previousStep = 0;
function staggeredStart(time) {
var elapsed = time - startTime;
var targetStep = Math.floor(N * elapsed / duration);
var restartingTargets = [];
for (var step = Math.max(targetStep - N, previousStep); step < targetStep; step++) {
var stepStartTime = step / N * duration;
restartingTargets.push({
target: targets[step % N],
fastForward: elapsed - stepStartTime,
});
}
startAnimations(restartingTargets, targetStep);
previousStep = targetStep;
requestAnimationFrame(staggeredStart);
}
staggeredStart(time);
PerfTestHelper.signalReady();
});
</script>