Sortable table: rows travel to their new places

A table of files inside a canvas. Every <tr> has the drawable attribute, so the canvas draws each row on its own. Tap a column heading to sort. The rows are reordered in the DOM at once, and the canvas draws each row moving from where it was to where it now belongs. The number fields and checkboxes work while the rows move. Change a size or tick a box and sort again.

index.html
worker.js
style.css
hero.avif
fonts.woff2
app.wasm
manifest.json
readme.md

 

Show the code drawable rows, the reorder, the travel
  1. The markup. An ordinary table inside the canvas. Each row has drawable. The table itself isn't drawn.
  2. Sort. Remember where each row is drawn, take a snapshot of it, then move the rows in the DOM.
  3. On paint. Draw each row between its remembered place and its new offsetTop.
  4. The paint loop and put().
  5. Styles.

How it works

The table is a child of the canvas and is laid out as a table. It has no drawable attribute, so it has no picture of its own. Each <tr drawable> has one. On every paint event the canvas draws the heading row and the eight body rows with drawElementImage(), and each draw puts the row's hit region where the row was drawn.

A sort does three things in one task. It records the y position at which each row was last drawn. It calls captureElementImage() on each row to keep a picture of the row as it looked. Then it moves the rows with moveBefore() (or insertBefore()). The table's layout changes at once: offsetTop of every row is its new place. For the next 700 ms the canvas draws each row at a position between the recorded one and the new one. In "Arc and lift" a row that moves up swings out to the right, a row that moves down swings to the left, and both are scaled up with a shadow under them. The trail is the same row drawn three more times at earlier points on its path with low alpha.

The striping comes from :nth-child(even), so a row changes background the moment it is moved. With "Fade from the old look" on, the snapshot taken before the move is drawn over the live row and fades out during the travel.

The row is drawn last at its travel position, so taps and typing go to the moving row. With moveBefore() a number field that has focus keeps it through the sort.

Live checks

These run on their own small tables in the corner of the window, not on the table above.

Findings