Four games made of live elements
Four ideas from the "play" group of the element catalogue: a hand of draggable cards that can be
dragged out of the canvas, a picture made of 256 checkboxes, a same-origin <iframe> cut into a sliding puzzle, and a
contenteditable page whose corner curls. Each one draws a single element in several places or leaves an element out of
the drawing, so what I wanted to find out is where input goes when the picture and the element no longer line up.
Drag a card out of the scene
Five notes, each a draggable element drawn by the canvas. Drag one to the tray under the canvas, to another place in the hand, or to the tray of this page in a second window. With a mouse the browser runs the drag. With a finger, put it on a card and move. Text dragged from any page can be dropped on the canvas and becomes a card.
The tray. It's plain HTML outside the canvas. Drop a card here.
The drag image is the card as the canvas drew it, tilted and with its shadow. The canvas draws that one card alone, copies the region into a plain canvas with drawImage() and draws the scene again, all inside the dragstart listener. The card leaves the scene because the paint handler stops drawing it. It's removed from the document when dragend reports dropEffect "move".
Show the code
- The cards are
draggableanddrawablechildren of the canvas. - Draw the hand, and leave out the card that is being dragged.
- Copy the card's pixels for the drag image.
- Route 1:
dragstart,setDragImage(),dropanddragend. - Route 2, for touch: pointer events and the same copied pixels.
- Styles.
Pixel editor from checkboxes
A form of 256 checkboxes, drawn as a 16 by 16 picture. Tap a square to toggle it, or drag across the squares to draw. The pattern behind the editor is the same form drawn again at a quarter of the size. Arrow keys move between the boxes and Space toggles one.
The picture has no other store: the hex string under the canvas is read from FormData in the paint handler. "Reset the form" is a <button type="reset"> with no script, and the picture goes back to the boxes' checked attributes.
Show the code
- Build the boxes and read the picture from
FormData. - Draw the form as the editor and as the pattern, and ring each box that changed.
- Draw by dragging.
- Styles: a checkbox as a flat square.
Sliding puzzle of a live page
The nine squares are one <iframe> that holds a small page. Tap plain text on a tile next to the gap to slide it. Tap a button, a link, a checkbox or the text field on any tile and the frame gets the tap. Scroll a tile with the wheel or a finger and every tile shows the new position.
An element has one hit region, and nine draws of the frame leave it at the last one. So the page moves the frame's region under whichever tile the pointer is on, and covers the other eight cells with transparent elements that are placed after it. Where regions overlap, the one that was placed last gets the input.
Show the code
- Draw the frame nine times, each through the clip of one tile.
- Put the frame's hit region under the active tile and shields over the rest.
- Decide whether a tap is the frame's or slides the tile.
- The page in the frame.
- Styles.
Contenteditable on a curling page
A notebook of four pages. Each page is a contenteditable element. Tap the text and type. Drag the curled corner to the left to turn the page, or use the buttons. The slider sets how far the corner stays curled. The back of the curled part shows the page's own text in reverse, and it changes as you type.
The page is drawn in two pieces through two clips: the flat part where it lies, and the part past the fold line through a reflection, which drawElementImage() accepts like any other transform. The last draw of the page is clipped away at the flat position, so a tap puts the caret where the flat text is.
Show the code
- Four editable pages and a transparent grip, all
drawable. - The fold line and the two halves of the page.
- Draw the flat part, the mirrored back and the shadows, then place the input.
- Drag the corner.
- Styles.
How it works
All four demos use the same three facts about drawElementImage():
- An element can be drawn many times, and it has one hit region. Every draw moves the region to that draw. The pattern of the pixel editor, the nine tiles and the mirrored back of the page are extra draws. Each demo ends its frame with one more draw of the element at the place where input should go, inside an empty clip, which moves the region and adds no pixels.
- Where two hit regions overlap, the element that was drawn last gets the input. The order in the document doesn't decide it. The puzzle covers eight cells of the frame's region with transparent elements placed after the frame, and the notebook puts a transparent grip over the corner of the page the same way.
- An element that isn't drawn keeps the region of its last draw. A card that is being dragged, a page that isn't on top and a shield that isn't needed are moved out of the way with
updateElementGeometry()and a transform that shrinks the region to a point.
Dragging by touch is kept apart from scrolling with touch-action on the drawn elements: none on the cards, the form and the grip, and pan-y on the canvas round them. The browser applies the value of the drawn element under the finger.
Show the code
- The paint loop and the draw helpers shared by the four demos.
- Each demo starts when its section comes near the screen.
- A lost context stops every demo.
- The elements are then shown as plain HTML.
Live checks
The checks wait for a tap. Each one builds its own small canvas in a corner of the window, and none of them touches the demos above. Pixels are read from a drawImage() copy, not from the drawable canvas.
Findings
Measured in headless Chromium 157 on 2026-11-21, with a mouse, a keyboard and emulated touch at 412 px wide. Not yet run on a phone.
- A
draggableelement drawn by a canvas starts a native drag. A mouse drag that began on the drawn card raiseddragstarton the card.dragoveranddropreached the tray outside the canvas, anddragendon the card reporteddropEffect"move". The canvas and the drawn cards also takedragoveranddrop. - The canvas pixels of one card can be copied inside
dragstart. A draw outside the paint event is in the canvas at once, so the card is drawn alone, copied withdrawImage()and the scene drawn again in one task. What the browser then shows as the drag image, withsetDragImage()and without it, isn't verified: the test browser has no drag image to read. The checkbox under the tray switches between the two on a real device. - An emulated long press didn't start a drag. A touch held for 0.9 s on a drawn card and then moved raised
pointerdownand nodragstart. A long press does nothing in the headless browser outside a canvas either, so this says nothing about a phone. The page has a second route with pointer events for touch and pen, and the line under the demo counts each route. touch-actionon a drawn element is applied where the element is drawn. A touch that began on a card (none) moved the card and didn't scroll. A touch that began on the canvas beside the cards (pan-y) scrolled the page 97 px. The same held for the form of checkboxes and the canvas round it.- A checkbox in a drawn form is toggled by a tap, and a change made by script raises
paint.changedElementsheld the form, which is thedrawableelement, and not the box.form.reset()and a reset button raisedpainttoo. The paint handler reads the picture fromFormData. - An element drawn at a quarter of its size kept hard edges. With
imageSmoothingEnabledoff, four 20 px columns drawn 5 px wide read 0, 255, 0, 255. - Where hit regions overlap, the element drawn last gets the input. The order of the elements in the document didn't matter. A transparent element placed after the iframe took the pointer over that part of the frame.
- One tile of an iframe takes real input. With the frame's region placed under a tile, a click went to the button at the matching point of the frame, the wheel scrolled the frame 90 px, and a touch drag scrolled it. A scroll inside the frame raised
painton the canvas, so all nine tiles followed. - Hit regions moved in
pointerdownchange where the same touch goes. A touch that began on a shield, which then moved the frame's region under the finger, scrolled the frame by 35 px in that same gesture, and its tap was delivered to the frame as a trustedclick. The page also has its own route (the frame document'selementFromPoint()andclick()), used only if no trusted click arrives within 120 ms. The line under the puzzle counts both. drawElementImage()accepts a reflection. The pixels are mirrored, and so aregetBoundingClientRect()andelementFromPoint()for the element's children, until the next draw.- A
contenteditableelement drawn in two pieces takes the caret at its flat place. A tap on the drawn page focused it, typed text was added, and each key raisedpaint, which redrew the flat part and the mirrored back. A page that isn't on top keeps its text and isn't drawn. - Not verified: everything on a phone. In particular whether a long press on a drawn card starts a native drag on Android and what the drag image shows, a drop into a second window, the on-screen keyboard over the notebook and over the text field in the puzzle, touch selection handles on the curled page, and the speed of 42 small draws of the form in each frame while a ring is moving.