Geometric Series Builder
Visualizes the geometric series a + a·r + a·r² + a·r³ + ... as a vertical row of horizontal bars, one per term. Each bar's width is proportional to the term's magnitude, so a series with absolute ratio below 1 shows bars shrinking toward zero while the partial sum approaches the limit. Diverging series make the same visualization show why the sum never settles. The component is the foundation primitive for series convergence, recurrence-relation work, and Master-Theorem-style Big-O analysis.
Geometric series with a = 1 and r = 0.5. Showing 6 of 12 terms. Running sum is 1.969, limit 2.
a + a·r + a·r² + …term 06 / 12 · converges
1
0.5
0.25
0.125
0.0625
0.03125
a = 1r = 0.5Sₙ = 1.969S∞ = 2
Customize
Series
1.00
0.50
6
Display
Installation
npx shadcn@latest add https://craftbits.dev/r/geometric-series-builder.jsonUsage
import { GeometricSeriesBuilder } from "@craft-bits/core";
<GeometricSeriesBuilder a={1} r={0.5} defaultPlaying />Drive the term count from outside the component:
const [n, setN] = useState(1);
<GeometricSeriesBuilder
a={1}
r={0.5}
n={n}
onNChange={setN}
/>Show a divergent series (no limit line):
<GeometricSeriesBuilder a={1} r={1.4} maxN={8} />Understanding the component
- One bar per term. Bar k has width proportional to the magnitude of the k-th term. The scale is fixed across the first
maxNterms, so bars only ever shrink (or grow) into the same axis — they never re-scale around the user mid-build. - Running sum, animated. The closed-form partial sum is rendered as a separate readout. Whenever the term count, first term, or ratio changes, the sum springs to its new value via
SPRINGS.snap. - Limit line, only when it makes sense. When the absolute ratio is below 1 and
showLimitis on, a dotted vertical reference line is drawn at the convergence limit. For divergent series the line is suppressed and the header reads "diverges" instead of "converges". - Negative ratios flip color. When a term is negative, its bar takes the
cb-errorborder and label color. The width still uses magnitude so the bar remains visible. - Autoplay uses
setInterval. Each tick adds one term until the count hitsmaxN, then autoplay disables itself. The effect cleans up on unmount, pause, or term-count change — no orphaned timers. - Controlled and uncontrolled twice over.
nandplayingeach ship avalue+onChangepair for controlled use anddefault*for uncontrolled. - Reduced motion is honoured. With
prefers-reduced-motion: reduce, every bar / sum spring collapses to instant, and the play button is disabled.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
a | number | 1 | First term. |
r | number | 0.5 | Common ratio. Series converges when the absolute value is less than 1. |
n | number | — | Controlled visible-term count. |
defaultN | number | 6 | Uncontrolled initial visible-term count. |
maxN | number | 12 | Hard cap on autoplay growth and the scrubber range. |
playing | boolean | — | Controlled play state. |
defaultPlaying | boolean | false | Uncontrolled initial play state. |
playSpeed | number | 600 | Milliseconds between autoplay terms. |
showLimit | boolean | true | Show the dotted reference line at the convergence limit. No effect when the series diverges. |
transition | Transition | SPRINGS.smooth | Spring for the bar widths. The sum readout always uses SPRINGS.snap. |
className | string | — | Merged onto the root <div> via cn(). |
Accessibility
- The whole figure is
role="figure"witharia-labelledbypointing at the formula header. - An
aria-live="polite"summary describesa,r, the visible-term count, the running sum, and the limit (or "diverges"). It re-announces whenever any of those change. - Step controls, the play / pause button, and the term-count scrubber each carry an explicit
aria-label. - The play button uses
aria-pressedso screen readers track the toggle state. It is disabled for users who prefer reduced motion. - Animation respects
prefers-reduced-motion: reduce— every bar and sum transition collapses to instant, and autoplay is short-circuited.
Credits
- Extracted from:
algoflashcards(src/lessons/primitives/viz/GeometricSeriesBuilder.tsx). Generalized the lesson primitive into a math-viz teaching component: replaced the bespoke fractions-of-n API with a purea+rparameterization, added controlled / uncontrolled state pairs fornandplaying, added setInterval-driven autoplay with cleanup, replaced inline springs withSPRINGS.*tokens, replaced hex-suffix track fills withcb-accent-muted/cb-errortoken classes, and added an automatic limit-line whose visibility flips with the convergence criterion.