Live DOM on physical devices

A canvas that draws HTML can also hand its pixels to a device that isn't a screen in a browser window. I wanted to know whether ordinary HTML can be the interface of three such devices: a key pad with a small display in every key, a receipt printer and an LED matrix sign. The three canvases below hold a small shop till. Each is converted to the bytes its device accepts, and a simulated device next to it is drawn from those bytes. Press the keys of the simulated key pad: each press clicks the HTML button it shows.

A key pad: one HTML button per key

The left canvas holds fifteen <button> elements and draws each into a 72 × 72 pixel square. The paint event lists the buttons that changed. Ten times a second the page compares the pixels of those keys with what it last sent. A key that differs is encoded as a JPEG and split into 1,024-byte HID output reports, the format of a 15-key Stream Deck. The right picture is drawn only from those reports. A press on it produces the input report a device would send, and the page answers that report with click() on the button.

The DOM. Fifteen buttons in a canvas, 360 × 216 pixels. They can be pressed here too.
Simulated key pad, drawn from the HID reports. Press a key.

Nothing sent yet.

Show the code HTML buttons to HID reports and back
  1. Fifteen buttons in a canvas. Each has the drawable attribute so that it can be drawn by itself.
  2. The keys are styled with CSS. One has a CSS animation.
  3. Draw each button into its 72 × 72 square when the canvas fires paint, and note which buttons changed.
  4. Compare the pixels of those keys, encode each one that differs as a JPEG and send it. The simulated device and a real one get the same calls.
  5. Inside the library: the report layout. Reader is what the simulated device uses to put a key image back together.
  6. An input report clicks the button.
  7. Connect a real device with WebHID.

A receipt printer: an element printed as it looks

The receipt is an HTML element with a table, a gradient and a text field. Add items with the key pad above and type a name in the field. "Print" reads the canvas, turns every pixel into one black or white dot, and builds ESC/POS bytes with the GS v 0 raster command. The simulated printer on the right parses those bytes and prints the dots it finds in them.

The DOM. One element in a canvas that is as wide as the print head has dots.

CANVAS CAFÉ

Order 41 ·

Thank you

Simulated printer, drawn from the ESC/POS bytes.

Nothing printed yet.

Show the code an element to ESC/POS bytes
  1. The receipt is one element in a canvas.
  2. It's styled for paper: black on white, fixed-width type.
  3. Draw it at one canvas pixel per printer dot.
  4. Print: read the pixels, convert, send.
  5. Inside the library: grey levels and one bit per dot.
  6. The ESC/POS bytes, and the reader the simulated printer uses.
  7. Connect a real printer with Web Serial.

An LED matrix: a ticker that is a CSS animation

The sign is an element with a line of text that moves with a CSS @keyframes animation. The canvas draws it into 64 × 32 pixels each time the browser repaints it. Each frame is converted to RGB565, two bytes per pixel, and the simulated panel lights one LED per pixel from those bytes. "Call" on the key pad changes the text.

The DOM, drawn into 64 × 32 pixels and shown enlarged.
ORDER UP Order 40 is ready
Simulated 64 × 32 panel, lit from the RGB565 frames.

No frame sent yet.

Show the code a CSS animation to RGB565 frames
  1. The sign is an element with two lines of text.
  2. The lower line moves with a CSS animation. Sizes are in container units, so the text fits the sign at any size.
  3. Draw the element into 64 × 32 pixels on every paint, then send a frame.
  4. Inside the library: the frame format and its reader.

What needs real hardware

Live checks

These run on test elements in a panel at the bottom of the window, not on the three canvases above. They wait for a tap.

Findings