Image crop

Frame an image in a rectangle or a circle of a fixed aspect, by dragging, pinching, the wheel or the keyboard, and export the framed part at a chosen size as a Blob or a data URL. The piece between ray-file-upload (or an <input type=file>) and ray-avatar.

Source on GitHub

A profile photo
Ada Lovelace
Choose photo Use a sample
Cancel Use photo
A cover in 16:9, with the framing read back
—
Code

API

For AI agents: this API as Markdown, or every component in llms-full.txt.

<ray-image-crop> · RayImageCrop

The image always covers the frame: at zoom 1 it fits the frame's short side, and it cannot be moved so far that a gap shows. The framing is read and set as crop, a rectangle in pixels of the original, so it can be saved, reopened or applied elsewhere. The image is decoded once into a reduced working copy (as many pixels as the output, or the stage on screen, need at max-zoom) and the full-size decode is released; toBlob and toDataURL draw from that copy. EXIF orientation is applied, as in an <img>. Styling: --ray-image-crop-inset (the dimmed margin around the frame, default --ray-space-4), --ray-image-crop-dim (its color) and --ray-image-crop-max-height (default 70vh).

PropertyAttributeTypeDefaultDescription
srcsrcstringThe image: a URL, or (as a property) a File or Blob, such as the one an <input type=file> gives. A cross-origin URL needs CORS to be exportable. A HostFile (the paths of ray-file-upload native) has no bytes: read it in the program and pass a URL.
aspectaspect reflectsstring1Width / height of the frame and of the output: "1", "16/9", "4:3", "1.5".
shapeshape reflectsrect | circlerectThe frame's mask. circle (an ellipse when the aspect is not 1) only masks: the output is still the rectangle, for ray-avatar to round.
zoomzoomnumber11: the image covers the frame; 2: twice as close. Kept between min-zoom and max-zoom; the user changes it.
minZoommin-zoomnumber1The least zoom (1 or more: the image always covers the frame).
maxZoommax-zoomnumber4The most zoom. It also sets how many pixels the working copy keeps.
zoomControlzoom-control reflectsbooleanA zoom slider under the image (the wheel, pinch and + / - zoom in any case).
outputSizeoutput-sizestring512Size of the exported image: a width ("320", the height from the aspect) or "640x360".
typetypestringimage/jpegFormat of toBlob/toDataURL. A browser that cannot encode it gives PNG (Safari has no WebP encoder).
qualityqualitynumber0.850…1, for image/jpeg and image/webp.
labellabelstringCrop image (text image-crop.label)Accessible name of the image area.
zoomLabelzoom-labelstringZoom (text image-crop.zoom)Name of the zoom slider, and the word of the announced zoom ("Zoom 150%").
disableddisabled reflectsbooleanNo moving nor zooming.
EventDescription
inputThe framing moved (every step of a drag, pinch, wheel or the slider).
changeThe framing settled: pointer released, a key pressed, the slider released or the wheel stopped (read crop, zoom).
ray-loadThe image loaded (its original size).
ray-errorThe image could not be loaded or decoded.
PartDescription
stageThe image area (focusable; role slider for the zoom).
imageThe image (a canvas of the working copy).
frameThe frame; what lies outside it is dimmed.
zoomThe zoom ray-slider (with zoom-control).

Texts

What it writes itself, in English; setStrings() translates them (Getting started: Languages).

KeyEnglish
image-crop.labelCrop image
image-crop.zoomZoom