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.
- Frame shape:
--goh-photo-focus-aspect: 16 / 9, or anaspect-ratiorule on the frame. - Our own rules are few and sit in a CSS layer called
goh-parts, so any ordinary rule of yours wins. If your own CSS is in layers (Tailwind 4 is), put this first in it:@layer goh-parts, theme, base, components, utilities; - The photo's size and position are fixed inline, so a theme's usual
imgrules don't break them. Colours, borders on the frame, corners and filters are yours. Leave padding off the photo.
Good to know
- Keyboard: Tab to a slider, then the arrow keys. Every slider has a name and says its value.
- The tag fills itself: leave it empty in your HTML. Anything put inside it later is not cleaned up.
- Scrolling: on a touch screen, dragging on the photo moves the photo, not the page. With a mouse, the wheel scrolls the page; Ctrl or Cmd with the wheel zooms.
- Strict CSP: works with no
unsafe-inline. - React and other server rendering: load the script after hydration, for example from an effect. The tag fills itself with HTML, and doing that earlier causes a mismatch warning.
- Browsers: Chrome and Edge 104, Firefox 101, Safari 16.4 or newer (from browser support tables, not tested that far back).
- Not handled yet: right-to-left layouts.