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.
Show the code drawable rows, the reorder, the travel
- The markup. An ordinary table inside the canvas. Each row has
drawable. The table itself isn't drawn. - Sort. Remember where each row is drawn, take a snapshot of it, then move the rows in the DOM.
- On
paint. Draw each row between its remembered place and its newoffsetTop. - The paint loop and
put(). - 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
- In Chromium 157 a
<tr drawable>inside a table that is a canvas child keepsdisplay: table-rowand the column widths of its table, and can be drawn at any position. A<tr>that is the canvas's direct child is laid out as a block (recorded on /elements/). - A row moved with
moveBefore(),insertBefore()orappend()can be drawn in the same task, before the nextpaintevent. The picture is the one from before the move, so a style that depends on position, such as:nth-child()striping, shows one frame late. captureElementImage(row)taken before the move still draws after it. That gives the two pictures a cross-fade needs.- Inputs in a row that is drawn rotated, scaled and away from its layout position take taps at the drawn position.
- A canvas shadow (
shadowBlur) applies todrawElementImage()of a row. - With
insertBefore()a focused number field loses focus when its row moves. WithmoveBefore()it keeps it (measured on /patch/; the menu above switches between the two). - Not verified: frame rate of the travel on a phone, and the on-screen keyboard staying open through a sort.