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
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.jsonUsage
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
- Seven built-in functions, each with analytical derivative.
x²,x³,sin(x),cos(x),eˣ,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. - Auto-scaled y-axis. The plot pre-samples
f(x)across the visible domain and grows the y extents to fit;y = 0is always kept inside the frame. - Tangent line spans the visible domain. Drawn as a single segment from
(xMin, f(x₀) + f'(x₀)(xMin − x₀))to the equivalent atxMax. Dragging x slides the tangent's pivot along the curve. - Secant overlay. When
showSecantis on, a dashed line connects(x, f(x))to(x + ε, f(x + ε)). The readout surfaces both the secant slope andf'(x)so convergence as ε shrinks is observable. - Spring transitions. Function changes use
SPRINGS.smooth; the tangent / marker follows pointer withSPRINGS.snap.prefers-reduced-motion: reducecollapses both toduration: 0. - Draggable x. A capture-anywhere transparent rect drives
setPointerCaptureso dragging keeps tracking when the pointer leaves the plot. Arrows nudge 1% of the range;Shift+Arrownudges 5%;Home/Endjump to the edges. - Controlled and uncontrolled, twice. Both
activeFunctionandxcan be controlled (pair withonActiveFunctionChange/onXChange) or left to internal state viadefaultActiveFunction/defaultX.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
functions | readonly FunctionDef[] | seven built-ins | Selectable function list. |
activeFunction | string | — | Controlled active function (by name). |
defaultActiveFunction | string | first entry of functions | Uncontrolled initial active function. |
onActiveFunctionChange | (name: string) => void | — | Fires on every function change. |
x | number | — | Controlled x position. |
defaultX | number | midpoint of range | Uncontrolled initial x. |
onXChange | (x: number) => void | — | Fires on every drag / keyboard nudge. |
range | readonly [number, number] | [-4, 4] | Visible x-axis domain. |
showSecant | boolean | false | Overlay the dashed secant line. |
secantEpsilon | number | 0.5 | Horizontal offset for the secant's second sample. |
transition | Transition | SPRINGS.snap | Spring for tangent / marker follow. |
className | string | — | Merged onto the root <div> via cn(). |
Accessibility
- The chart is
role="img"with anaria-labelledbyheading that names the active function. - The drag surface is
role="slider"witharia-valuemin/aria-valuemax/aria-valuenowreflecting x. - An
aria-live="polite"summary above the chart reads outf(x)andf'(x)whenever the marker or function changes. - Keyboard:
←/→nudge 1% of the range,Shift+←/Shift+→nudge 5%,Homejumps torange[0],Endtorange[1]. - The function picker is a
role="radiogroup"ofrole="radio"buttons, each taggeddata-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 extensiblefunctionslist.