Try 'magnetic button'
View code

Number 71: Lens over a product photo

Specification

Catalogue
No. 71 of 110
Interaction
Hover
Element
Media
Preview
Live component
Imports
react 19.3.1
Added

Notes

A round lens follows the pointer and magnifies that spot, while the whole photo stays in view.

The visitor can check a seam or a grain and still see the whole item around it. It shows only while the hand is there, and works by keyboard with the arrow keys.

2.6 times magnification in a 132px lens, follows the pointer with no easing. Phone: press and hold to look, let go to hide. Keyboard: arrows move it, Shift for big steps, Escape hides. Unlike the zoom piece, the rest of the photo stays visible around the lens.

The names, figures and pictures in the demo are sample content; a real build uses the client's own.

Code

React and plain CSS
"use client";

import { useEffect, useRef, useState, type CSSProperties, type KeyboardEvent, type PointerEvent } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "./lens.css";

const S = "/samples";

const LENS_ZOOM = 2.6;

function LensDemo() {
  const frame = useRef<HTMLDivElement>(null);
  const [at, setAt] = useState<{ x: number; y: number } | null>(null);
  const [pinned, setPinned] = useState(false);
  const [size, setSize] = useState({ w: 0, h: 0 });

  useEffect(() => {
    const el = frame.current;
    if (!el) return;
    const ro = new ResizeObserver(() => setSize({ w: el.clientWidth, h: el.clientHeight }));
    ro.observe(el);
    return () => ro.disconnect();
  }, []);

  const fromEvent = (e: PointerEvent<HTMLDivElement>) => {
    const r = e.currentTarget.getBoundingClientRect();
    return { x: Math.min(1, Math.max(0, (e.clientX - r.left) / r.width)), y: Math.min(1, Math.max(0, (e.clientY - r.top) / r.height)) };
  };
  const onKey = (e: KeyboardEvent<HTMLDivElement>) => {
    const step = e.shiftKey ? 0.1 : 0.03;
    const moves: Record<string, [number, number]> = { ArrowLeft: [-step, 0], ArrowRight: [step, 0], ArrowUp: [0, -step], ArrowDown: [0, step] };
    if (e.key === "Escape") {
      setAt(null);
      setPinned(false);
      return;
    }
    const m = moves[e.key];
    if (!m) return;
    e.preventDefault();
    const cur = at ?? { x: 0.5, y: 0.5 };
    setAt({ x: Math.min(1, Math.max(0, cur.x + m[0])), y: Math.min(1, Math.max(0, cur.y + m[1])) });
    setPinned(true);
  };

  const D = 132; // lens diameter in px
  const lensStyle: CSSProperties | undefined = at
    ? {
        left: at.x * size.w - D / 2,
        top: at.y * size.h - D / 2,
        width: D,
        height: D,
      }
    : undefined;
  const innerStyle: CSSProperties | undefined = at
    ? {
        width: size.w,
        height: size.h,
        transform: `translate(${D / 2 - at.x * size.w * LENS_ZOOM}px, ${D / 2 - at.y * size.h * LENS_ZOOM}px) scale(${LENS_ZOOM})`,
      }
    : undefined;

  return (
    <div className="dm">
      <div
        ref={frame}
        className="lens-frame"
        tabIndex={0}
        role="group"
        aria-label="Leather tote. Arrow keys move the lens, Escape hides it"
        onPointerMove={(e) => {
          if (e.pointerType === "mouse" || e.buttons) setAt(fromEvent(e));
        }}
        onPointerDown={(e) => {
          setAt(fromEvent(e));
          if (e.pointerType !== "mouse") e.currentTarget.setPointerCapture(e.pointerId);
        }}
        onPointerLeave={(e) => {
          if (e.pointerType === "mouse" && !pinned) setAt(null);
        }}
        onPointerUp={(e) => {
          if (e.pointerType !== "mouse") setAt(null);
        }}
        onKeyDown={onKey}
        onBlur={() => {
          setPinned(false);
          setAt(null);
        }}
      >
        <img src={`${S}/tote.svg`} alt="A tan leather tote with saddle stitching, brass rivets and an embossed label" draggable={false} />
        {at && (
          <div className="lens" style={lensStyle} aria-hidden="true">
            <img src={`${S}/tote.svg`} alt="" style={innerStyle} draggable={false} />
          </div>
        )}
      </div>
      <p className="dm-hint">Point, or press and hold on a phone, to look closer at the stitching</p>
    </div>
  );
}

// What a hovered card shows: the piece used the way a visitor would.
const TOUR: Tour = [
  { sweep: ".lens-frame", from: [0.2, 0.35], to: [0.75, 0.6], ms: 2200, wait: 200 },
  { sweep: ".lens-frame", from: [0.75, 0.6], to: [0.45, 0.45], ms: 1200, wait: 600 },
];

export default function LensEntry(props: LivePreviewProps) {
  return (
    <PieceStage {...props} label="Lens over a product photo" sample tour={TOUR}>
      <LensDemo />
    </PieceStage>
  );
}

More like this

Image panels that widen

Hover, Layout AnimationMedia and galleries

Product you can turn

DragMedia and galleries

Bookshelf

Press, Layout AnimationMedia and galleries

One crop, three shapes, same area

Layout AnimationMedia and galleries

Photo grid that opens into the photo

Layout AnimationMedia and galleries

Zoom into a photo where you point

PressMedia and galleries