File size: 797 Bytes
5f07a23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function drawImageMaintainAspectRatio(ctx, image, canvas) {
  // Clear the canvas first
  ctx.fillStyle = '#000'; // or '#fff' for white bars
  ctx.fillRect(0, 0, canvas.width, canvas.height);
  
  // Calculate aspect ratios
  const imageRatio = image.width / image.height;
  const canvasRatio = canvas.width / canvas.height;
  
  let drawWidth, drawHeight, x, y;
  
  if (imageRatio > canvasRatio) {
    // Image is wider than canvas
    drawWidth = canvas.width;
    drawHeight = canvas.width / imageRatio;
    x = 0;
    y = (canvas.height - drawHeight) / 2;
  } else {
    // Image is taller than canvas
    drawHeight = canvas.height;
    drawWidth = canvas.height * imageRatio;
    x = (canvas.width - drawWidth) / 2;
    y = 0;
  }
  
  ctx.drawImage(image, x, y, drawWidth, drawHeight);
}