Derivative Explorer

Plot one of seven canonical functions and drag a marker along x. The tangent line at the marker is drawn through the point with slope f'(x); the analytical derivative is surfaced both on the chart and in the readout below. An optional secant overlay teaches the limit-of-secants definition of the derivative.

y = x²f(1.00) = 1.000 f'(x) = 2.000
-2-10123456789101112131415161718-4-3-2-101234slope = 2.00f(x)tangent
x = 1.000f'(x) = 2.000
Customize
Function
x^2
Position
1.00
Secant
0.50

Installation

npx shadcn@latest add https://craftbits.dev/r/derivative-explorer.json

Usage

import { DerivativeExplorer } from "@craft-bits/core";
 
<DerivativeExplorer defaultActiveFunction="x^2" defaultX={1} />

Control x and the active function from outside:

const [x, setX] = useState(1);
const [fn, setFn] = useState("sin(x)");
 
<DerivativeExplorer
  activeFunction={fn}
  onActiveFunctionChange={setFn}
  x={x}
  onXChange={setX}
  range={[-3, 3]}
/>

Show the secant line to teach the limit definition:

<DerivativeExplorer
  defaultActiveFunction="x^2"
  defaultX={1}
  showSecant
  secantEpsilon={0.5}
/>

Understanding the component

  1. Seven built-in functions, each with analytical derivative. , , sin(x), cos(x), , ln(x), 1/x. The derivative is the exact closed form — no finite-difference approximation — so the tangent reflects the true gradient at any x.
  2. Auto-scaled y-axis. The plot pre-samples f(x) across the visible domain and grows the y extents to fit; y = 0 is always kept inside the frame.
  3. Tangent line spans the visible domain. Drawn as a single segment from (xMin, f(x₀) + f'(x₀)(xMin − x₀)) to the equivalent at xMax. Dragging x slides the tangent's pivot along the curve.
  4. Secant overlay. When showSecant is on, a dashed line connects (x, f(x)) to (x + ε, f(x + ε)). The readout surfaces both the secant slope and f'(x) so convergence as ε shrinks is observable.
  5. Spring transitions. Function changes use SPRINGS.smooth; the tangent / marker follows pointer with SPRINGS.snap. prefers-reduced-motion: reduce collapses both to duration: 0.
  6. Draggable x. A capture-anywhere transparent rect drives setPointerCapture so dragging keeps tracking when the pointer leaves the plot. Arrows nudge 1% of the range; Shift+Arrow nudges 5%; Home / End jump to the edges.
  7. Controlled and uncontrolled, twice. Both activeFunction and x can be controlled (pair with onActiveFunctionChange / onXChange) or left to internal state via defaultActiveFunction / defaultX.

Props

PropTypeDefaultDescription
functionsreadonly FunctionDef[]seven built-insSelectable function list.
activeFunctionstringControlled active function (by name).
defaultActiveFunctionstringfirst entry of functionsUncontrolled initial active function.
onActiveFunctionChange(name: string) => voidFires on every function change.
xnumberControlled x position.
defaultXnumbermidpoint of rangeUncontrolled initial x.
onXChange(x: number) => voidFires on every drag / keyboard nudge.
rangereadonly [number, number][-4, 4]Visible x-axis domain.
showSecantbooleanfalseOverlay the dashed secant line.
secantEpsilonnumber0.5Horizontal offset for the secant's second sample.
transitionTransitionSPRINGS.snapSpring for tangent / marker follow.
classNamestringMerged onto the root <div> via cn().

Accessibility

  • The chart is role="img" with an aria-labelledby heading that names the active function.
  • The drag surface is role="slider" with aria-valuemin / aria-valuemax / aria-valuenow reflecting x.
  • An aria-live="polite" summary above the chart reads out f(x) and f'(x) whenever the marker or function changes.
  • Keyboard: / nudge 1% of the range, Shift+← / Shift+→ nudge 5%, Home jumps to range[0], End to range[1].
  • The function picker is a role="radiogroup" of role="radio" buttons, each tagged data-state="on" | "off".
  • Animation respects prefers-reduced-motion: reduce — all springs collapse to an instant swap.

Credits

  • Extracted from: craftingattention (app/src/lessons/primitives/math/DerivativeExplorer.tsx). Stripped the lesson-specific Explore / Predict / Challenge mode strip, narration heuristics, bookmark presets, and undo / redo history; generalized to a single primitive with controlled / uncontrolled state pairs and an extensible functions list.