A video can indeed be used as the image source for a canvas element. To achieve this, simply encapsulate the code within a function that accepts the video and desired size as arguments, and returns a canvas element.
It is important to ensure that the video is loaded and at the specific frame from which you wish to capture the snapshot.
Illustrative Functions
function generateThumbnail(video, width, height) {
var canvas = document.createElement("canvas"), // create a new canvas
context = canvas.getContext("2d"); // obtain context
canvas.width = width; // set dimensions
canvas.height = height;
context.drawImage(video, 0, 0, width, height); // draw specified frame
return canvas; // return the canvas
}
The resulting canvas can then be integrated into the DOM to serve as the placeholder for the image. If you prefer using an image element instead, additional steps are required, including managing the asynchronous nature of image loading through a callback (or promise):
function generateThumbnail(video, width, height, callback) {
var canvas = document.createElement("canvas"), // create a new canvas
context = canvas.getContext("2d"), // obtain context
image = new Image(); // final image
canvas.width = width; // set dimensions
canvas.height = height;
context.drawImage(video, 0, 0, width, height); // draw specified frame
image.onload = function() { // handle asynchronous loading
callback(this); // pass image to callback function
};
image.src = canvas.toDataURL(); // convert canvas to URL
}
If issues arise in terms of the video frame size, you can modify the arguments of the drawImage() function like so:
context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight, 0, 0, width, height);