Skip to content

Instantly share code, notes, and snippets.

@rezonn
Last active May 5, 2020 07:59
Show Gist options
  • Save rezonn/6ff32e7318c00a0038c302f3607fa194 to your computer and use it in GitHub Desktop.
Save rezonn/6ff32e7318c00a0038c302f3607fa194 to your computer and use it in GitHub Desktop.
Webcam to Video to Canvas native JavaScript. Run on server
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<video id="video" autoplay playsinline></video>
<canvas id="canvas" onclick="this.webkitRequestFullscreen()"></canvas>
<style>:-webkit-full-screen {position:fixed;width:100%;height:auto;top:0;left:0;}</style>
<script>
navigator.mediaDevices.getUserMedia({video:true}).then(function (stream) {
video.srcObject = stream;
}).catch(function (error) {
console.log(error);
});
video.onplay = function(){
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
var ths = this;
var ctx = canvas.getContext('2d');
(function loop() {
if (!ths.paused && !ths.ended) {
ctx.save();
ctx.translate(canvas.width, 0);
ctx.scale(-1, 1);
ctx.drawImage(ths, 0, 0);
ctx.restore();
window.requestAnimationFrame(loop);
}
})();
}
</script>
<br><code>dont work?</code>
<br><code>Safari -> Developer menu -> WebRTC -> Allow...</code>
<br><code>in macos terminal:</code>
<br><code>python3 -m http.server 8000</code>
<br><code>open http://localhost:8000</code>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment