A notice printed on cloth
The notice hangs from four pegs. Pull one of its bottom corners, press "Gust of wind", or scroll the page, and it swings as cloth. Try the link, the checkbox and the button on it while it's still moving.
After An Infinite Carousel of Cloth Posters by Jesper Landberg, HTML cloth by Thomas Richter-Trummer and the Cloth component in CanvasUI by DavidHDev.
Show the code the markup, the cloth, the paint, the hit box, the corners
- The markup. An article, and an empty button for each bottom corner.
- Styles.
- The cloth. A grid of points, the links between them, and which points are pegged.
- One step of time. Move each point, then pull each link back to its length.
- The paint. Still: one draw of the article. Moving: triangles from a copy.
- The hit box. One draw with no alpha, under the matrix of the triangle beneath the pointer.
- Finding that triangle.
- Drawing the cloth. Far triangles first, each shaded by the way it faces.
- The corners, the wind and the scroll.
How it works
The notice is an <article> that's a child of the canvas. While the cloth is still, the canvas draws it with one drawElementImage() call, so it's the element itself: you can select its text and its hit box is exact.
The cloth. When something disturbs it, the canvas draws the article once more, copies that rectangle of itself into a plain canvas with drawImage(), and from then on draws 216 triangles from the copy. The triangles join a grid of 10 by 13 points. Each point is linked to its neighbours by a fixed length, four points on the top edge are pegged, and each frame the points move under the wind and a pull towards where they hang at rest, and the links are pulled back to their lengths. The cloth is seen from straight in front. A point's depth changes how its triangles are lit and which is drawn on top.
The hit box. A hit box is the element's box under one matrix, and a cloth isn't one matrix. But each triangle is. Whenever the pointer moves or goes down, and on every frame, the page finds the triangle under the pointer and draws the article once with globalAlpha at 0 under that triangle's matrix. Nothing is painted, and the hit box now agrees with the picture at the pointer. It disagrees everywhere else, and that's only a problem for a second pointer.
Changes. If you tick the box or press the button while the cloth moves, the copy is out of date. A click, input or change event on the article makes the next frame take a new copy.
The corners. Each bottom corner is an empty <button> whose hit box is put at the corner on every paint. Dragging one pins that point of the cloth to the pointer. They have touch-action: none, and the rest of the canvas scrolls the page as usual. When the cloth has come to rest, the element is drawn again.
Live checks
These run on their own small canvases in the corner of the window.
Findings
- A cloth has no single matrix, but each of its triangles has one. In Chromium 157 a
drawElementImage()withglobalAlphaat 0 under a sheared matrix moved the element's hit box and painted no pixels. - With the matrix of the triangle under the pointer, a control is hit where the cloth draws it. I held a corner up with the mouse so the checkbox was drawn 30 pixels from its flat place, tapped it by touch at the drawn place, and it ticked. The button counted a press the same way.
- The same hit box is wrong away from that triangle. The live check above uses the matrix from one corner of a bent cloth and misses the button in the middle. A second finger somewhere else on the cloth would get the wrong answer until its own
pointerdownmoved the hit box. - The draw that moves the hit box works from inside a
pointerdownlistener in the capture phase, outside apaintevent. Taps by touch landed on the moving button 4 times out of 4 in emulation. - The picture on the cloth is a copy, so a control that changes while the cloth moves needs a new copy. The page takes one after a
click,inputorchangeevent on the article. A change that raises none of those, such as a caret blinking, isn't shown until the cloth is still. - Keyboard focus isn't shown on the moving cloth for the same reason, though Enter on the focused button still presses it.
- 216 triangles and two steps of the cloth took 2.9 ms in a live check at device scale 2.625. On the page the test browser, which renders in software, managed 41 to 47 paints a second at that scale and 60 at scale 1. Not verified on a phone: the line under the demo reports it, and the same line is sent to this site's report log.
- The cloth is seen from straight in front with no perspective, so a point's place on the canvas is its x and y and a triangle's matrix is an ordinary 2D one. With perspective the matrix would need a fourth row.
updateElementGeometry()takes one, and I didn't try it here. - Where the cloth folds over, the back of it shows the picture mirrored and darker, and a tap there goes to whatever control is printed on that part.
- The first version drew pale lines between triangles.
drawImage()was given exactly the triangle's box, and the clip path is a little larger than the triangle, so the rim of each clip was empty. Drawing 1 pixel more of the copy on each side fixed it. The same change is in the crumpled receipt, which shares the triangle code. - Scrolling the page swings the cloth. With reduced motion on, it doesn't, and a gust lasts only as long as the wind.