Heat map over table cells
A table of requests per second, seven days by eight times of day, inside a canvas. Each <td> is drawn on its own with no background.
Under the cells the canvas paints a colour field computed from the numbers, with a smooth gradient from each cell to its neighbours and contour lines at 20, 40, 60 and 80.
Tap a number and type a new one, or press Live data. The numbers are the table's own text, and the field is recomputed from them.
Show the code the table, the field, the cells on top
- The table. Built from an array. Every
<th>and<td>hasdrawable, and each<td>can be edited. - The field. A small bitmap, six samples for each cell in each direction, interpolated from the cell numbers with a Catmull-Rom curve.
- Contour lines. Marching squares over the same samples.
- On
paint. Stretch the bitmap over the cells, stroke the lines, then draw each cell on top. - The paint loop and
put(). - Styles.
How it works
The table is laid out by the browser with table-layout: fixed, so all number cells are the same size. The table has no picture. Each cell has drawable and a transparent background, so its picture is its text and, when it has focus, its outline and caret.
The page reads the numbers from the cells' text. For each frame it fills a bitmap of 48 by 42 pixels: six samples across and down each cell. A sample's value is interpolated from the 4 by 4 cells around it with a Catmull-Rom curve, which passes through each cell's own number at the cell's centre.
The value picks a colour from a list of stops. drawImage() stretches the bitmap over the cell area with smoothing on. In "Cell blocks" each cell is one flat rectangle.
The contour lines come from the same samples. For each square of four neighbouring samples and each level, marching squares finds where the level crosses the square's edges and joins the crossings.
Then each cell is drawn at its layout position with drawElementImage(). With the checkbox on, a cell is scaled about its centre by its number, from 0.8 to 1.35 times, and its hit region is scaled with it.
A changed number moves the shown value towards the new one over a few frames, so the colour spreads. The text colour of a cell is switched between white and black from the brightness of its colour.
Live checks
These run on their own small tables in the corner of the window.
Findings
- In Chromium 157 a
<td drawable>with no background has a picture with transparent pixels around its text. What the canvas painted before the cell shows through, so a canvas-drawn background needs no CSS on the cells. - The table lays the cells out. With
table-layout: fixedthe cell boxes are a regular grid, andoffsetLeftandoffsetTopof a cell are relative to the table. - Editing a cell's text (
contenteditable="plaintext-only") raises apaintevent with norequestPaint(). The caret and the focus outline are part of the cell's picture. - A cell drawn scaled about its centre is hit tested at the scaled box.
drawImage()of a 48 by 42 bitmap stretched to the cell area gives the smooth gradient at no measurable cost next to the 63drawElementImage()calls of a frame.- Not verified: the on-screen number keyboard for
inputmode="numeric"on a cell drawn in a canvas, and frame rate with Live data on a phone.