A photo you can shred and pull back out
Drag the photo down into the shredder, or use the slider under it. While any of the photo is still showing, pull it back up and it comes out whole. Let go with all of it inside and it's deleted. The heart on the photo can be pressed as long as it's above the slot.
After Photo Shredder by Gabriel Obholz (on Memorables).
Show the code the markup, the copy, the paint, the drag and the slider, the strips
- The markup. A
<figure>with a button in it, the shredder as one element, and a range outside the canvas. - Styles. The photo has
touch-action: none, so a drag on it moves the photo and not the page. - The copy. Draw the photo once, whole, and copy that rectangle of the canvas into a plain canvas.
- The paint. The part above the slot is the live element under a clip. The strips come from the copy. The shredder is drawn again with no alpha so it takes the taps below the slot.
- The drag and the slider. Both set the same number. Letting go at 100 deletes the photo.
- The strips. Each strip is pieces of the copy, pushed sideways a little more the further down they are.
How it works
The photo is a <figure> with a picture made of CSS gradients, a caption and a favourite button. It's a child of the canvas. The shredder and its bin are one more element in the canvas, and the range is an ordinary <input type="range"> under it.
One number. How far the photo has gone in is the range's value, from 0 to 100. Dragging the photo sets the range, and the range moves the photo, so the arrow keys on the range do what a drag does. Letting go of either at 100 deletes the photo.
Above the slot. The part of the photo that hasn't gone in is drawn with drawElementImage() under a clip that ends at the slot. It's the real element, moved down, so its button can be pressed. A clip doesn't shrink the element's hit box, so the shredder is drawn again after it with no alpha. Where two hit boxes overlap the element drawn last takes the tap, and below the slot that's the shredder.
Below the slot. When the photo starts going in, and again whenever its button changes, the canvas draws the whole photo once, copies that rectangle of itself into a plain canvas with drawImage(), and clears. The strips under the shredder are drawn from that copy: 16 strips, each cut into pieces 12 pixels tall that sway further out the further they hang. The row at the top of each strip is the row of the photo that's inside the shredder now, so pulling the photo up pulls the strips back in.
Deleted. At 100 the strips are let go and fall into the bin. The photo is made inert, so its button can't be reached, and "Put the photo back" brings it back.
Live checks
These run on their own small canvases in the corner of the window.
Findings
- A range works as the native control under the gesture. Dragging the photo and moving the range set the same number, so the arrow keys and End on the range shred the photo, and Home pulls it out again. Letting go of either at 100 deletes it. Script that sets a range's value with
stepUp()fires noinputevent (the failing check above), so the page calls its own handler after a change it makes. - In Chromium 157 a clip around
drawElementImage()cuts the picture and not the hit box. The live check found the button 30 pixels below the clip, where nothing was drawn. So the photo's lower half would still take taps inside the shredder. - Drawing the shredder a second time, last and with
globalAlpha0, fixes it. Where two hit boxes overlap the element drawn last takes the tap. With the photo 30% of the way in, the heart is already below the slot and a tap there went to the shredder. At 5% it's above the slot, and a tap pressed it by mouse and by touch. - The strips are 16 columns of pieces 12 pixels tall cut from one copy of the photo, up to 304
drawImage()calls a paint. In a live check they took 1.1 ms to draw at device scale 2.625. On the page the test browser, which renders in software, painted 60 times a second while dragging and while the strips fell, with a longest gap of 17 to 32 ms. 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 part above the slot is the live element, so it stays sharp and its button works. The copy is taken again whenever the button changes, so the heart is red in the strips too.
- A touch drag on the photo moved it 88% of the way in and didn't scroll the page, in touch emulation, because the figure has
touch-action: none. A drag on the empty part of the canvas still scrolls. Not verified on a phone. If the browser cancels a drag, the page sends a report. - After a rest the first step of the movement is one frame long. Before I added that, the first paint after a pause moved the photo 98% of the way at once, so pressing End deleted it almost straight away.
- The strips aren't paper. Each piece is pushed sideways by a sine wave that grows towards the end of the strip, and the outer strips lean out. Nothing tears and the pieces don't touch each other.
- With reduced motion on, the strips don't sway and a deleted photo is gone in one paint.
- Without the API there's nothing to draw the shredder, so the photo is plain HTML and the range is turned off.