<t-jsxgraph>

A JSXGraph board that shares variables with the page. Where <t-vega> and <t-obsplot> draw charts of data, JSXGraph draws points, lines and curves that the reader can drag. JSXGraph loads only on pages that have a <t-jsxgraph>.

A lifeguard runs at 5 on sand and swims at 1.5. Going in at the marked point takes , Heading straight for the swimmer takes .

\frac{\sin\theta_1}{\sin\theta_2} = \val[%.2f]{sin1 / sin2} \qquad \frac{v_\text{run}}{v_\text{swim}} = \val[%.2f]{vr / vs}

The straight line is the shortest way to the swimmer, but not the fastest. Sand is quicker than water, so the fastest route runs further along the beach and swims less. It's the route where the two ratios above are equal, which is Snell's law: light is slower in water than in air, and it bends at the surface by the same rule, for the same reason.

The lifeguard, the swimmer and the entry point are each two variables, through point(). The entry point's y is the number 0, so it only slides along the shoreline. The times, the fastest route and the angles are <t-let>s, and the dashed route reads best inside a function.

Writing the board

Put a script in a <script type="text/plain"> inside the element, or keep it in its own file and point to it with src. Unlike <t-obsplot>, it's a script, not an expression, and it runs once, when JSXGraph has loaded. It builds the board with JSXGraph's own API, and has these names in reach:

In Markdown, keep blank lines out of the script: a blank line ends the HTML block.

Variables

From the page to the board

Where JSXGraph takes a function (a coordinate, a curve, an attribute such as visible), the function can read variables:

board.create("point", [() => q, () => p], { fixed: true });
board.create("functiongraph", [(x) => amp * Math.sin(freq * x)]);

JSXGraph calls these functions whenever the board updates, and the board updates when a variable they read changes. A variable read outside a function is read once, when the script runs.

From the board to the page

point(x, y) makes a point at the values of two variables, given by name. Dragging it sets them, and changing them elsewhere moves it. A number instead of a name keeps that coordinate fixed, as point("ex", 0) does above: the point only slides along the shoreline.

Theme

The board draws in the page's colors, in light and dark mode:

Attributes

AttributeDefaultDescription
x-5 5The range of the x-axis: left, then right.
y-5 5The range of the y-axis: bottom, then top.
axisDraw axes, with a gridline at every tick.
srcA URL to load the script from, instead of the <script> inside.

How JSXGraph loads

The first <t-jsxgraph> on a page loads JSXGraph 1.14.0 from jsDelivr: about 1 MB of minified JavaScript, 260 KB over the network. Pages without a <t-jsxgraph> load none of it.