requestAnimationFrame

MAKE SURE TO CLICK INTO THE RESULT IFRAME OR ELSE SAFARI WILL ONLY RENDER RAF / SETINTERVAL AT MAX 30FPS DUE TO POWER SAVING CODE

by dustinkerstein

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/110/three.min.js"></script>

CSS

body {
  margin: 0;
  padding: 0;
  background-color: black; 
}

video {
  width: 100vw;
  height: 100vh;
}

JavaScript

// MAKE SURE TO CLICK INTO THE RESULT IFRAME OR ELSE SAFARI WILL ONLY RENDER RAF / SETINTERVAL AT MAX 30FPS DUE TO POWER SAVING CODE

var index = 0;
var url = "https://s3.amazonaws.com/files.panomoments.com/hd_dashinit.mp4"; // 100% I-Frames for fast seeking
var video = document.createElement('video');
video.preload = "auto";
video.muted = true;
video.autoplay = true; // Needed for iOS to trigger readyState = 4
video.loop = true;
document.body.appendChild(video);


var animationStrategy = 1; 

if (animationStrategy == 1) {
	window.requestAnimationFrame(render); // Slower performance than setInterval
} else if (animationStrategy == 2) {
	setInterval(render, 1000/60);
}

function render() {
	if (animationStrategy == 1) {
  	window.requestAnimationFrame(render);
  }
  if (video.readyState >= 3) { 
    index = (index + 1) % 242;
    video.currentTime = index;
  }
}

fetch(url).then(function(response) {
  response.blob().then(function(myBlob) {
    video.src = window.URL.createObjectURL(myBlob); // Seeking with blob occasionally shows XPC memory issue on Safari but even with this issue you should be able ot see that rAF performs worse than setInterval
  });
});

(function(){var script=document.createElement('script');script.onload=function(){var stats=new Stats();document.body.appendChild(stats.dom);requestAnimationFrame(function loop(){stats.update();requestAnimationFrame(loop)});};script.src='//mrdoob.github.io/stats.js/build/stats.min.js';document.head.appendChild(script);})()