I am taking a picture from a video using this tutorial
HTML:
<canvas id="canvas" style="display:none;" ></canvas>
<div class="output">
<img id="photo" alt="" src="">
</div>
JS
let imgElement = document.querySelector("#photo");
function takePicture(results) {
let canvas = document.getElementById('canvas');
let context = canvas.getContext('2d');
if (width && height) {
canvas.width = width;
canvas.height = height;
context.drawImage(results.image, 0, 0, canvas.width, canvas. Height);
let data = canvas.toDataURL('image/png');
imgElement.setAttribute('src', data);
} else {
clearPhoto();
}
}
It works and gives me an image of the full screen but I need a CROPPED image - just some part in the middle of the screen - the face of the user.
I try to:
context.drawImage(results.image, 0, 0, canvas. Width, canvas. Height, 100, 100, 300, 300);
but it only shrinks the image. I also tried to make a copy of this image in another canvas and image element but it comes up empty.
How to crop it properly?