{
  "id": 22307349,
  "title": "How to Resize an Image Without Losing Its Aspect Ratio Using JavaScript Canvas",
  "url": "https://urgent.news/2026/11/28/how-to-resize-an-image-without-losing-its-aspect-ratio-using",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-28T05:19:46.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/bedish/how-to-resize-an-image-without-losing-its-aspect-ratio-using-javascript-canvas-10h"
  },
  "original_language": "en",
  "account": "In a recent article, a script was shared that demonstrates how to resize an image in the browser without losing its aspect ratio. Initially, the script produced slightly distorted images due to a single line of code bug. The author shares what they learned about canvas sizes, aspect ratios, and image quality to avoid squashed photos.\n\nAspect ratio is simply the width divided by the height of an image. For example, a 1920 × 1080 image has an aspect ratio of 1920 / 1080 ≈ 1.777..., which is commonly referred to as 16:9. To resize an image without distortion, both the width and height must change by the same scale factor. The new dimensions can be calculated as follows: newWidth = width × scale and newHeight = height × scale.\n\nIf only one target dimension is known, such as a desired width, the other dimension can be calculated using the formula: newHeight = Math.round(newWidth * (height / width)). This ensures the image maintains its aspect ratio during resizing.\n\nA common mistake is confusing the canvas size with the CSS size. The canvas has two sizes: canvas.width and canvas.height, which represent the real pixel dimensions of the output. The CSS width and height only affect how the canvas is displayed on the page. To obtain the actual pixel dimensions, set the canvas attributes and draw the image onto it.\n\nA function called fitWithin is provided to fit an image within a specified box while keeping its aspect ratio. It calculates the smaller of the two scale factors based on the maximum desired width and height. The function returns an object containing the new width and height values.\n\nWhen resizing, it's important to use Math.round to avoid fractional pixels, which can result in blurry edges. Additionally, Math.max(1, ...) is used to prevent a very thin image from rounding down to zero pixels.\n\nThe full resize function is provided, which takes a file input, desired maximum width and height, image type (default is JPEG), and quality factor (default is 0.9). It uses the createImageBitmap function to load the image, calls fitWithin to determine the new dimensions, creates a canvas with the appropriate size, and then draws the image onto the canvas. The resulting bitmap is then converted to a blob and returned as a Promise.\n\nTo use this function, simply attach an event listener to a file input element. When a file is selected, the resizeImage function is called asynchronously, and the resulting blob is assigned as the source of a preview element. Since the image processing occurs locally on the device, the image never leaves the user's computer.",
  "summary": "A few weeks ago I wrote a quick script to resize images in the browser, and the first version made every photo look slightly squashed. The bug was one line. Here's what I learned about canvas sizes, aspect ratios and image quality, so you can skip the squashed photos. Aspect ratio in one minute The aspect ratio is just width divided by height. A 1920 × 1080 image is 1920 / 1080 = 1.777..., which…",
  "summary_source": "publisher",
  "key_points": [
    "Aspect ratio is width divided by height, e.g., 1920 × 1080 has 1.777 (16:9) ratio",
    "New dimensions calculated by multiplying original size by scale factor",
    "fitWithin function resizes image within box while preserving aspect ratio"
  ],
  "editors_take": "Implementing client-side image processing through JavaScript canvas allows users to resize photos locally, ensuring privacy while preventing visual distortion and blurry edges through precise mathematical scaling.",
  "opinion": false,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "machine_translated": false,
  "ai_generated": true,
  "disclaimer": "Accounts, briefs, key points, the editor’s take and translations are written by software from other outlets’ reporting and may contain errors. A summary whose summary_source is \"publisher\" is the outlet’s own feed text. Always check the linked original."
}