A circle crop is not a shape: what the exported PNG actually contains
A circle crop is a square image file with transparent corners, not a photo with a round frame painted on top. On a 512 px crop of a 1200 × 800 test photo, 55,764 pixels were fully transparent, 1,132 pixels along the circ
A circle crop is a square image file with transparent corners, not a photo with a round frame painted on top. On a 512 px crop of a 1200 × 800 test photo, 55,764 pixels were fully transparent, 1,132 pixels along the circle edge were partly transparent, and 205,248 were solid.
The file is square; the shape lives in the alpha channel
PNG and WebP store an alpha value for every pixel. Zero means nothing is painted there, 255 means the pixel is fully covered, and values in between mean partial coverage. A circle crop uses all three. The canvas keeps its square dimensions — 512 × 512 in the examples below — and the area outside the circle is either clear or a soft ramp where it meets the edge.
That is why one PNG looks round on a white profile page, a dark dashboard and a photo background: the viewer composites your pixels over whatever is behind them.
JPEG cannot store this. The format has no alpha channel, so whatever was transparent becomes a solid colour at encode time. If an upload target only accepts JPEG, pick the corner colour on purpose, because you cannot recover transparency after export.
The edge: soft ramp versus staircase
Anti-aliasing is coverage arithmetic. When a browser rasterises a clipped arc, each boundary pixel receives a fractional alpha based on how much of its area falls inside the shape. A hard mask asks one question per pixel — is the centre inside? — and answers alpha 0 or 255.
Both versions passed a naive corner test: four corner pixels at alpha 0 in each. The difference shows up along the rim: the clipped version had 1,132 partially transparent pixels, the hard mask had none.
Why worry about pixels you can barely see? Because the edge is what people see when the file is small, and a 512 px export often lands in a 40 px avatar slot. When the browser scales that PNG down, a soft edge gives the resampler a gradient to average; a binary edge leaves isolated solid pixels that survive as notches. That is also the argument for checking an export at 40 px instead of trusting the large preview: defects that vanish at 512 px become obvious at display size.
Why border-radius: 50% does not give you a circle PNG
Applying border-radius: 50% and overflow: hidden to an <img> produces a visually round element. It does not produce a round file, which is usually the actual problem.
The clip happens at paint time. The bytes behind the src attribute never change. Right-click → Save image as still hands you the original rectangle. Share the page and the recipient sees the rectangle in an editor. Screenshot the circle and you capture the page background underneath it, at screen resolution.
If the round picture only ever exists on a page you control, this does not matter. It matters the moment a file goes into an upload form, a slide, a store listing or a print order: those systems read bytes, not CSS.
CSS also clips at layout size, and the browser does not store the clipped result anywhere you can download.
Fit versus fill: choose this before you touch pixels
Two framing decisions come first. Crop to fill scales the photo until it covers the circle, then cuts the overflow, so the circle is fully covered but the sides or the top and bottom are gone. Fit full image shrinks the whole photo until it fits inside the circle, so nothing is cut and the unused area stays transparent — or becomes white, dark, a custom colour, or a blurred copy of the same photo if you prefer a filled background.
For a portrait, fill is usually right: the face and shoulders cover the frame. Watch the top of the head, where fill crops fail. For a logo, fit is usually right: the mark keeps its corners and you control the gap around it. A wide wordmark fitted inside a circle ends up small, so add padding and judge it at 40 px. Stickers and badges usually look better with fit plus a transparent margin.
One photo, two circle crops: fill on the left keeps the circle full, fit on the right keeps the whole frame and leaves transparent space.
A canvas snippet that produces the same file
Here is the whole operation, dependency-free, in a form you can paste into a page. It centres a square source crop and clips it to a circle.
async function circleCrop(file, size = 512) {
const bitmap = await createImageBitmap(file);
// Square source crop, centred on the short side (crop-to-fill behaviour).
const side = Math.min(bitmap.width, bitmap.height);
const sx = (bitmap.width - side) / 2;
const sy = (bitmap.height - side) / 2;
const canvas = document.createElement('canvas');
canvas.width = canvas.height = size; // also clears the canvas
const ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(size / 2, size / 2, size / 2, 0, Math.PI * 2);
ctx.clip();
ctx.drawImage(bitmap, sx, sy, side, side, 0, 0, size, size);
return new Promise((resolve, reject) => {
canvas.toBlob(
(blob) => (blob ? resolve(blob) : reject(new Error('PNG encode failed'))),
'image/png'
);
});
}
const input = document.querySelector('input[type="file"]');
input.addEventListener('change', async () => {
const blob = await circleCrop(input.files[0]);
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'avatar-512.png';
a.click();
URL.revokeObjectURL(url);
});
The order matters: set the clip path, then draw. A fresh canvas is already transparent, so the pixels outside the arc stay at alpha 0 with no clearRect call. canvas.width = canvas.height = size resets the bitmap if you reuse the same canvas, which sidesteps stale-canvas bugs.
For fit instead of fill, keep the clip and swap the draw line for a proportional scale:
const scale = Math.min(size / bitmap.width, size / bitmap.height);
const w = bitmap.width * scale;
const h = bitmap.height * scale;
ctx.drawImage(bitmap, (size - w) / 2, (size - h) / 2, w, h);
toBlob is asynchronous and its callback can receive null, so the promise wrapper earns its place. 'image/png' is deliberate: PNG carries alpha, which is the whole point. For very large sources, ctx.imageSmoothingQuality = 'high' gives the downscale a better filter; the edge itself still comes from the clip.
If a phone photo arrives sideways, the cause is the EXIF orientation flag rather than the crop; normalise the source before drawing.
What I measured, and what it means
The run was in Chromium 158 on macOS. The source was a 1200 × 800 test image with a marker in each corner; I took a centred 800 × 800 source rectangle and drew it into a 512 × 512 canvas. The anti-aliased version used ctx.arc(256, 256, 256) plus clip() before drawImage. The hard version drew the same pixels and then set alpha to 0 for every pixel whose centre fell outside the circle. I also re-drew the shipped example PNG and measured it the same way.
| Measurement | Anti-aliased clip()
|
Hard-edged mask | Shipped example export |
|---|---|---|---|
| Fully transparent pixels (alpha 0) | 55,764 | 56,252 | 55,764 |
| Partially transparent pixels (alpha 1–254) | 1,132 | 0 | 1,132 |
| Fully opaque pixels (alpha 255) | 205,248 | 205,892 | 205,248 |
| Four corner pixels (alpha) | 0, 0, 0, 0 | 0, 0, 0, 0 | 0, 0, 0, 0 |
The three buckets sum to 262,144, which is 512 × 512. The ideal circle area at radius 256 is about 205,887 px, in line with 205,248 opaque pixels plus 1,132 partial ones. The hard mask reports 205,892 opaque — a handful more, because a per-pixel-centre rule pulls in boundary pixels whose centres are inside even when part of their area is outside.
The shipped export matched the anti-aliased canvas clip exactly: same three counts, same clear corners. That is the useful confirmation. A corner check alone would not have told you anything, because the hard mask passed it too.
File size is the other half of the story. That PNG was 36,680 bytes. The same canvas encoded as JPEG at quality 0.92 was 25,835 bytes, 29.6% smaller. Reading the JPEG back into a canvas gave 0 transparent pixels, 0 partial pixels, 262,144 opaque pixels, and all four corners at alpha 255 — the corners came back as solid colour. The shipped example export is 36,687 bytes, within a few bytes of the canvas export, which is what you expect when the pixels are the same.
Treat those figures as one test image's results, not universal constants. The structure is what generalises: a transparent PNG has a soft edge and clear corners; a JPEG cannot.
The same circle PNG over light and dark backgrounds. The corners show each background through them, and the rim stays smooth.
A short pre-upload check
- Export PNG or WebP when the corners must be transparent; JPEG cannot keep them clear.
- View the file at 40 px, on a light and a dark surface. If the circle reads as an oval, the framing was off, not the mask.
- Confirm real transparency by placing the file on a coloured slide or reading one corner pixel:
ctx.getImageData(0, 0, 1, 1).data[3]is 0 for a clear corner. - Check the top of the head in fill mode and the wordmark in fit mode; those spots expose bad framing fastest.
- Match the width to the destination and keep the original.
Maker note and links
I built the cropper used for the test image, example export and measurements above, and I use it for this job. It runs in the browser: the photo is read and exported on the device, nothing is uploaded, and there is no account, email step, watermark or daily limit. Output is PNG, JPEG or WebP at any whole-number width from 1 to 4,096 px, with circle, oval, heart and rounded-rectangle shapes, ring and padding controls, and 40 px and 64 px previews.
If you want the tool rather than the theory, start at the circle cropper. If you are still choosing an approach, the five-check comparison covers transparency, output size, local processing, shapes and phone support. Step-by-step paths for Google Slides, Google Docs, PowerPoint, Photoshop, Figma and GIMP live on the app-by-app page.
The browser workspace: framing controls, export settings, and 40 px / 64 px previews next to the full-size canvas.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.

