Eskillsfy

Home / Blog / Images

Images · 23 Nov 2026

Image Cropper: how it works and how to build one

Crop images freely or to 1:1, 4:3, 16:9 and other ratios. This guide explains what happens behind the button and shows the code, so you can build your own.

What it does

Drag the crop box and its corners over your image, lock an aspect ratio for profile pictures, thumbnails or stories, or type the exact size in pixels. Download as JPG, PNG or WebP at full quality.

When to use it

  • Profile photos
  • Removing edges from screenshots
  • Fitting photos to 1:1 or 16:9

How to use it

  1. Choose or drop an image.
  2. Pick an aspect ratio and drag the crop box.
  3. Download the cropped image.

Try it free in your browser, nothing is uploaded: https://eskillsfy.com/tools/image-cropper

How it works

The crop box is an absolutely positioned div over the image, stored in image pixels (not screen pixels). Pointer events move or resize it; the drag distance is multiplied by naturalWidth ÷ displayed width to convert screen pixels to image pixels.

Exporting draws just that rectangle with drawImage’s nine-argument form: source x, y, width, height, then destination.

Build it yourself

Here is the core of the tool. It has no dependencies unless noted, so you can paste it into any TypeScript or JavaScript project.

crop.ts (ts)

export function crop(img: HTMLImageElement, box: { x: number; y: number; w: number; h: number }) {
  const c = Object.assign(document.createElement("canvas"), { width: Math.round(box.w), height: Math.round(box.h) });
  c.getContext("2d")!.drawImage(img, box.x, box.y, box.w, box.h, 0, 0, c.width, c.height);
  return c;
}

// Screen → image pixels while dragging:
const scale = img.naturalWidth / img.clientWidth;
const dx = (e.clientX - startX) * scale;

Make it production-ready

  • Lock to a ratio while resizing.
  • Use setPointerCapture so drags keep working outside the box.

Questions

Does cropping reduce quality?

No. The cropped area is copied pixel for pixel from the original.

Try the Image Cropper

Free, runs in your browser, nothing is uploaded.

Open it
Keep reading

More in Images

All posts