Skip to content

Instantly share code, notes, and snippets.

@ayamflow
Created April 7, 2017 00:00
Show Gist options
  • Save ayamflow/d91f7100feeaa4093f2883f5c8fa1f83 to your computer and use it in GitHub Desktop.
Save ayamflow/d91f7100feeaa4093f2883f5c8fa1f83 to your computer and use it in GitHub Desktop.
Force play video
// https://github.com/hay/gifvideo/blob/master/gifvideo.js
var start;
var previousTime;
video.load();
// requestAnimationFrame automatically provides Date.now()
function play(now) {
// only play if it has the data,
// without waiting for ambiguous events
if (video.readyState >= video.HAVE_FUTURE_DATA) {
if (video.currentTime >= video.duration - 0.05) {
// safer way to check whether the video is ended
video.currentTime = 0; // restart video
} else {
//make it start at 0
previousTime = previousTime || now;
// increase it by the time it passed since the last
// animationFrame
video.currentTime += (now - previousTime) / 1000;
}
}
previousTime = now;
requestAnimationFrame(play);
}
play(Date.now());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment