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.

Buy stamps

Twelve, second class.

Water the plants

Not the cactus.

Return the book

Due on Friday.

Book the train

Two seats, 09:14.

Call the dentist

Before five.

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
  1. The cards are draggable and drawable children of the canvas.
  2. Draw the hand, and leave out the card that is being dragged.
  3. Copy the card's pixels for the drag image.
  4. Route 1: dragstart, setDragImage(), drop and dragend.
  5. Route 2, for touch: pointer events and the same copied pixels.
  6. 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
  1. Build the boxes and read the picture from FormData.
  2. Draw the form as the editor and as the pattern, and ring each box that changed.
  3. Draw by dragging.
  4. 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
  1. Draw the frame nine times, each through the clip of one tile.
  2. Put the frame's hit region under the active tile and shields over the rest.
  3. Decide whether a tap is the frame's or slides the tile.
  4. The page in the frame.
  5. 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.

Page 1

This page is an element that can be edited. Tap here and type. The corner at the bottom right is curled, and the back of the curl is this same element, drawn a second time and mirrored about the fold. Type until the words reach the corner and they show there in reverse. Drag the corner to the left to turn the page. The slider under the notebook sets how far the corner stays curled.

Page 2: a list

  • Stamps, twelve
  • Water the plants
  • Library book, Friday
  • Train, two seats
  • Dentist, before five
  • Lamp for the hall
  • Batteries, four AA
  • Paper for the printer
  • Tea, the green tin
  • String, a long piece
  • Something for Sunday

Page 3

An almost empty page. Write on it, turn back to page 2 and forward again: the text is still here, because a page that is not on top is the same element, kept in the canvas and not drawn.

Page 4

The last page. Its corner does not curl, because there is no page under it.

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
  1. Four editable pages and a transparent grip, all drawable.
  2. The fold line and the two halves of the page.
  3. Draw the flat part, the mirrored back and the shadows, then place the input.
  4. Drag the corner.
  5. Styles.

How it works

All four demos use the same three facts about drawElementImage():

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
  1. The paint loop and the draw helpers shared by the four demos.
  2. Each demo starts when its section comes near the screen.
  3. A lost context stops every demo.
  4. 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.