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.
Nothing sent yet.
Show the code HTML buttons to HID reports and back
- Fifteen buttons in a canvas. Each has the
drawableattribute so that it can be drawn by itself. - The keys are styled with CSS. One has a CSS animation.
- Draw each button into its 72 × 72 square when the canvas fires
paint, and note which buttons changed. - 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.
- Inside the library: the report layout.
Readeris what the simulated device uses to put a key image back together. - An input report clicks the button.
- 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.
Nothing printed yet.
Show the code an element to ESC/POS bytes
- The receipt is one element in a canvas.
- It's styled for paper: black on white, fixed-width type.
- Draw it at one canvas pixel per printer dot.
- Print: read the pixels, convert, send.
- Inside the library: grey levels and one bit per dot.
- The ESC/POS bytes, and the reader the simulated printer uses.
- 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.
No frame sent yet.
Show the code a CSS animation to RGB565 frames
- The sign is an element with two lines of text.
- The lower line moves with a CSS animation. Sizes are in container units, so the text fits the sign at any size.
- Draw the element into 64 × 32 pixels on every
paint, then send a frame. - Inside the library: the frame format and its reader.
What needs real hardware
- Key pad. A Stream Deck with 15 keys that takes JPEG images (Original V2 or MK.2), on a browser with WebHID. The report layout here follows the open-source drivers and hasn't been run against a device. Other models use other key sizes and image formats, and the page refuses them.
- Printer. A thermal printer that accepts ESC/POS over a serial port: USB serial, or Bluetooth serial, which Chrome also offers through Web Serial. The port must be chosen in the browser's prompt. No printer has been tried.
- Matrix. A microcontroller with a HUB75 or similar panel and firmware that reads this page's frame format from its serial port: the bytes
H C, width, height, then RGB565 pixels with the low byte first. There's no common protocol for LED matrices, so the firmware has to be written for it. None was written or tried here.
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
- Ordinary HTML works as the interface of all three devices, as far as bytes go. The buttons, the receipt and the sign are elements with CSS. Nothing in them is written for the device. The conversion code is about 200 lines for the three formats.
- A grandchild of the canvas with the
drawableattribute can be drawn by itself. The fifteen buttons sit in a grid inside one child of the canvas. Each is drawn with its owndrawElementImage()call into a square of a different size than its layout box. event.changedElementsnames the keys to refresh. A device with fifteen separate displays needs to know which of them changed. Thepaintevent lists the drawable buttons whose rendering was invalidated: a frame of the animated key lists that button only, and a clock tick lists the clock. The list isn't a pixel comparison. Setting a button's text to the value it already has puts it on the list, so adding one item listed all three item keys. The page hashes the pixels of each listed key and sends only those that differ from what was last sent.- A CSS animation inside a canvas child fires
painton every frame with no script. The ticker has no timer and norequestPaint(). The page limits the matrix to 30 frames a second and the key pad to 10 updates a second. - A physical key press can only be a synthetic click. The page calls
click()on the button. That runs its handlers, but the event isn't trusted, there's no:activestate and no focus change. The page adds a class while a key is held so that the key picture shows the press. - Text for one-bit paper should be styled for it. The printer has no grey. Anti-aliased text edges become scattered dots under error diffusion and get thinner under a threshold. Heavier type and black on white print cleanly. The gradient bar in the receipt shows what each method does with grey.
- The printed picture includes the caret. The name field in the receipt is a real
<input>. If it has focus when "Print" is pressed, the blinking caret is in the canvas and can be printed as a vertical line after the name. Placeholder text is printed too. A page that prints forms should blur the field and hide placeholders first. - Serial ports are slow for frames. A 64 × 32 RGB565 frame is 4,100 bytes. At 115,200 baud that is about 2.8 frames a second, and at 921,600 about 22. The page drops frames while the port is busy.
- Not verified on hardware. Every result above is from the simulated devices in Chromium 157. The WebHID and Web Serial calls were only checked for existing. Whether a real device accepts these bytes isn't known.