Picture-in-picture for any element
One call, pipElement(el), puts any element in a window above your other windows. It can be a timer, a table or one button. Choose how, then press
Float on a specimen, or Pick any element and click anything on this page.
Nothing is floating.
| Lap | 1 |
|---|---|
| This lap | 0.0 s |
| Best | – |
The fog comes on little cat feet. It sits looking over harbor and city on silent haunches and then moves on.
Show the code float any element
- One call.
elis any element,mode()is the radio button above, andmediamaps media keys to buttons inside the element (optional). - Inside, mode "move": the real element goes into a Document Picture-in-Picture window. No canvas at all. The start of this branch is shown.
- Inside, mode "stream": the element stays, a live picture of it floats, and input comes back. This is where HTML-in-canvas comes in: the element is drawn into a canvas and the pixels are served to the window.
- Inside, mode "video": the canvas becomes a
MediaStreamin a<video>, which classic Picture-in-Picture accepts. - How the element gets into a canvas.
- The whole library.
The three ways
- Move the element (Document Picture-in-Picture). The node is adopted by the floating window's document and put back when it closes. It's DOM in that window, so input, selection and accessibility are the browser's, and it keeps running when this tab is hidden. The page gets a placeholder of the same size. This route doesn't use HTML-in-canvas.
- Live picture (Document Picture-in-Picture and HTML-in-canvas). The element never leaves the page, so nothing reflows and no stylesheet has to be copied. The window shows its pixels and sends pointer and keyboard input back. The picture stops updating while this tab is hidden, because
paintstops. - As a video (
captureStream()and classic Picture-in-Picture). View only, with media keys mapped to the element's buttons where you say so. This is the route for browsers without Document Picture-in-Picture, and it also stops while the tab is hidden.
import { pipElement } from '/lib/element-pip.js';
const pip = await pipElement(el, { mode: 'move' | 'stream' | 'video' | 'auto' });
pip.close(); await pip.closed; // el also fires 'enterpip' and 'leavepip'
Live checks
| Result | Experiment | Detail |
|---|