Navigation

v2.1.0
Building

Image adjustments

Crop, turn and tune a picture in the browser with the cropper, and hand on the original file and its adjustments as data.

On this page

A reader chooses a picture, crops it, turns it a quarter at a time and sets its brightness, contrast and saturation, and sees the result as they go. Nothing in the browser changes the picture: what the page hands on is the original file and the adjustments as data, and the application's image pipeline cuts and encodes the picture it stores. This page builds that with the cropper, native inputs and CSS filters.

Image adjustments

<section class="sheet p-lg container-inline" data-example="image-adjustments" aria-labelledby="adjust-title">  <div class="flex flex-wrap items-center justify-between gap-sm mb-md">    <h2 class="m(0) text-md font-medium fg-text-strong" id="adjust-title">Adjust a picture</h2>    <div class="flex flex-wrap items-center gap-xs">      <label class="btn btn-sm">        <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <path d="M7.5892 2.215A0.4996 0.4996 0 0 0 7.5 2.5L7.5006 10.0245A0.5 0.5 0 0 0 8.4946 10.0734L8.5 10L8.4986 2.4621A0.5007 0.5007 0 0 0 8.4108 2.215A0.7044 0.7044 0 0 0 7.5892 2.215Z" fill="currentColor" stroke="none" fill-rule="evenodd" />          <path d="M5 5.5L7.8586 2.6414A0.2 0.2 0 0 1 8.1414 2.6414L11 5.5" />          <path d="M2.5 10.5L2.5 12.5A1 1 0 0 0 3.5 13.5L12.5 13.5A1 1 0 0 0 13.5 12.5L13.5 10.5" />        </svg>        <span data-choose>Choose a picture</span>        <input type="file" class="sr-only" accept="image/*" aria-describedby="adjust-hint">      </label>      <button type="button" class="btn btn-sm btn-quiet" data-clear disabled>Clear</button>    </div>  </div>  <p class="m(0) mb-md text-sm fg-text-muted" id="adjust-hint">The picture stays in this browser. Nothing is uploaded.</p>  <div data-error hidden>    <div class="alert alert-danger mb-md" role="alert">      <div class="alert-body">        <p class="alert-description">That file could not be read as a picture. Choose a PNG, JPG, WebP or AVIF.</p>      </div>    </div>  </div>  <div class="grid gap-lg grid-cols(1) grid-cols(2)@container(--2xl)">    <div class="flex flex-col gap-sm min-w(0)">      <div class="cropper" style="--cropper-x: 0; --cropper-y: 0; --cropper-width: 1; --cropper-height: 1">        <img class="cropper-image" src="/demo/dawn.svg" alt="A pier at dawn" width="640" height="400">        <div class="cropper-box" role="application" aria-roledescription="crop box" tabindex="0" aria-label="Crop the picture">          <span class="cropper-handle cropper-handle-top cropper-handle-left"></span>          <span class="cropper-handle cropper-handle-top"></span>          <span class="cropper-handle cropper-handle-top cropper-handle-right"></span>          <span class="cropper-handle cropper-handle-right"></span>          <span class="cropper-handle cropper-handle-bottom cropper-handle-right"></span>          <span class="cropper-handle cropper-handle-bottom"></span>          <span class="cropper-handle cropper-handle-bottom cropper-handle-left"></span>          <span class="cropper-handle cropper-handle-left"></span>        </div>        <p class="cropper-status" role="status"></p>      </div>      <div class="flex flex-wrap items-center gap-xs">        <button type="button" class="btn btn-sm" data-rotate="-90">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M3.1626 8.6029A4.8333 4.8333 0 0 0 12.5278 9.5552A4.8333 4.8333 0 0 0 4.5522 4.5549L2.653 6.3845A0.5001 0.5001 0 0 1 2.4222 6.492A0.7044 0.7044 0 0 1 1.8413 5.9111A0.4998 0.4998 0 0 1 1.9862 5.6401L3.8478 3.8451Q4.7383 2.9615 5.9133 2.5222Q7.2234 2.0323 8.6132 2.1898Q10.003 2.3473 11.1703 3.1179A5.8276 5.8276 0 0 1 13.4737 9.8799Q13.0195 11.2028 12.0149 12.176A5.8276 5.8276 0 0 1 4.9149 12.9636Q3.7213 12.2343 2.9883 11.0431Q2.3309 9.9747 2.1708 8.7305A0.5 0.5 0 0 1 2.5032 8.1942Q2.6066 8.1862 2.7088 8.1685A0.4998 0.4998 0 0 1 3.1626 8.6029Z" fill="currentColor" stroke="none" fill-rule="evenodd" />            <path d="M2.3333 2.3333L2.3333 5.8A0.2 0.2 0 0 0 2.5333 6L6 6" />          </svg>Rotate left</button>        <button type="button" class="btn btn-sm" data-rotate="90">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M3.1626 8.6029A4.8333 4.8333 0 0 0 12.5278 9.5552A4.8333 4.8333 0 0 0 4.5522 4.5549L2.653 6.3845A0.5001 0.5001 0 0 1 2.4222 6.492A0.7044 0.7044 0 0 1 1.8413 5.9111A0.4998 0.4998 0 0 1 1.9862 5.6401L3.8478 3.8451Q4.7383 2.9615 5.9133 2.5222Q7.2234 2.0323 8.6132 2.1898Q10.003 2.3473 11.1703 3.1179A5.8276 5.8276 0 0 1 13.4737 9.8799Q13.0195 11.2028 12.0149 12.176A5.8276 5.8276 0 0 1 4.9149 12.9636Q3.7213 12.2343 2.9883 11.0431Q2.3309 9.9747 2.1708 8.7305A0.5 0.5 0 0 1 2.5032 8.1942Q2.6066 8.1862 2.7088 8.1685A0.4998 0.4998 0 0 1 3.1626 8.6029Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="translate(16 0) scale(-1 1)" />            <path d="M2.3333 2.3333L2.3333 5.8A0.2 0.2 0 0 0 2.5333 6L6 6" transform="translate(16 0) scale(-1 1)" />          </svg>Rotate right</button>      </div>    </div>    <div class="flex flex-col gap-md min-w(0)">      <div class="well overflow-hidden flex items-center justify-center h(220)" data-stage aria-label="The adjusted picture" role="img"></div>      <div class="segmented segmented-sm" role="radiogroup" aria-label="Show the picture">        <label class="segmented-item">          <input type="radio" class="segmented-input" name="adjust-mode" value="fit" checked>Fit</label>        <label class="segmented-item">          <input type="radio" class="segmented-input" name="adjust-mode" value="fill">Fill</label>        <label class="segmented-item">          <input type="radio" class="segmented-input" name="adjust-mode" value="tile">Tile</label>      </div>      <div class="grid gap-sm">        <div class="field">          <div class="flex items-baseline justify-between">            <label for="adjust-brightness">Brightness</label>            <output class="fg-text-muted" for="adjust-brightness" data-for="brightness">100%</output>          </div>          <input type="range" class="slider" id="adjust-brightness" name="brightness" min="0" max="200" value="100">        </div>        <div class="field">          <div class="flex items-baseline justify-between">            <label for="adjust-contrast">Contrast</label>            <output class="fg-text-muted" for="adjust-contrast" data-for="contrast">100%</output>          </div>          <input type="range" class="slider" id="adjust-contrast" name="contrast" min="0" max="200" value="100">        </div>        <div class="field">          <div class="flex items-baseline justify-between">            <label for="adjust-saturation">Saturation</label>            <output class="fg-text-muted" for="adjust-saturation" data-for="saturation">100%</output>          </div>          <input type="range" class="slider" id="adjust-saturation" name="saturation" min="0" max="200" value="100">        </div>      </div>      <div class="flex flex-wrap gap-xs">        <button type="button" class="btn btn-sm" data-reset>Reset</button>      </div>    </div>  </div>  <details class="mt-lg">    <summary class="text-sm fg-text-strong">What the page hands on</summary> <pre class="code-block mt-sm"><code data-output></code></pre>  </details></section>
Choose a picture of your own, or adjust the sample: crop it, turn it, set its tones, then open what the page hands on.

