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.

Step 1 of 4

Flatbread

  1. Mix 250 g flour, 150 g yoghurt and a pinch of salt into a dough.
  2. Rest the dough under a bowl for the time on the timer.
  3. Divide it into four and roll each piece thin.
  4. Cook in a dry, hot pan until both sides have brown spots.

Have ready: nothing yet.

Findings of this page

10:00

Fold
Angle 125°
Hinge width 0 px
When folded
At the hinge
ValueThis window reportsThe 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
  1. One canvas with two children. Both are ordinary HTML. Nothing in the markup knows about the fold.
  2. Each pane has two layouts, chosen by a data-mode attribute.
  3. 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.
  4. Inside the library: read the posture and the segments.
  5. Choose a rectangle for each pane. Folded: one segment each. Otherwise the content covers the screen and the controls are a bar.
  6. 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.
  7. Draw: one clipped drawElementImage() per strip and pane.
  8. On a real device, a posture change plays the opening.
  9. The hit box of a tilted half is a matrix with a perspective row.
  10. Set the hit boxes after drawing. A pane across both halves gets the matrix of the half under the pointer.
  11. 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