Skip to content

Commit eecdb88

Browse files
committed
#796 [JS] fix: use aspect ratio 16/9 for calculate canvas size
1 parent 73510b4 commit eecdb88

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

js/modules/media.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,25 @@ window.saturne.media.drawImageOnCanvas = function(event) {
316316
img.src = event.target.result;
317317
window.saturne.media.img = event;
318318
img.onload = function() {
319-
if (img.width >= $('.fast-upload-options').data('image-resolution-width')) {
320-
window.saturne.media.canvas.width = $('.fast-upload-options').data('image-resolution-width');
321-
window.saturne.media.canvas.height = $('.fast-upload-options').data('image-resolution-height');
319+
const maxWidth = $('.fast-upload-options').data('image-resolution-width');
320+
const maxHeight = $('.fast-upload-options').data('image-resolution-height');
321+
const aspectRatio = 16 / 9;
322+
323+
let canvasWidth, canvasHeight;
324+
325+
if (img.width / img.height > aspectRatio) {
326+
// Image is wider than 16:9
327+
canvasWidth = Math.min(img.width, maxWidth);
328+
canvasHeight = canvasWidth / aspectRatio;
322329
} else {
323-
window.saturne.media.canvas.width = img.width;
324-
window.saturne.media.canvas.height = img.height;
330+
// Image is taller than 16:9
331+
canvasHeight = Math.min(img.height, maxHeight);
332+
canvasWidth = canvasHeight * aspectRatio;
325333
}
334+
335+
window.saturne.media.canvas.width = canvasWidth;
336+
window.saturne.media.canvas.height = canvasHeight;
337+
326338
context.drawImage(img, 0, 0, window.saturne.media.canvas.width, window.saturne.media.canvas.height);
327339

328340
// Restore drawing state

0 commit comments

Comments
 (0)