#Who does what

PartOwns
The cropper and its behaviorThe crop box: its edges, the keys, the pointer, the crop in the picture's own pixels
Native inputsChoosing the file, the ranges of the tones, the radios of the display
Your pageReading the file, the object URL it is shown by, the preview of the adjustments, and the data it hands on
Your applicationCutting, turning, filtering and encoding the picture it stores, and uploading it

The page sets a filter and a transform on its own preview; no Master UI behavior paints a picture. That is the one place in this example where data decides a style, and it stays in the page.

#The file

  • Choosing. A file input, inside a button's label, takes accept="image/*". The picture is decoded with HTMLImageElement.decode() before it is shown, so a file that is not a picture is refused with a message in an alert, and the picture on show stays.
  • Showing. A chosen file is shown by an object URL. Replacing the picture or clearing it releases the old URL with URL.revokeObjectURL(), and so does the function mount() returns when the example stops.
  • Starting over. The cropper reads the picture's size from the width and height of cropper-image, so a new picture sets both from its natural size and the cropper is attached again, on the whole picture.
  • Privacy. The file never leaves the browser; say so beside the button.

#The adjustments

  • Crop. The cropper's value, { x, y, width, height } in the picture's pixels, read on each input and change from the box.
  • Turn. A quarter at a time, left or right, as 0, 90, 180 or 270 degrees. The crop is kept in the picture's own orientation; the preview turns the cropped picture about its middle, so its width and height swap at 90 and 270.
  • Tones. Brightness, contrast and saturation, from 0% to 200%, with 100% as the picture is. CSS has exactly these three as brightness(), contrast() and saturate(), so the preview is what an image pipeline applying the same three would make.
  • Display. Fit shows the whole result in the stage, fill covers the stage with it, and tile repeats it, as a background of that picture would.

Reset puts the tones and the crop back and keeps the picture.

adjustments.json
{  "file": { "name": "dawn.jpg", "type": "image/jpeg", "size": 482113 },  "crop": { "x": 160, "y": 80, "width": 1280, "height": 800 },  "rotate": 90,  "brightness": 110,  "contrast": 100,  "saturation": 85,  "mode": "fit"}

Hand the original File and this object to the application together: it can cut, turn and filter the full-resolution picture the same way, and keep the original to adjust again later.

#What this leaves out

A full photo editor has more than these three tones: color temperature, highlights and shadows, curves, and an exported file. CSS has no filter that is any of them, and an approximation would show the reader a result the stored picture would not have, so this example leaves them out rather than imitate them. Encoding the result in the browser, with a canvas or an OffscreenCanvas, is the application's choice and is not shown here.

#Accessibility

  • The file input is a real input inside a labelled button, reached with Tab; the hint under it says where the picture goes.
  • The crop box has the cropper's keys, and its status region says the crop after each change.
  • Each tone is a labelled range with its value written beside it, and the turns are buttons named by their direction.
  • The preview is named with the picture's own description and "adjusted", such as "A pier at dawn, adjusted"; what was done to it is in the data the page shows, not only in the drawing.