Try 'magnetic button'
View code

Number 73: One crop, three shapes, same area

Specification

Catalogue
No. 73 of 110
Interaction
Layout Animation
Element
Media, Picker
Preview
Live component
Imports
react 19.3.1, @number-flow/react 0.6.2
Added

Notes

A photo switches between landscape, square and portrait while keeping the same area, so it never looks smaller in one shape than another.

Upload flows, product image settings and post schedulers ask for a shape. Fixing the width and letting the height change makes portrait look huge and landscape look small; keeping the area equal makes the choice about shape alone. It picks the shape where the focal point entry picks the subject.

The frames are 276 by 207, 239 by 239 and 207 by 276, within 0.02% of the same area, and all fit one 276px square, so nothing around them moves. Width and height change over 520ms cubic-bezier(.22, 1, .36, 1), never a scale, so the photo is re-cropped by object-fit: cover on every frame instead of squashed. The shape control is a radio group with arrow keys, 44px tall, and its thumb slides on 55% of the frame's time. Under reduced motion the shape still changes over 160ms, because the move shows which side was cropped.

The photograph is sample content.

Code

React and plain CSS
"use client";

import { useState, type CSSProperties } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import { radioArrows } from "@/components/pieces/shared";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "./crop-shapes.css";

/** Three frames of the same area (within 0.02%), all inside one 276px square, so the block never resizes mid-move. */
const SHAPES = [
  { id: "landscape", label: "Landscape", ratio: "4:3", w: 276, h: 207 },
  { id: "square", label: "Square", ratio: "1:1", w: 239, h: 239 },
  { id: "portrait", label: "Portrait", ratio: "3:4", w: 207, h: 276 },
] as const;

function CropShapesDemo() {
  const [at, setAt] = useState(1);
  const shape = SHAPES[at]!;
  return (
    <div className="dm crop" style={{ "--crop-at": at } as CSSProperties}>
      <div className="crop-stage">
        {/* Width and height move, never a scale, so the photo is re-cropped on every frame instead of squashed. */}
        <div className="crop-frame" style={{ width: shape.w, height: shape.h }}>
          <img src="/samples/studio-shelf.svg" alt="Rust and bone glazed pots on a shelf" draggable={false} />
        </div>
      </div>
      <div className="crop-tabs" role="radiogroup" aria-label="Aspect ratio" onKeyDown={radioArrows}>
        <span className="crop-thumb" aria-hidden="true" />
        {SHAPES.map((s, i) => (
          <button key={s.id} type="button" role="radio" aria-checked={i === at} aria-label={`${s.label}, ${s.ratio}`} tabIndex={i === at ? 0 : -1} className="crop-tab" onClick={() => setAt(i)}>
            <svg viewBox="0 0 18 18" width="18" height="18" aria-hidden="true">
              <rect x={(18 - s.w / 18) / 2} y={(18 - s.h / 18) / 2} width={s.w / 18} height={s.h / 18} rx="2" fill="none" stroke="currentColor" strokeWidth="1.5" />
            </svg>
            <span>{s.ratio}</span>
          </button>
        ))}
      </div>
    </div>
  );
}

// What a hovered card shows: square to portrait, then to landscape.
const TOUR: Tour = [
  { click: ".crop-tab", nth: 2, wait: 900 },
  { click: ".crop-tab", nth: 0, wait: 900 },
];

export default function CropShapesEntry(props: LivePreviewProps) {
  return (
    <PieceStage {...props} label="One crop, three shapes, same area" tour={TOUR} sample>
      <CropShapesDemo />
    </PieceStage>
  );
}

More like this

Bookshelf

Press, Layout AnimationMedia and galleries

Image panels that widen

Hover, Layout AnimationMedia and galleries

Photo grid that opens into the photo

Layout AnimationMedia and galleries

Card stack that deals the next picture

Press, Layout AnimationMedia and galleries

Product you can turn

DragMedia and galleries

Option cards with a sliding ring

Press, Layout AnimationPickers and booking