displaydev.workerdisplay

displaydev.workerdisplay

A display for Python running in a Web Worker: it draws on an OffscreenCanvas the page transferred to the worker, and takes its input from events the page relays.

Running the app in a worker leaves the page’s main thread free, so a long redraw never holds up anything the page has to do on time (Web MIDI, audio hand-offs, input). The page side is a few lines of JavaScript:

const off = canvas.transferControlToOffscreen();
worker.postMessage({type: "canvas", canvas: off}, [off]);
// then, for every pointer, wheel and key event on the canvas:
worker.postMessage({type: "input", event: {
    type: e.type,                      // pointerdown, pointerup, pointercancel,
                                       // pointermove, wheel, keydown, keyup
    x: e.offsetX, y: e.offsetY,        // CSS pixels inside the canvas
    movementX: e.movementX, movementY: e.movementY,
    button: e.button, buttons: e.buttons,
    pointerType: e.pointerType, pointerId: e.pointerId,
    deltaX: e.deltaX, deltaY: e.deltaY,
    key: e.key, location: e.location, repeat: e.repeat,
    ctrlKey: e.ctrlKey, shiftKey: e.shiftKey, altKey: e.altKey, metaKey: e.metaKey,
}});
// and whenever the canvas's CSS size changes:
worker.postMessage({type: "resize", width: rect.width, height: rect.height});

In the worker, hand each input event to feed and each resize to WorkerDisplay.set_css_size. The events come out of display_drv.get_events exactly as PSDisplay produces them, so appdev and LVGL’s display_driver work unchanged. The page should call preventDefault() itself on wheel, contextmenu and the keys that scroll a page; a worker can’t.

Gamepads aren’t available in a worker.

class displaydev.workerdisplay.WorkerDevices(display=None)[source]

Bases: WebInput

Input for a WorkerDisplay, fed by events the page relays.

feed(event)[source]

Queue one relayed browser event (a JS object or a dict).

class displaydev.workerdisplay.WorkerDisplay(canvas, width=None, height=None, *, quiet=False)[source]

Bases: CanvasDisplay

Emulate a display on an OffscreenCanvas, from a Web Worker.

Parameters:
  • canvas – The OffscreenCanvas the page transferred to this worker.

  • width (int, optional) – Panel width; defaults to the canvas width.

  • height (int, optional) – Panel height; defaults to the canvas height.

  • quiet (bool) – Suppress init chatter when True.

set_css_size(width, height)[source]

Tell the display how big the page shows the canvas, in CSS pixels.

property touch_scale

CSS pixels per framebuffer pixel, the same contract as PSDisplay.

map_pointer(local_x, local_y)[source]

Map canvas-local CSS pixels to framebuffer (x, y).

map_pointer_rel(dx, dy)[source]

Map a pointer delta in CSS pixels to framebuffer pixels.

displaydev.workerdisplay.feed(event)[source]

Hand a relayed input event to the most recent WorkerDisplay.

displaydev.workerdisplay.resize(width, height)[source]

Hand a relayed resize to the most recent WorkerDisplay.