Urgent.News

What's breaking now, across hundreds of outlets.

Tech

How to Resize an Image Without Losing Its Aspect Ratio Using JavaScript Canvas

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…

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.

Aspect 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.

If 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.

A 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.

A 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.

When 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.

The 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.

To 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.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

CoreShroud Update: Deeper Protection, Faster Pipeline, Cleaner UI

The CoreShroud beta has been running for a few weeks now, and most of the work since the last post did not go into new buttons.

  • Protection pipeline reorganized to report individual stage outcomes
  • Protection presets streamline repeat builds and simplify audit trails
  • UI improvements include responsive layout, localized relative times, and better accessibility

Windows 12何時來?微軟高層表態 先救Windows 11

Windows 11問世已滿5年,下一代Windows 12究竟何時登場?微軟Windows部門執行副總裁Pavan Davuluri近日受訪時做出回應,雖然未透露新一代作業系統的推出時間,但強調現階段的首要任務是讓Windows 11變得更快、更穩定,以及證明AI功能真的對用戶有幫助…

More from Saturday, 28 November 2026 →