Text that makes room for a shape you drag
Drag the worm's head into the text. The lines move out of its way as it goes. The worm is drawn by the canvas. The text is ordinary paragraphs, and the browser lays them out again on every frame.
After Margin Dragon by Baked Design Studio, where a dragon flies through a page of text.
Show the code the markup, the polygon, the frame, the paint, the drag
- The markup. An article with an empty float in it, and an empty button for the head.
- Styles. The float and its starting polygon.
- The worm. Discs along a curve from the left edge to the head.
- The polygon. How far right the discs reach on each row, as a CSS
polygon(). - A frame. Move the head, write the polygon, time the layout.
- The paint. One draw of the article, then the worm on top.
- The drag. Pointer events and arrow keys on the head button.
- Drawing the worm. Canvas arcs.
How it works
The text is an <article> that's a child of the canvas. Its first child is an empty <div> floated left, as tall as the article and 80% of its width. A float that size would push all the text into a thin column, but it has a shape-outside polygon, and text is kept out of the polygon only. Where the worm isn't, the polygon is a strip 16 pixels wide, which is the left margin.
The shape. The worm is 28 discs along a curve from the left edge to its head. For every 6 pixels down the article, the page works out how far right any disc reaches and adds 10 pixels. Those numbers become the polygon's right-hand edge.
A frame. While the head is moving, a requestAnimationFrame() callback moves it towards the pointer on a spring, writes the new polygon to the float's style, and reads offsetHeight to make the browser lay the text out there and then. The time that takes is the layout cost the line under the demo reports. The canvas then gets a paint event, draws the article with one drawElementImage() call, and draws the worm over it with canvas arcs.
Input. The article's hit box doesn't move: it's drawn at the same place every time. The words inside it move because the layout moved, and hit testing follows the layout, so the link is pressed where it's drawn. The head is an empty <button> whose hit box is put at the head on each paint. It has touch-action: none, so dragging it doesn't scroll the page, and the arrow keys move it.
One side only. A float sits at an edge, and text wraps round one side of it. So the worm comes in from the left margin and the text is always to its right. The head stops at 62% of the width so there's room left for words.
Live checks
These run on their own small canvases in the corner of the window.
Findings
- In Chromium 157, rewriting a
shape-outsidepolygon on a float inside a drawn element moves the text in the element's picture. The picture drawn in the nextpaintevent has the new layout. - The change raises a
paintevent by itself. The page callsrequestPaint()as well, because the worm has to be drawn again even when no line moved. - One layout of these three paragraphs took 0.3 ms in the test browser, and the longest took 1.1 ms. The page ran at 60 paints a second with a layout before each. That browser is a desktop processor rendering in software. Not verified on a phone: the line under the demo reports the numbers, and the same line is sent to this site's report log.
- A link that the polygon pushed 140 pixels along was found by
elementFromPoint()at its new place and not at its old one. The article's hit box didn't change. Hit testing inside it follows the layout. - A polygon with 82 points was accepted and read back from
getComputedStyle(). - Text can only go on one side of the shape. A float sits at an edge, and the test browser has no CSS exclusions (
wrap-flow). So this page can't put the worm in the middle of a line with words either side of it, which Margin Dragon does by placing the text itself. - A line clears the shape over the whole of its height, so the gap between the worm and the text is uneven: it's widest where the worm's edge slopes.
- The head is an empty
<button>withtouch-action: none, as on the crumpled receipt. In touch emulation a drag on it moved the worm and didn't scroll the page, and a drag on the text scrolled the page. - With reduced motion on, the head jumps to where it's dragged with no spring.