Tab switcher: live pages in a fan
Four pages of one small app, each in a same-origin <iframe> inside a canvas, drawn as a hand of cards. The cards are the running pages:
start the timer on one card and the others show the time, pick a colour in Settings and every card changes.
Tap inside a card to use the page. Tap a card's title bar to open it at full size, and tap the bar again to go back. Drag sideways on the bars or the background to turn the fan.
Show the code the pose of a card, the draw, the title bars
- The markup. Four iframes with
drawable. - Where a card goes. An angle on a circle for the fan, the middle of the canvas when open, and a blend of the two in between.
- On
paint. Rotate, scale and draw each iframe, then draw its title bar. - Input on the title bars. The bars are canvas drawing, so the page hit tests them itself.
- How the pages talk to each other. One
BroadcastChannel. - Styles.
How it works
Each iframe is laid out at 300 by 440 CSS pixels, so its page sees a phone-sized viewport at all times. On every paint event the canvas works out a pose for each card: a centre, an angle and a scale.
In the fan the centres lie on a circle whose middle is 380 pixels below the canvas, 11 degrees apart, at about 0.6 scale. drawElementImage() draws the iframe under that transform and puts its hit region there, so a tap on a rotated card reaches the page inside it at the right place.
A change inside any iframe raises the canvas's paint event, so the page runs no timer for the cards. The title in a card's bar is read from iframe.contentDocument.title on each paint. The Timer page puts the running time in its title.
The title bars and the close marks are drawn by the canvas and aren't elements. Pointer events over them go to the canvas, and the page maps the pointer into each card's own coordinates with the inverse of the card's matrix.
Opening a card blends its pose towards the middle of the canvas at the largest scale that fits. The other cards drop below the canvas, and their hit regions are shrunk to a point so they take no input. Where two cards overlap, the one drawn last is on top in the picture and also takes the tap.
The iframes are added and removed from click handlers, never inside the paint event: Chromium 157 crashes if an iframe is inserted or removed there.
Live checks
These run on their own small iframes in the corner of the window.
Findings
- In Chromium 157 a same-origin iframe can be drawn rotated and scaled, and a tap at the drawn position reaches the document inside it. The frame's own layout size doesn't change, so the page inside doesn't reflow when its card grows.
- A change in the framed document (a timer tick, typing, a colour set through a
BroadcastChannelmessage) raises the outer canvas'spaintevent. - Where two drawn iframes overlap, the one drawn last takes the tap, in either document order. With a 2D canvas the picture and the hit testing agree without extra work.
contentDocument.titleis readable duringpaint, so the bars follow title changes with no message passing.- The frames keep running while their card is dropped out of view. Nothing in the API pauses a frame that isn't drawn.
- This works for same-origin pages only. A cross-origin iframe is drawn as transparent pixels (measured on /iframe-tiles/).
- Not verified: text entry in a rotated card with the on-screen keyboard, scrolling inside a rotated card by touch, and how many live iframes a phone keeps drawing at 60 frames per second.