in client/src/components/camera-preview/camera-preview.ts [55:75]
async capture(): Promise<Blob> {
const video = this.video;
if (!video || !this.capturedImage || !this.capturedImage.nativeElement) {
throw new Error('Component not ready');
}
const canvas = this.capturedImage.nativeElement as HTMLCanvasElement;
const width = canvas.width = window.innerWidth;
const height = canvas.height = window.innerHeight;
const videoWidth = video.videoWidth;
const videoHeight = video.videoHeight;
// dimensions need to be relative to video, not the video element, so we need to scale
const scale = Math.max(width / videoWidth, height / videoHeight);
const dx = (videoWidth - width / scale) * 0.5;
const dy = (videoHeight - height / scale) * 0.5;
const context = canvas.getContext('2d');
if (!context) {
throw new Error('Failed creating 2D canvas context');
}
context.drawImage(video, dx, dy, videoWidth - 2 * dx, videoHeight - 2 * dy, 0, 0, width, height);
return await canvasToBlob(canvas);
}