A receipt you can crumple and throw away
Press "Crumple it". The receipt folds up into a ball of paper and drops to the floor. Drag the ball and throw it at the bin, or tap it to smooth it out. When it's flat again it's the same table as before, with a few more creases each time.
After Crumpled Receipt by Lorenzo Cabra (on Memorables) and CrumpledPaper Reveal by Matt Ölschlegel (on Memorables).
Show the code the markup, the one picture, the ball, the triangles, the creases
- The markup. A receipt with a table in it, a bin, and an empty button that is the ball.
- Styles. The ball button has
touch-action: none, so a drag on it doesn't scroll the page. - The one picture. Draw the receipt and its creases, copy that part of the canvas, then make the receipt
inert. - The paint. Flat, it's one element draw. As a ball, every frame comes from the copy.
- Dragging and throwing. Pointer events on the ball button, which is captured for the drag.
- The sheet. Each grid point has a flat place and a place on a lumpy ball.
- One triangle. A clip, a transform and a
drawImage(). - All of them. Far ones first, each shaded by the way it faces.
- Creases. Thin bands of shade and light.
How it works
The receipt is an <article> with a <table> in it, and it's a child of the canvas. While it's flat the canvas draws it with one drawElementImage() call, so you can select the text and press its button.
One picture. When you press "Crumple it", the canvas draws the receipt once more, copies that rectangle of itself into a plain canvas with drawImage(), and clears. From then until the receipt is flat again no frame draws the element. The receipt is made inert and its hit box is shrunk to a point, so nothing invisible can be pressed or reached with the Tab key.
Triangles. The copy is treated as a sheet of 10 by 14 squares, each cut into two triangles. Every corner of the grid has a flat position and a position on a lumpy ball, and a number from 0 to 1 moves it between them. Each frame the triangles are sorted from far to near and drawn with drawImage() under a clip and a transform. A triangle that faces away is filled with blank paper. Each one is then darkened by the way it faces and how deep in the ball it is.
The ball. The ball you drag is a <button> with nothing in it. Each frame the canvas draws it at the paper's position, which draws nothing but moves its hit box there. Pointer events on it drag and throw the paper, a click smooths it out, and it takes keyboard focus like any button. The bin is a button as well.
Creases. Each time the paper is smoothed out, three fold lines are added to a list. They're painted over the flat receipt after it's drawn, and into the copy, so they show on the ball too. Paint on top of an element doesn't change where it can be pressed.
Live checks
These run on their own small canvases in the corner of the window.
Findings
- In Chromium 157 a
<table>inside an element in a canvas is in the element's picture, and its text can be selected while the receipt is flat. - One element draw is enough for the whole time the receipt is a ball. The page copies that part of the canvas into a plain canvas with
drawImage()and draws 280 triangles from the copy each frame. - 280 triangles, each a clip, a transform and a
drawImage(), took 2.6 ms to draw in the test browser at device scale 2.625. That browser renders in software. The crumple ran at 52 to 60 paints a second there, with a longest gap of 19 to 41 ms. 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. - Setting
inerton the receipt took its button out of hit testing and out of the Tab order. Afterinertwas removed, one whole draw of the receipt put its hit box back. - A
<button>with nothing in it, drawn at the paper's position each frame, leaves no pixels and can be pressed, dragged and focused there. Pointer capture on it kept a drag going after the pointer left it. touch-action: noneon that button stopped a touch drag on the ball from scrolling the page in touch emulation, and a drag on the empty part of the canvas still scrolled. Not verified on a phone. If the browser cancels a drag there, the page sends a report.- Paint over an element doesn't change its hit testing. The creases are drawn over the receipt and its button is still pressed through them.
- The creases on the flat receipt and on the ball match because the same lines are painted in both places: over the element on the canvas, and into the copy.
- The ball isn't a simulation of paper. Each grid point moves in a straight line from its flat place to a fixed place on a lumpy ball, with a random push out of the sheet half way. A triangle can pass through another on the way.
- Where the sheet doesn't close up, there's a small hole in the ball and the canvas shows through it.
- With reduced motion on, the receipt becomes a ball on the floor in one paint, and flat again in one paint. A drag still moves it.