Topic

Animation

Let a parameter run. Every frame is drawn at an exact rational value, with the quick sketch while it plays and the certified picture when it stops.

Playing a parameter

animate(draw, { from, to, steps, duration, loop, bounce, onSettle }) calls draw(value, info) for each frame, where value is a Core Rational. It returns a controller with play, pause, toggle, seek(index), seekTo(fraction) and stop. onSettle runs when the animation pauses, is sought or ends: that is the place to draw the exact stage.

const anim = animate((a) => {
  drawScene(ctx, vp, [graph("sin(a*x)", { a }, vp).quick.value], { axes: true, background: "#fff" });
}, {
  from: 1, to: 4, steps: 90, duration: 3000, bounce: true,
  onSettle: async (a) => drawScene(ctx, vp, [await graph("sin(a*x)", { a }, vp).settle()], { axes: true, background: "#fff" }),
});
anim.play();

parameterSteps(from, to, n) gives the n+1n + 1 exact values on their own. Anything can be drawn in a frame: a tangent sliding along a curve, a Riemann sum with more and more pieces, a surface with the camera turning.

Animated files

animationSVG(vp, frames, { duration, loop, axes }) writes one SVG that plays wherever SVG images do, with no script. animationPNG(vp, frames, options) writes an animated PNG, which browsers play in an <img>. frames is a list of layer lists, one per frame.

const frames = parameterSteps(1, 4, 30).map((a) => [graph("sin(a*x)", { a }, vp).quick.value]);
const svg = animationSVG(vp, frames, { duration: 3000, axes: true, background: "#fff" });
const png = await animationPNG(vp, frames, { duration: 3000, axes: true });
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