Topic

Drawing and labels

The library computes; small renderers draw. One scene can go to a canvas for interactive use or to an SVG string for export, with labels in mathematical notation.

Canvas and SVG

Interactive drawing uses the 2D canvas: it redraws a few thousand cells or a long polyline in well under a frame, which is what slider drags need. SVG is for export, printing, server-side rendering and tests, where a vector file and crisp text matter more than redraw speed. There is no WebGL; nothing so far needs it.

Both back ends share one step, primitives(vp, layers), which is the only place exact values become floating-point pixel coordinates. It returns paths, rectangles, dots and labels; paint(ctx2d, prims) and svgElements(prims) only paint them. A native app can paint the same primitives with its own drawing API.

drawScene(ctx2d, vp, layers, { axes: true, scale: devicePixelRatio, background: "#fff" });
const svg = sceneSVG(vp, layers, { axes: true, background: "#fff" });

Layers

A layer is a geometry object, a frame from graph, implicit or region (quick or exact), a label, or { item, ...style }. Style keys: color, width, dash, fill, fillAlpha, radius and open for points, label (TeX, or true for the object's own toTeX()), anchor, and for regions corners and cornerLabels.

Labels with mathematical notation

label([x, y], tex, { anchor, color, size }) places LaTeX at a world point. Anchors n ne e se s sw w nw c say where the label sits relative to the point; labels that would run off the edge flip to the other side.

  • On a canvas, which cannot typeset TeX, mountLabels(container, vp, prims, { katex }) places the labels in an HTML layer over the canvas and typesets them with KaTeX. Positions are in percent, so they follow the canvas when CSS scales it. Without KaTeX, labels fall back to a plain-text reading (texToText: x29\tfrac{x^2}{9} becomes x²/9).
  • In SVG, pass tex: (s) => katex.renderToString(s) to sceneSVG, and labels go in as foreignObject HTML (the page needs KaTeX's stylesheet). Without it, they are <text> elements with the plain-text reading.

These docs load KaTeX from ratmath.com/assets/katex/0.19.1/, shared by all the RatMath docs sites.

Axes

axes: true (or { grid, axis, text } colors) draws a grid at 1, 2 or 5 times a power of ten, axis lines through 0, and tick labels.

Try itEdit the code and run it with @ratmath/graphics and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math, and SHOW(svg) draws a picture.
Ctrl/⌘ + Enter runs