Photo focus and zoom

Try it

Your own photo stays on your device.

The same numbers in other frames, plain CSS

<goh-photo-focus> lets someone choose which part of a photo shows, and how close. They drag, pinch or use three sliders, and the tag reports three numbers. It never changes or uploads the photo. Saving is up to your site.

<script type="module" src="photo-focus-x.y.z.js"></script>
<goh-photo-focus src="photo.jpg" alt="Two children planting a tree" x="0.5" y="0.3" zoom="1.4"></goh-photo-focus>
const tag = document.querySelector('goh-photo-focus');
tag.addEventListener('photofocus', (e) => { latest = e.detail; }); // { x, y, zoom }, on every move: save it later, not here

Show the result

No script needed. The numbers are CSS:

.frame { overflow: hidden; }
.frame img { width: 100%; height: 100%; object-fit: cover;
  object-position: 50% 30%; transform-origin: 50% 30%; scale: 1.4; } /* x, y as percentages, then zoom */

Settings

Attribute Default What it does
src, alt The photo and its text alternative
x, y 0.5 Focus, 0 to 1
zoom 1 1 just fills the frame
max-zoom 4 The closest zoom
disabled Shows the photo but stops changes
label-x, label-y, label-zoom English Slider labels
frame-class, image-class, label-class, text-class, slider-class, output-class Your class names, added to each piece
Property What it gives
value { x, y, zoom }. Set it too, whole or in part; bad numbers are ignored.
crop The visible part in the photo's own pixels, { x, y, width, height }. Read only, null until the photo loads.
Event When
photofocus Someone changed it: it fires all through a drag. detail is the value. Setting it from code fires nothing.
photoerror The photo could not load.

The exact meaning of the saved numbers is in schema.json.

Styling

The tag's pieces are in your page, not sealed off, so your fonts, rules and classes apply. Every piece also has a fixed class: goh-photo-focus-frame, -image, -label, -text, -slider and -output.

Good to know