Cropper.js Avatar Crop with Preview — Free JS Snippet

Cropper.js Avatar Crop with Live Circular Preview · Forms · Plain HTML, CSS & JS · Live preview

CategoryForms

What's included

Features

Fixed round crop area over a draggable, zoomable photo
Live previews at three sizes via the preview option
viewMode 1 keeps the crop box from showing empty space
Rotate, flip, zoom and reset controls
Circular border-radius applied to Cropper's own view box and face
Fixed 256 × 256 output regardless of source resolution
Genuine transparent circular PNG by clipping a second canvas
Generated sample photo, so no image files are needed

About this UI Snippet

Cropper.js Avatar Crop with Live Preview — HTML, CSS & JavaScript

Screenshot of the Cropper.js Avatar Crop with Live Circular Preview snippet rendered live

Avatar upload is one of the most common image tasks on the web and one of the easiest to get wrong. Users pick a 4000-pixel photo straight off a phone, and the app stores all of it, displays it squashed into a circle with object-fit, and cuts off the face. A cropper fixes that at the source: the user chooses exactly which part of the image becomes their avatar, and you upload a small, correctly framed file instead of the original.

Cropper.js does the geometry. This snippet configures it the way avatar editors behave, which differs from the library's default "resize the box over the photo" mode. dragMode: 'move' makes dragging pan the photo, while cropBoxMovable and cropBoxResizable are off so the crop area stays fixed and centred — the same model as most social apps. viewMode: 1 prevents the photo being dragged so far that the box would show empty space, and aspectRatio: 1 locks the box square. Because the box is square, the round look is applied with a border-radius on Cropper's own view-box and face elements, so what the user sees while editing matches the avatar they will get.

The most useful option is preview. Passing a selector makes Cropper mirror the current crop into every matching element, live, with no extra code. Three sizes — 96, 56 and 32 pixels, matching a profile page, a comment and a compact list — show at once, which is how you catch the mistake of choosing a crop that looks fine large but unreadable small.

The export step has the classic gotcha. getCroppedCanvas() always returns a rectangle; there is no circular option, and the CSS border-radius does not exist in the pixels. To produce a genuinely round image with a transparent background you have to clip a second canvas to a circle and draw the cropped square into it, as the save handler does. Passing width and height to getCroppedCanvas fixes the output at 256 pixels regardless of the source size, with imageSmoothingQuality set to high for a clean downscale. Upload that output, and display size and storage stay small.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Ask an AI assistant like Claude to upload the result with fetch and FormData, add drag-and-drop for choosing a file, or add a zoom slider bound to cropper.zoomTo().

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build an avatar cropper with Cropper.js 1.6 loaded from a CDN (script and CSS).

Requirements:
- Use aspectRatio: 1, viewMode: 1, dragMode: 'move', cropBoxMovable: false and cropBoxResizable: false so the photo moves behind a fixed crop area.
- Make the crop box look circular with CSS border-radius on .cropper-view-box and .cropper-face.
- Use the preview option to show live circular previews at 96, 56 and 32 pixels.
- Add zoom in/out, rotate, flip and reset buttons using zoom(), rotate(), scaleX() and reset().
- On save, call getCroppedCanvas({ width: 256, height: 256 }), clip a second canvas to a circle, and export a transparent PNG with its approximate size.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Source Code

<div class="av-wrap">
  <div class="av-stage"><img id="avImg" alt="Photo to crop"></div>
  <aside class="av-side">
    <h3>Profile photo</h3>
    <div class="av-previews" aria-label="Live previews">
      <div class="av-pv av-lg"></div>
      <div class="av-pv av-md"></div>
      <div class="av-pv av-sm"></div>
    </div>
    <div class="av-tools">
      <button type="button" data-a="zoomIn" title="Zoom in">+</button>
      <button type="button" data-a="zoomOut" title="Zoom out">&minus;</button>
      <button type="button" data-a="rotate" title="Rotate 90 degrees">&#8635;</button>
      <button type="button" data-a="flip" title="Flip horizontally">&#8646;</button>
      <button type="button" data-a="reset" title="Reset">Reset</button>
    </div>
    <button type="button" class="av-save" id="avSave">Crop &amp; save</button>
    <div class="av-out" id="avOut" aria-live="polite">Drag the photo, use the corners or mouse wheel, then save.</div>
  </aside>
</div>

Step by step

How to Use

  1. 1
    Drag the photoDrag the image behind the circle to position the face. The crop area itself stays fixed.
  2. 2
    Zoom and rotateUse the mouse wheel or the + and − buttons to zoom, the rotate button for quarter turns, and flip to mirror.
  3. 3
    Watch the previewsThe three circles update live at 96, 56 and 32 pixels as you move the photo.
  4. 4
    Save the cropPress Crop & save to produce a 256 × 256 circular PNG and see its approximate size.
  5. 5
    ResetPress Reset to restore the original position, zoom and orientation.

Real-world uses

Common Use Cases

Profile and team photos
Let users frame their own avatar. For social-media sizes and non-square ratios, see the aspect-ratio preset cropper.
Account settings pages
Replace a raw file input with an editor that produces small, consistent images.
ADMIN
CMS author and staff directories
Enforce one avatar shape and size across a whole site.
Learning canvas export
A concrete example of getCroppedCanvas and why a circle needs a second clipping step.

Got questions?

Frequently Asked Questions

No, it always returns a rectangle. Draw it into a second canvas with a circular clip path to get a truly round, transparent image.

Set dragMode: "move" and disable cropBoxMovable and cropBoxResizable.

Pass a selector to the preview option. Cropper renders the current crop into every matching element automatically.

Pass width and height to getCroppedCanvas. The result is scaled to that size regardless of the source image resolution.

It restricts the crop box to the canvas so the user cannot select an area outside the image.

Cropping in the browser saves upload bandwidth. Still validate and re-process the image server-side, as client output can be altered.

Yes. Use the JSX, Vue, Angular or Tailwind export buttons on this page to convert the markup and styles. The behaviour comes from Cropper.js, so in a framework project install it with npm install cropperjs instead of the CDN tag, create it in useEffect / onMounted / ngAfterViewInit once the image has loaded, and release it with destroy() when the component unmounts.