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:
WebInputInput for a
WorkerDisplay, fed by events the page relays.
- class displaydev.workerdisplay.WorkerDisplay(canvas, width=None, height=None, *, quiet=False)[source]
Bases:
CanvasDisplayEmulate a display on an
OffscreenCanvas, from a Web Worker.- Parameters:
- 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.
- displaydev.workerdisplay.feed(event)[source]
Hand a relayed
inputevent to the most recentWorkerDisplay.
- displaydev.workerdisplay.resize(width, height)[source]
Hand a relayed
resizeto the most recentWorkerDisplay.