Skip to content
English
Shape fitting comes with Pro. Its preview shows once you unlock it.

How it works

A textbook figure of shape fitting on a centimetre grid. The hand-drawn outline stays in grey; the fitted shape is drawn over it in the accent with a soft fill. The ellipse and the circle have the same area, centre and second moments as the closed sketch; the triangle is the largest one inside the sketch's convex hull, scaled to the same area. In Auto the closer of triangle and ellipse wins, judged by the RMS distance of the sketch from the shape's outline. The triangle's corners are named A, B and C, the ellipse's semi-axes a and b, the circle's radius r. The row under the grid reads the area, and four measurements follow: sides, angles and perimeter; semi-axes, rotation and eccentricity; or radius, diameter and circumference; with the RMS error for each.

Use it for

  • Teach what fitting means: a model shape that is as close as possible to imperfect data, with its error shown next to it.
  • Geometry exercises where learners sketch a triangle or an ellipse and read its angles, axes or area without measuring by hand.

Look elsewhere when

  • For drawing precise shapes for a design, use a shape tool with handles: the fit always follows the sketch.

Usage

import React, { useState } from 'react'
import { ContourPress } from './ContourPress.jsx'

// Points are centimetres on the 24 × 11 cm grid, origin top left
const sketch = [[12.5, 2], [19, 9], [5.5, 8.8]]

export function GeometryExercise() {
  const [points, setPoints] = useState(sketch)
  const [fit, setFit] = useState(null)
  return (
    <ContourPress
      value={points}
      onChange={(next, details) => {
        setPoints(next)
        setFit(details.fit)
      }}
      labels={{ title: 'Fit a triangle to a sketch' }}
    />
  )
}

Props

PropTypeDefaultWhat it does
valueArray<[x: number, y: number]>—Controlled sketch: the outline's points in centimetres on the 24 × 11 grid, origin top left. The outline is closed from the last point back to the first.
defaultValueArray<[x: number, y: number]>[]Starting sketch when uncontrolled.
onChange(points, { reason, fit }) => void—Called when a sketch is finished or cleared. reason is 'draw', 'keyboard' or 'clear'; fit is null or { type, area, error } plus sides, angles and perimeter (triangle), semiAxes, rotation and eccentricity (ellipse) or radius and circumference (circle), all in centimetres and degrees.
mode'auto' | 'triangle' | 'ellipse' | 'circle'—Controlled fit mode. 'auto' keeps whichever of the triangle and the ellipse lies closer to the sketch.
defaultMode'auto' | 'triangle' | 'ellipse' | 'circle''auto'Starting fit mode when uncontrolled.
onModeChange(mode) => void—Called when a fit mode is chosen.
holdMsnumber400How long the pointer must rest while drawing before the fit is previewed (160–1500 ms). Lifting fits in any case.
labelsobject—Replaces any built-in text: title, sketch, fitted, grid ('{unit}'), unit, canvas, help, empty, keys, mode, modeAuto, modeTriangle, modeEllipse, modeCircle, clear, readoutTriangle, readoutEllipse, readoutCircle, readoutEmpty, readoutSmall, sides, angles, perimeter, semiAxes, rotation, eccentricity, radius, diameter, circumference, error, pointAdded ('{n}', '{x}', '{y}'), pointRemoved ('{n}'), fittedText ('{shape}', '{area}', '{error}') and cleared.
localestring'en'Language tag for the decimal readouts.
classNamestring—Extra class on the card, for example to set its width (30rem by default, never wider than its container).

Accessibility

  • The grid is an application region with its instructions as its description. With the keyboard, arrows move a cross-hair by 0.5 cm (Shift: 2 cm), Space adds a point, Enter closes the outline and fits it, Backspace removes the last point and Escape drops the points. A hint with these keys shows while the grid has keyboard focus.
  • The fit mode is a radio group: Tab reaches the chosen mode, arrows move and select. Clear is a button, marked unavailable while the grid is empty.
  • A polite live region announces each added point and each fit with its area and error. The measurements are a description list under the grid.
  • Drawing uses pointer capture and touch-action none on the grid only; a cancelled gesture or a lost window focus drops the stroke and keeps the previous sketch. A tap changes nothing.

Motion

While the pointer rests for holdMs a dashed preview of the fit appears; lifting fills it in, and a new fit fades in over --ds-duration-base. With reduced motion every change lands at once.

Add it to your project

Pro members copy a prompt and their coding agent installs the source, the same way as the free components.

ContourPress.jsxcontour-press.csstokens.cssREADME.md

Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.

Form inputs
Availability grid comes with Pro. Its preview shows once you unlock it.
Boolean operations comes with Pro. Its preview shows once you unlock it.
Cascader comes with Pro. Its preview shows once you unlock it.