‹ Phoria demos

Tags and plots

Two more walk-throughs, carrying on from Getting started. First, 3D in plain HTML using the <phoria-view> tag, then plotting maths functions - surfaces that move, contour maps, shapes and fields. As before, every step is a complete program running beside its code, which you can copy as one HTML file or open in CodePen. Allow about fifteen minutes.

Part 3: 3D without a render loop

Sometimes you just want a model, a chart or a scene on a page, without writing any JavaScript beyond a single import. phoria2/element gives you a special HTML tag for exactly that, <phoria-view> and its attributes are the same options a View takes, written in hyphen-case style.

1. A model in one tag

src points to a glTF model, which is loaded and framed automatically for you. environment="studio" lights it with a built-in studio mode, orbit lets you turn it with the mouse and auto-rotate turns it for you, ground-shadow sits it on a soft shadow and ambient-occlusion darkens its creases. The script is just one line: importing the module defines the tag, and every <phoria-view> on the page starts drawing.

The tag is an ordinary block element, so you size it with CSS (it's 300px high by default unless you change it). Its canvas lives inside the tag, which means your page's CSS styles the labels and tooltips just like anything else on the page.

2. Attributes are live

When you change an attribute, the view updates to match - here the buttons change exposure, environment-intensity and background, and the model doesn't need to load again. A switch like ground-shadow is on when it's there, and you turn it off by removing it or setting it to "false".

3. A chart from JSON

Instead of a model, the tag can hold its content as data, in a <script type="application/json"> inside it. That can be a chart spec, a node or a whole scene - the same descriptions create() takes. phoria2/charts is only loaded because the content contains a chart, so pages without one don't pay for it.

Hover over a bar to see its tooltip. The script hasn't changed at all - everything here is HTML.

4. Custom JavaScript when you want it

Clicking sends a pick event, with the hit in its detail - its datum is the row behind the bar you clicked. Once the content has loaded you get a load event, and from then on the tag's View is there for anything the attributes don't cover. Here it's used to find the chart by its name and give it new figures every two seconds.

Part 4: Plotting functions

Maths Is Beautiful! As well as rows of data, charts can plot maths functions. You give the chart a function, fn, and the ranges to sample it over, and it does the rest - you get the axes, colours, tooltips and smooth transitions like any other chart.

1. A surface from a function

Here fn(x, z) works out a height for each point across x and z. The surface is coloured by height unless you tell it otherwise, and you can hover over it to read off a point.

How finely the function is sampled is set by samples. 48 is the default unless you change it - go higher for a sharper surface, or lower if you need it to be faster.

2. A parameter and a slider

Anything in params is passed to your function along with the point. When you change them with plot.update({ params }), the surface smoothly changes to its new shape. Giving y a fixed range keeps the axis still, so you see the surface itself move rather than the axis rescaling to fit it.

3. Moving with time

With time: true, your function is also given the chart's clock as t, in seconds, and the surface is worked out again every frame. Only the surface changes - the axes, scales and legend stay as they are, which is why y has a fixed range. Here two ripples spread out and meet: where two crests meet they add together, and where a crest meets a trough they cancel out.

4. Contour lines

Contour lines join up points of equal height, like the ones on a walker's map. Give contours the number of lines you want, or the exact heights to draw them at, and place puts them on the surface, on the floor underneath it, or both.

5. Shapes that fold back

A height on its own can't make a shape like a ring, but a parametric surface can. Its fn(u, v) returns a whole point, [x, y, z], for each pair of u and v values. A curve works the same way with a single parameter, t. Both use equal units on every axis so a torus stays nice and round, and giving both charts the same x, y and z ranges puts them in the same box, so the knot sits neatly on the ring.

6. A field of arrows

mark: 'vector' draws an arrow at each sample, pointing the way your function says, with its length and colour showing how strong it is there. frame: 'origin' swaps the chart's usual walls for axes through zero with arrowheads, like you'd draw on a blackboard.

Where next

Copyright © Kevin Roast 2013-2026 · GitHub · npm