From 1bed329472e3e034bc2d744248f6cce4792204f4 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Wed, 8 Jul 2026 11:25:05 -0400 Subject: [PATCH] Add fixed aspect ratio and image scaling on download --- index.html | 11 +++++++---- js/app.js | 27 ++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 62bff7c..f905ec0 100644 --- a/index.html +++ b/index.html @@ -39,10 +39,13 @@

Capture a photo from your webcam and save it using the student number.

-
- - -

Requesting camera access…

+
+
+ + +

Requesting camera access…

+
+

Frame matches the final 145×185 photo — fill it with the student's face and shoulders.

diff --git a/js/app.js b/js/app.js index 9ad02e5..79fd1c5 100644 --- a/js/app.js +++ b/js/app.js @@ -7,6 +7,10 @@ const inputError = document.getElementById('input-error'); const captureBtn = document.getElementById('capture-btn'); const downloadLink = document.getElementById('download-link'); +const OUTPUT_WIDTH = 145; +const OUTPUT_HEIGHT = 185; +const TARGET_RATIO = OUTPUT_WIDTH / OUTPUT_HEIGHT; + let captured = false; async function startCamera() { @@ -53,10 +57,27 @@ function capturePhoto() { } inputError.classList.add('hidden'); - canvas.width = video.videoWidth; - canvas.height = video.videoHeight; + const videoWidth = video.videoWidth; + const videoHeight = video.videoHeight; + const videoRatio = videoWidth / videoHeight; + + let sx, sy, sw, sh; + if (videoRatio > TARGET_RATIO) { + sh = videoHeight; + sw = sh * TARGET_RATIO; + sx = (videoWidth - sw) / 2; + sy = 0; + } else { + sw = videoWidth; + sh = sw / TARGET_RATIO; + sx = 0; + sy = (videoHeight - sh) / 2; + } + + canvas.width = OUTPUT_WIDTH; + canvas.height = OUTPUT_HEIGHT; const ctx = canvas.getContext('2d'); - ctx.drawImage(video, 0, 0, canvas.width, canvas.height); + ctx.drawImage(video, sx, sy, sw, sh, 0, 0, OUTPUT_WIDTH, OUTPUT_HEIGHT); const dataUrl = canvas.toDataURL('image/png'); const filename = `${sanitizeFilename(studentNumber)}.png`;