Documentation
¶
Overview ¶
Package mathx provides math-rendering mobjects: LaTeX equations, number lines, 2D coordinate axes, and plotted functions.
Equations are rendered via a pure-Go LaTeX parser (tdewolff/canvas.ParseLaTeX, backed by star-tex.org/x/tex). No external LaTeX installation is required. The parser supports a useful subset of math expressions; complex documents and custom packages are out of scope.
When rendered in sketchy mode (Sloppiness > Architect), equations pick up the active style's roughness — producing a "handwritten math" effect that's unique to this library. The math stays correct (path topology preserved); only the stroke style changes.
Index ¶
- func HighlightTerm(eq *Equation, index int, duration time.Duration) animation.Animation
- func TransformEquation(from, to *Equation, duration time.Duration) animation.Animation
- func Write(eq *Equation, duration time.Duration) animation.Animation
- type Axes
- func (a *Axes) Bounds() geometry.Rect
- func (a *Axes) MoveTo(x, y float64) *Axes
- func (a *Axes) Plot(fn func(float64) float64) *Graph
- func (a *Axes) PointAt(x, y float64) geometry.Point
- func (a *Axes) Position() (float64, float64)
- func (a *Axes) Render(r render.Renderer, ctx style.Context)
- func (a *Axes) Reveal() float64
- func (a *Axes) SetPosition(x, y float64)
- func (a *Axes) SetReveal(t float64)
- func (a *Axes) SetStyle(s style.Style)
- func (a *Axes) SetVisualScale(float64)
- func (a *Axes) Style() *style.Style
- func (a *Axes) VisualBounds() geometry.Rect
- func (a *Axes) WithGrid(show bool) *Axes
- func (a *Axes) WithLabels(show bool) *Axes
- func (a *Axes) WithSize(w, h float64) *Axes
- func (a *Axes) WithSteps(xs, ys float64) *Axes
- type Equation
- func (e *Equation) Bounds() geometry.Rect
- func (e *Equation) MoveTo(x, y float64) *Equation
- func (e *Equation) Position() (float64, float64)
- func (e *Equation) Render(r render.Renderer, ctx style.Context)
- func (e *Equation) Reveal() float64
- func (e *Equation) SetPosition(x, y float64)
- func (e *Equation) SetReveal(t float64)
- func (e *Equation) SetStyle(s style.Style)
- func (e *Equation) SetVisualScale(s float64)
- func (e *Equation) Source() string
- func (e *Equation) Style() *style.Style
- func (e *Equation) Submobjects() []*geometry.Path
- func (e *Equation) Symbol(i int) *geometry.Path
- func (e *Equation) SymbolCount() int
- func (e *Equation) WithColor(c color.Color) *Equation
- func (e *Equation) WithHeight(h float64) *Equation
- func (e *Equation) WithStyle(s style.Style) *Equation
- type Graph
- func (g *Graph) Bounds() geometry.Rect
- func (g *Graph) Children() []mobject.Mobject
- func (g *Graph) Render(r render.Renderer, ctx style.Context)
- func (g *Graph) Reveal() float64
- func (g *Graph) Seed() int64
- func (g *Graph) SetReveal(t float64)
- func (g *Graph) SetStyle(s style.Style)
- func (g *Graph) SetVisualScale(float64)
- func (g *Graph) Style() *style.Style
- func (g *Graph) WithColor(c color.Color) *Graph
- func (g *Graph) WithRange(xMin, xMax float64) *Graph
- func (g *Graph) WithSamples(n int) *Graph
- func (g *Graph) WithSeed(s int64) *Graph
- type NumberLine
- func (n *NumberLine) AddPoint(value float64, labelText string) geometry.Point
- func (n *NumberLine) Bounds() geometry.Rect
- func (n *NumberLine) MoveTo(x, y float64) *NumberLine
- func (n *NumberLine) PointAt(value float64) geometry.Point
- func (n *NumberLine) Position() (float64, float64)
- func (n *NumberLine) Render(r render.Renderer, ctx style.Context)
- func (n *NumberLine) Reveal() float64
- func (n *NumberLine) SetPosition(x, y float64)
- func (n *NumberLine) SetReveal(t float64)
- func (n *NumberLine) SetStyle(s style.Style)
- func (n *NumberLine) SetVisualScale(float64)
- func (n *NumberLine) Style() *style.Style
- func (n *NumberLine) VisualBounds() geometry.Rect
- func (n *NumberLine) WithLabels(values ...float64) *NumberLine
- func (n *NumberLine) WithLength(px float64) *NumberLine
- func (n *NumberLine) WithStep(s float64) *NumberLine
- type Shade
- func (s *Shade) Bounds() geometry.Rect
- func (s *Shade) Render(r render.Renderer, ctx style.Context)
- func (s *Shade) Reveal() float64
- func (s *Shade) Seed() int64
- func (s *Shade) SetReveal(t float64)
- func (s *Shade) SetStyle(st style.Style)
- func (s *Shade) SetVisualScale(float64)
- func (s *Shade) Style() *style.Style
- func (s *Shade) WithSamples(n int) *Shade
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HighlightTerm ¶
HighlightTerm flashes a specific submobject of an equation by index. Useful for "look at the m in E=mc^2." Index is 0-based; out-of-range indices are no-ops.
Current limitation: highlight pulses the equation's overall scale via SetReveal. True per-symbol highlighting requires per-symbol position/style state and is not yet implemented.
func TransformEquation ¶
TransformEquation morphs one equation into another. Phase-4 approximation: cross-fade between the two — `from` fades out while `to` fades in. A proper symbol-matching morph (where shared symbols reposition rather than fade) is a Phase-5 follow-up.
Both equations must be added to the scene; the caller is responsible for that.
func Write ¶
Write animates an equation as if being handwritten: each symbol fades in with a small stagger, so glyphs appear left-to-right rather than the entire equation popping in at once. Conceptually like DrawOn for math.
Implementation: rather than truly drawing each glyph stroke-by-stroke (which is a Phase-5 nicety), we reveal the equation as a whole using the reveal fraction. The animation duration is split: nothing visible for first 10%, then linear reveal to 100% at end.
Types ¶
type Axes ¶
Axes is a 2D coordinate plane: two perpendicular number lines with tick marks. Used as the parent of Graph mobjects.
func NewAxes ¶
NewAxes constructs a 2D coordinate plane covering the given ranges. Pixel dimensions default to 800×500; override with WithSize.
func (*Axes) Plot ¶
Plot constructs a Graph of f over the axes. Default range is the axes' xMin..xMax; override with Graph.WithRange.
func (*Axes) SetPosition ¶
func (*Axes) SetVisualScale ¶
func (*Axes) VisualBounds ¶
func (*Axes) WithLabels ¶
type Equation ¶
Equation is a math formula rendered from LaTeX source.
In Architect (crisp) mode, the equation is drawn as clean filled glyph paths. In Artist or Cartoonist mode, the same paths are stroked with the active sloppiness — producing the signature "handwritten math" look.
eq := mathx.NewEquation("E = mc^2").WithHeight(80)
scene.Add(eq)
Sub-symbols are addressable by index for fine-grained animation. Indices correspond to glyph order in the LaTeX source (left-to-right, no semantic awareness).
func NewEquation ¶
NewEquation constructs an equation from LaTeX source. Default height is 80px; override with WithHeight.
func (*Equation) Render ¶
Render draws the equation at its current position with the resolved style. In crisp mode each glyph is filled; in sketchy mode each glyph is stroked with the active roughness (handwritten math).
func (*Equation) SetPosition ¶
SetPosition is the imperative form of MoveTo.
func (*Equation) SetReveal ¶
SetReveal sets the reveal fraction (0..1). Used by Write and FadeIn/Out animations.
func (*Equation) SetVisualScale ¶
SetVisualScale — equations expose VisualScale via SetReveal mapping (no separate scale support yet). Implementing the Scaler interface lets PopIn target equations.
func (*Equation) Submobjects ¶
Submobjects returns one path per visual glyph (approximately). Subpaths are clustered by bounding-box geometry so closed counters and accents stay merged with their letter body. Used by Write and TransformEquation animations.
func (*Equation) SymbolCount ¶
SymbolCount returns the number of addressable submobjects.
func (*Equation) WithHeight ¶
WithHeight sets the equation's pixel height and recompiles.
type Graph ¶
Graph is a plotted function over an Axes. It samples the function at `samples` points across [xMin, xMax], smooths them into a cubic Bezier path via Catmull-Rom tangents, and renders with the active style.
Three behaviors keep the curve looking like part of the chart:
- Boundary clipping interpolates to the actual yMin/yMax crossing instead of dropping the last in-range sample — so the curve meets the chart edge cleanly.
- Discontinuities (Inf/NaN) start a new subpath, so the line breaks where the function is undefined rather than connecting across the jump.
- When the active style is rough (Roughness > 0) the curve's Bezier control points are jittered perpendicular to the local segment, scaled by the style's MaxJitter. The path stays continuous (no per-segment MoveTo) so the curve reads as one drawn line, matching the rough axes around it.
func (*Graph) SetVisualScale ¶
func (*Graph) WithSamples ¶
type NumberLine ¶
NumberLine renders a horizontal axis from min to max with tick marks at every `step` value and optional numeric labels under selected ticks.
func NewNumberLine ¶
func NewNumberLine(min, max float64) *NumberLine
NewNumberLine constructs a number line from min to max with step=1.
func (*NumberLine) AddPoint ¶
func (n *NumberLine) AddPoint(value float64, labelText string) geometry.Point
AddPoint marks a value with a filled dot and a label. Returns the dot's center so the caller can attach further elements.
func (*NumberLine) Bounds ¶
func (n *NumberLine) Bounds() geometry.Rect
func (*NumberLine) MoveTo ¶
func (n *NumberLine) MoveTo(x, y float64) *NumberLine
func (*NumberLine) PointAt ¶
func (n *NumberLine) PointAt(value float64) geometry.Point
PointAt converts a value on the number line to a scene coordinate.
func (*NumberLine) Position ¶
func (n *NumberLine) Position() (float64, float64)
func (*NumberLine) Reveal ¶
func (n *NumberLine) Reveal() float64
func (*NumberLine) SetPosition ¶
func (n *NumberLine) SetPosition(x, y float64)
func (*NumberLine) SetReveal ¶
func (n *NumberLine) SetReveal(t float64)
func (*NumberLine) SetStyle ¶
func (n *NumberLine) SetStyle(s style.Style)
func (*NumberLine) SetVisualScale ¶
func (n *NumberLine) SetVisualScale(float64)
func (*NumberLine) Style ¶
func (n *NumberLine) Style() *style.Style
func (*NumberLine) VisualBounds ¶
func (n *NumberLine) VisualBounds() geometry.Rect
func (*NumberLine) WithLabels ¶
func (n *NumberLine) WithLabels(values ...float64) *NumberLine
WithLabels picks specific values to receive numeric labels under their tick. If empty, every step gets a label.
func (*NumberLine) WithLength ¶
func (n *NumberLine) WithLength(px float64) *NumberLine
func (*NumberLine) WithStep ¶
func (n *NumberLine) WithStep(s float64) *NumberLine
type Shade ¶
Shade fills the region between a Graph curve and the axes' x-axis (or yMin, whichever is in range) across [xMin, xMax]. Used to mark areas under a probability distribution, integrals, etc.
Reveal grows the shaded region from left to right by clipping the x-range — useful for animating the tail of a bell curve filling in.
func NewShade ¶
NewShade builds a fill region under g across [xMin, xMax]. The region is bounded above by g.fn and below by y=0 (clamped to the axes' y-range).