A window that pours into the dock
Press the line in a window's title bar, or its button in the dock. The window bends into a funnel and pours into the button, and the button pours it back out. Both windows are <dialog> elements with real controls in them. Type in the notes, send the window away and bring it back, and what you typed is still there. The timer keeps counting while its window is in the dock.
After Genie Minimise by Raphael Salaja, which I found on Memorables. The effect is the one macOS uses for its Dock.
Show the code the markup, the funnel, the strips, the paint
- The markup. Two dialogs, each in a positioned wrapper, and a
<nav>of buttons. - The funnel. Where a row of the window goes at a given progress.
- The strips. One draw for each 2 px row, from a copy, by source rectangle or by clip.
- Open and close. The
openattribute before the window pours out,dialog.close()after it has poured in. - One picture. The window drawn once and copied into a plain canvas, for the copy route.
- The paint. A whole draw for a window at rest, strips for one that's moving, and the dock on top.
- Styles.
- The whole library file.
How it works
One canvas draws three elements: two wrappers that each hold a <dialog>, and the dock. A window at rest is one drawElementImage() call.
A window that's moving is drawn as rows 2 pixels tall, up to 92 of them for the notes. For each row the page works out where it is in the funnel: how far down, how wide and how far across. The sides bend in towards the dock button first, and the rows then slide down between them. Each row is one draw with nine arguments: a source rectangle inside the element and a destination rectangle on the canvas.
The second route draws the whole element once for each row, scaled and clipped to that row.
Both of those ask the browser to draw the window's picture about 90 times a frame, each time at a different scale. On my phone that was slow and uneven. So the page starts on a third route: it draws the window once, copies those pixels into a plain canvas with drawImage(), and then copies strips out of that canvas. The copy is taken when the movement starts, so the timer's digits don't change while its window is in flight.
The menu under the demo switches between the three, and the line under it reports the paints and the longest gap between two paints for the last movement. The same line is sent to this site's report log.
Open and closed are the dialog's own states. When a window has poured in, the page calls dialog.close(). A closed dialog has no box, so the canvas has nothing to draw and the keyboard can't reach its controls. To bring it back the page sets the open attribute, moves focus to the window's minimise button and draws strips from the first frame that has a picture. The dock button carries aria-expanded.
Input. The copy route never draws the window while it moves, so its hit box stays where the one whole draw put it. A draw with a source rectangle puts the element's hit box at the piece that was drawn, with the size of the piece. After 92 of them the box is one thin row near the dock. So on every route, while a window moves, the page shrinks its hit box to a point with updateElementGeometry(), and when the window is whole again one ordinary draw puts the box back.
Live checks
These run on their own dialogs in the corner of the window. None of them calls show(), because that moves focus. The dialogs have the open attribute in their markup.
Findings
- I tried the first version on my phone and it was janky and slow. It drew every strip from the element. I have no numbers from that visit, because the page didn't send any. It does now.
- Two things were wrong. The page capped the time step at a thirtieth of a second, so a phone that painted slower than that also played the movement slower. And each of up to 92 strips a frame was a new draw of the window's picture at its own scale.
- The page now moves by real time and starts on the copy route. On my phone (Chrome 159 on Android) the copy route then ran at 61 paints a second with a longest gap of 19 ms. In the test browser, which renders in software, all three routes run at 60, and I have no phone numbers for the other two. Try them in the menu and read the line under the demo.
- The three routes draw the same shape. In the live checks each covers 37.7% of the fixture canvas.
- Issuing 60 strips took about 0.1 ms a frame from a copy and about 0.2 ms by the other two routes. That is the time to record the draws and says nothing about the time to rasterise them.
- After the strips the hit box is wrong by both routes. By source rectangle none of 20 points in the window's layout box hit it. By clip 4 of 20 did. My guess is that the last clipped draw leaves a whole-sized box under the last strip's transform.
- One whole draw restores the box: 20 of 20 points.
updateElementGeometry()with a transform scaled to a point takes the window out of hit testing while it moves. - A closed
<dialog>leaves its wrapper 0 pixels tall. Drawing the wrapper then doesn't throw and draws nothing. - A
<dialog open drawable>with its defaultposition: absolutealso has a picture. I kept the wrappers because they hold each window's place while it's closed. dialog.show()moves focus into the dialog and scrolls to it. The window's hit box was parked at the middle of the canvas, and with the canvas half off the screen the page scrolled 477 pixels. The page now sets theopenattribute and callsfocus({ preventScroll: true })on the minimise button.- Text in a strip is stretched or squashed with the strip. Half way down the funnel the notes can't be read, which is true of the macOS effect as well.
- Not verified: the paint rate of the source rectangle and clip routes on a phone, and whether the keyboard stays down when a window comes back by touch.
- Not built: dragging a window by its title bar, and the "scale" effect that macOS offers beside this one.