-
-
Save monkeymonk/d1f28f566afca14952f64a83b82dea4a to your computer and use it in GitHub Desktop.
Capture a thumbnail from a video with JavaScript.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<script type='text/javascript'> | |
window.onload = function () { | |
var video = document.getElementById('videoId'); | |
var canvas = document.getElementById('canvasId'); | |
var img = document.getElementById('imgId'); | |
video.addEventListener('play', function () { | |
canvas.style.display = 'none'; | |
img.style.display = 'none'; | |
}, false); | |
video.addEventListener('pause', function () { | |
canvas.style.display = 'block'; | |
img.style.display = 'block'; | |
draw(video, canvas, img); | |
}, false); | |
}; | |
function draw(video, canvas, img) { | |
var context = canvas.getContext('2d'); | |
context.drawImage(video, 0, 0, canvas.width, canvas.height); | |
var dataURL = canvas.toDataURL(); | |
img.setAttribute('src', dataURL); | |
} | |
</script> | |
</head> | |
<body> | |
<video id="videoId" autoplay loop controls> | |
<source src="test.webm" type="video/webm"> | |
<source src="test.mp4" type="video/mp4"> | |
Your browser does not support the video tag. | |
</video> | |
<canvas id="canvasId"></canvas> | |
<img id="imgId" src=""/> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment