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.

Wash day

This notice is an <article>. While it hangs still, the canvas draws it as it is and you can select this text.

While it moves, it's a copy cut into triangles. The link and the two controls below can still be pressed.

 

Show the code the markup, the cloth, the paint, the hit box, the corners
  1. The markup. An article, and an empty button for each bottom corner.
  2. Styles.
  3. The cloth. A grid of points, the links between them, and which points are pegged.
  4. One step of time. Move each point, then pull each link back to its length.
  5. The paint. Still: one draw of the article. Moving: triangles from a copy.
  6. The hit box. One draw with no alpha, under the matrix of the triangle beneath the pointer.
  7. Finding that triangle.
  8. Drawing the cloth. Far triangles first, each shaded by the way it faces.
  9. 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