Skip to content

Instantly share code, notes, and snippets.

@giniedp
Created June 9, 2017 19:46
Show Gist options
  • Save giniedp/5f7dca64e10ecea1d45c7fad55c012d3 to your computer and use it in GitHub Desktop.
Save giniedp/5f7dca64e10ecea1d45c7fad55c012d3 to your computer and use it in GitHub Desktop.
Capture screenshot from html video element
(function(window) {
'use strict';
window.snapshot = function snap(video, width, height, download) {
video = video || window.document.getElementsByTagName('video')[0];
width = video.width || video.videoWidth;
height = video.height || video.videoHeight;
var canvas = window.document.createElement('canvas');
canvas.width = width;
canvas.height = height;
var context = canvas.getContext('2d');
context.fillRect(0, 0, width, height);
context.drawImage(video, 0, 0, width, height);
var dataURI = canvas.toDataURL('image/jpeg');
if (download) {
dataURI = dataURI.replace('image/jpeg', 'image/octet-stream');
}
window.open(dataURI, '_blank').focus();
};
}(this));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment