<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
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:
board, the JSXGraph board. Make elements withboard.create(type, parents, attributes).point(x, y, attributes), a draggable point tied to variables. See below.JXG, the whole library.- The page's variables, your registered functions and the usual
Math,Arrayand friends.
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.
- The values snap to the variables'
min,maxandstep, from the<t-num>or<t-var>that declares them, and the point snaps with them. - Lines and polygons made from such points can be dragged too. Dragging a segment between two of them moves both, and sets all four variables at once.
- The reader can also tab to a point and move it with the arrow keys.
Theme
The board draws in the page's colors, in light and dark mode:
- Axes, gridlines and labels are
currentColor, with the strengths from--tangle-chart-gridand--tangle-chart-text, and the font from--tangle-chart-font. - Points, lines, curves and polygons are
--tangle-accent. - For other colors, give an element a
cssClassand color it from your CSS, as the example does with.runand.swim. CSS beats the colors JSXGraph sets, and the class stays on when the pointer is over the element. - The board is 3:2. Set
aspect-ratioorheighton.tangle-jsxgraphto change that.
Attributes
| Attribute | Default | Description |
|---|---|---|
x | -5 5 | The range of the x-axis: left, then right. |
y | -5 5 | The range of the y-axis: bottom, then top. |
axis | Draw axes, with a gridline at every tick. | |
src | A 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.
- If the page already loaded it (
window.JXG), that copy is used. - To load it from somewhere else, call
configure({ jsxgraphUrl })in the module that importstangle.js, or setwindow.TangleConfigbefore it loads. See Configuration.