chromium/tools/perf/page_sets/tough_animation_cases/css_transitions_staggered_triggering_by_inserting_style_element.html

<!DOCTYPE html>
<meta name="viewport" content="width=device-width, user-scalable=no">
<link rel="stylesheet" type="text/css" href="resources/tablet.css">
<link rel="stylesheet" type="text/css" href="resources/transition.css">
<script src="resources/perf_test_helper.js"></script>

<container id="container"></container>

<script>
var N = PerfTestHelper.getN(1000);
var duration = 1000;
var targets = [];
var styles = [];
var keyframeValues = [0, 1];

for (var i = 0; i < N; i++) {
  var target = document.createElement('target');
  target._keyframe = 0;
  container.appendChild(target);
  targets.push(target);
}

function addNewClassStyle(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 className = 'style' + step + 'keyframe';
  style.textContent = '.' + className + '0 { opacity: ' + keyframeValues[0] + '; }\n';
  style.textContent += '.' + className + '1 { opacity: ' + keyframeValues[1] + '; }\n';
  container.appendChild(style);
  styles.push({
    style: style,
    lastStep: step + N,
  });

  return className;
}

function startTransitions(startingTargets, startStep, endStep) {
  var newClassName = addNewClassStyle(startStep, endStep);
  startingTargets.forEach(function(startInfo) {
    startInfo.target._keyframe ^= 1;
    startInfo.target.className = newClassName + startInfo.target._keyframe;
    startInfo.target.style.transitionDelay = -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,
      });
    }
    startTransitions(restartingTargets, targetStep);
    previousStep = targetStep;
    requestAnimationFrame(staggeredStart);
  }
  staggeredStart(time);
});

PerfTestHelper.signalReady();
</script>