DOM drawn across a fold
A foldable device reports its posture with navigator.devicePosture and the rectangle of each screen half with
viewport.segments. I wanted to know what one canvas that draws DOM can do with those two values: place a pane on each half,
draw one element across the fold, and keep buttons and text fields working where they are drawn. A fold control stands in for the device,
so the page works on a flat screen. Change the angle, then press the buttons and type in the picture.
| Value | This window reports | The canvas is using |
|---|---|---|
navigator.devicePosture.type | ||
viewport.segments | ||
| Draw calls in the last frame |
Show the code two panes placed by posture and drawn across a fold
- One canvas with two children. Both are ordinary HTML. Nothing in the markup knows about the fold.
- Each pane has two layouts, chosen by a
data-modeattribute. - The page creates the view and passes it the values of the controls. The last line switches to the device's own posture and segments.
- Inside the library: read the posture and the segments.
- Choose a rectangle for each pane. Folded: one segment each. Otherwise the content covers the screen and the controls are a bar.
- Cut each half into strips and work out where each strip appears. This is only needed for the picture of a folded device on a flat screen.
- Draw: one clipped
drawElementImage()per strip and pane. - On a real device, a posture change plays the opening.
- The hit box of a tilted half is a matrix with a perspective row.
- Set the hit boxes after drawing. A pane across both halves gets the matrix of the half under the pointer.
- The whole library.
How it works
The canvas has two children, a content pane and a controls pane. When the posture is folded, each pane gets the rectangle of one
segment. When it is continuous, the content pane covers the whole screen and the controls pane becomes a bar. The panes are
resized with CSS, so the browser lays them out again, and the canvas draws them at their new place. A pane that changes place slides there over
320 ms, drawn at sizes between the two.
The fold control draws a picture of a folded device. Each half is cut into strips 8 px long that run beside the fold. A strip
further from the hinge is nearer the eye, so it's drawn wider. Each strip is one drawElementImage() call with a clip and a
scale. Next to the hinge the angle changes over 16 px, which is why the text curves there. On a real foldable none of this is drawn,
because the screen is bent already. There the canvas covers the window and only shades the crease.
"Open" and "Close" swing the angle to 180° and to 60° with a damped motion. In the simulated picture the half that holds the controls is drawn at one fixed tilt and only the other half turns, and the scale is chosen once for every angle, so the controls stay in the same place on the screen while the angle changes. On a real device the same picture is played when
devicePosture fires change: the API gives no angle, so on "continuous" the canvas draws the two halves
as slabs that start at 90° and swing flat over 1.1 seconds, with the live panes on them. A light band crosses each half and the shadow
under the device grows as the halves lift.
A hinge with a width hides pixels. A window that spans a dual-screen device has rows that no screen shows. With "Cut the picture" on, the canvas lays the content out shorter by the width of the hinge and draws it in two pieces, one on each side, so no row is hidden. With it off, the hinge covers part of the content, which is what a spanning window does without a canvas.
Live checks
These run on a second, small fold in a panel at the bottom of the window, not on the one above. They wait for a tap.
Findings
- Layout by posture needs no canvas. Drawing one element across the fold does. CSS can place two elements with
env(viewport-segment-*)and thedevice-posturemedia feature. CSS can't remove the rows of one element that fall under a hinge. A canvas can: it draws the element twice with a clip, before and after the hinge. - A tilted half keeps exact hit testing. One half of a folded surface is a plane.
updateElementGeometry()accepts a matrix with a perspective row, so a button on a half that is drawn at 100° is found byelementFromPoint()where it is drawn, and typing, the range slider and the checkboxes work there. - An element across both halves has one hit box and two planes. The page gives the hit box to the half under the pointer, in
pointermoveandpointerdownlisteners. With a mouse that is always in time. A touch has nopointermovebefore it: thepointerdownof a first tap on the other half is aimed with the old hit box, but the listener moves the box before the browser picks the target of theclick, so the tap still follows a link after a 30 px hinge (emulated touch, Chromium 157). Controls that act onpointerdown, such as a range slider, would miss on that first touch. With no hinge the two planes differ only by the perspective, a few pixels near the fold. - The curve next to the hinge is drawn but not hit tested. The matrix describes the flat part of each half. In the 16 px next to the hinge the picture is curved and the hit box is not, so a target there is off by up to 2 px at 90°.
- The posture and the segments were read through DevTools emulation only. In Chromium 157,
Emulation.setDeviceMetricsOverridewith a display feature gives two entries inviewport.segmentsanddevicePosture.type"folded", and "Use this device's fold" places the panes on them.visualViewport.segments, the older name, doesn't exist in this build. - Not verified on a foldable. Segment rectangles on a real device, the hinge width a dual-screen device reports, how the on-screen keyboard changes the segments, and whether the crease shading lines up with the physical crease all need the hardware.
- The API has no fold angle, so the opening is played, not tracked. The posture is one of two words. A picture that follows the hinge as a hand opens it isn't possible with this API. The page starts a fixed 1.1 second swing when the word changes to
"continuous", and a shorter one when it changes to"folded". With emulated posture changes the swing plays and the panes stay live during it. How it looks against the real movement of a hinge isn't known. - The panes are live during the swing. Every frame redraws both panes from the DOM and sets their hit boxes, at 60° as well as at 180°. The countdown keeps running on a slab in motion.