I'm having a weird issue when trying to make a webpage on which I want to capture a picture from the visitors webcam. I have two divs with a button in between which draws the content of the webcam stream in the first div at the time the button is clicked on a canvas in the second div.
The problem is that the image of my stream seems to get clipped: only the upper left corner seems to be captured. I've tried playing with the dimensions of the canvas and the div containing it to no avail. The problem also persists when I save the canvas as an image.
Here is a test page I made that has this issue:
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Webcam test</title>
<style>
#picturecontaienr {
width: 800px;
height: 800px;
}
#takenpicture {
width: 750px;
height: 750px;
margin: 0%;
padding: 0%;
}
</style>
</head>
<body>
<h1>Webcam test!</h1>
<div id="webcamcontainer">
<video width="500" height="500" autoplay="true" id="webcamstream">
</video>
</div>
<button id="takepicture">Take picture!</button>
<div id="picturecontainer">
<canvas id="takenpicture">
</canvas>
<script>
var video = document.getElementById ( "webcamstream" );
navigator.getUserMedia = ( navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia ||
navigator.oGetUserMedia );
if ( navigator.getUserMedia ) {
navigator.getUserMedia ( {video:true}, function ( stream ) {
video.src = window.URL.createObjectURL ( stream );
}, function ( ) {
document.getElementById ( "videocontainer" ).innerHTML += "<p>You did not give permission to take a picture of your QR code!</p>";
} );
}
document.getElementById ( "takepicture" ).addEventListener ( "click", function ( ) {
var drawingContext = document.getElementById ( "takenpicture" ).getContext ( "2d" );
drawingContext.drawImage ( video, 0, 0, 500, 500 );
//var imageSource = document.getElementById ( "takenpicture" ).toDataURL ( "image/png" );
//alert ( imageSource );
} );
</script>
</body>
</html>
I would be very grateful if someone could point me in the right direction.
Thanks in advance, Joshua