210 lines
6.2 KiB
JavaScript
210 lines
6.2 KiB
JavaScript
// Pure JS host implementation of the wasi-gfx WIT interfaces:
|
|
// - wasi-gfx:frame-buffer/frame-buffer@0.2.0
|
|
// - wasi-gfx:surface/surface@0.2.0
|
|
// - wasi-gfx:surface/surface-frame-buffer@0.2.0
|
|
//
|
|
// Works in both Browser (HTMLCanvasElement + Canvas2D ImageData) and Node/headless.
|
|
|
|
export class WasiGfxBuffer {
|
|
constructor(width = 1, height = 1) {
|
|
this.width = Math.max(1, width | 0);
|
|
this.height = Math.max(1, height | 0);
|
|
this._data = new Uint8Array(this.width * this.height * 4);
|
|
}
|
|
|
|
getWithCopy() {
|
|
return new Uint8Array(this._data);
|
|
}
|
|
|
|
setWithCopy(val) {
|
|
if (!(val instanceof Uint8Array)) {
|
|
this._data = new Uint8Array(val);
|
|
} else {
|
|
this._data = new Uint8Array(val);
|
|
}
|
|
}
|
|
}
|
|
|
|
function makeEventStream(subscribe) {
|
|
let unsubscribe = null;
|
|
return new ReadableStream({
|
|
start(controller) {
|
|
unsubscribe = subscribe((ev) => controller.enqueue(ev));
|
|
},
|
|
cancel() {
|
|
if (unsubscribe) unsubscribe();
|
|
},
|
|
});
|
|
}
|
|
|
|
export class WasiGfxSurface {
|
|
constructor(desc = {}, options = {}) {
|
|
this._width = Math.max(1, (desc.width ?? options.width ?? 1280) | 0);
|
|
this._height = Math.max(1, (desc.height ?? options.height ?? 720) | 0);
|
|
this.canvas = options.canvas ?? null;
|
|
this.onPresent = options.onPresent ?? null;
|
|
this.presentedFrames = 0;
|
|
this.lastPresented = null;
|
|
this._listeners = {
|
|
resize: new Set(),
|
|
frame: new Set(),
|
|
pointerUp: new Set(),
|
|
pointerDown: new Set(),
|
|
pointerMove: new Set(),
|
|
keyUp: new Set(),
|
|
keyDown: new Set(),
|
|
};
|
|
|
|
if (this.canvas) {
|
|
this.canvas.width = this._width;
|
|
this.canvas.height = this._height;
|
|
this._ctx2d = this.canvas.getContext('2d');
|
|
this._bindCanvasEvents();
|
|
} else {
|
|
this._ctx2d = null;
|
|
}
|
|
}
|
|
|
|
_bindCanvasEvents() {
|
|
const c = this.canvas;
|
|
const pos = (e) => {
|
|
const r = c.getBoundingClientRect();
|
|
return {
|
|
x: (e.clientX - r.left) * (c.width / (r.width || c.width)),
|
|
y: (e.clientY - r.top) * (c.height / (r.height || c.height)),
|
|
};
|
|
};
|
|
c.addEventListener('pointerup', (e) => this._emit('pointerUp', pos(e)));
|
|
c.addEventListener('pointerdown', (e) => this._emit('pointerDown', pos(e)));
|
|
c.addEventListener('pointermove', (e) => this._emit('pointerMove', pos(e)));
|
|
if (typeof window !== 'undefined') {
|
|
window.addEventListener('keyup', (e) => this._emit('keyUp', {
|
|
key: undefined,
|
|
text: e.key,
|
|
altKey: e.altKey,
|
|
ctrlKey: e.ctrlKey,
|
|
metaKey: e.metaKey,
|
|
shiftKey: e.shiftKey,
|
|
}));
|
|
window.addEventListener('keydown', (e) => this._emit('keyDown', {
|
|
key: undefined,
|
|
text: e.key,
|
|
altKey: e.altKey,
|
|
ctrlKey: e.ctrlKey,
|
|
metaKey: e.metaKey,
|
|
shiftKey: e.shiftKey,
|
|
}));
|
|
}
|
|
}
|
|
|
|
_emit(channel, event) {
|
|
for (const fn of this._listeners[channel]) fn(event);
|
|
}
|
|
|
|
_subscribe(channel, fn) {
|
|
this._listeners[channel].add(fn);
|
|
return () => this._listeners[channel].delete(fn);
|
|
}
|
|
|
|
height() { return this._height; }
|
|
width() { return this._width; }
|
|
|
|
requestSetSize(height, width) {
|
|
const nw = width != null ? Math.max(1, width | 0) : this._width;
|
|
const nh = height != null ? Math.max(1, height | 0) : this._height;
|
|
if (nw !== this._width || nh !== this._height) {
|
|
this._width = nw;
|
|
this._height = nh;
|
|
if (this.canvas) {
|
|
this.canvas.width = nw;
|
|
this.canvas.height = nh;
|
|
}
|
|
this._emit('resize', { width: nw, height: nh });
|
|
}
|
|
}
|
|
|
|
onResize() { return makeEventStream((fn) => this._subscribe('resize', fn)); }
|
|
onFrame() { return makeEventStream((fn) => this._subscribe('frame', fn)); }
|
|
onPointerUp() { return makeEventStream((fn) => this._subscribe('pointerUp', fn)); }
|
|
onPointerDown() { return makeEventStream((fn) => this._subscribe('pointerDown', fn)); }
|
|
onPointerMove() { return makeEventStream((fn) => this._subscribe('pointerMove', fn)); }
|
|
onKeyUp() { return makeEventStream((fn) => this._subscribe('keyUp', fn)); }
|
|
onKeyDown() { return makeEventStream((fn) => this._subscribe('keyDown', fn)); }
|
|
|
|
_commitFrame(rgbaBytes) {
|
|
this.lastPresented = new Uint8Array(rgbaBytes);
|
|
this.presentedFrames += 1;
|
|
if (this._ctx2d && typeof ImageData !== 'undefined') {
|
|
const expected = this._width * this._height * 4;
|
|
if (this.lastPresented.length === expected) {
|
|
const img = new ImageData(
|
|
new Uint8ClampedArray(this.lastPresented.buffer, this.lastPresented.byteOffset, expected),
|
|
this._width,
|
|
this._height,
|
|
);
|
|
this._ctx2d.putImageData(img, 0, 0);
|
|
}
|
|
}
|
|
if (this.onPresent) this.onPresent(this.lastPresented, this._width, this._height);
|
|
}
|
|
}
|
|
|
|
export class WasiGfxFrameBufferContext {
|
|
constructor(surface) {
|
|
this.surface = surface;
|
|
this._buffer = new WasiGfxBuffer(surface.width(), surface.height());
|
|
}
|
|
|
|
getCurrentBuffer() {
|
|
if (this._buffer.width !== this.surface.width() || this._buffer.height !== this.surface.height()) {
|
|
this._buffer = new WasiGfxBuffer(this.surface.width(), this.surface.height());
|
|
}
|
|
return this._buffer;
|
|
}
|
|
|
|
present() {
|
|
this.surface._commitFrame(this._buffer.getWithCopy());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a wasi-gfx host state & import table for jco-transpiled components.
|
|
* Tracks all created surfaces and contexts so hosts/tests can inspect presented frames.
|
|
*/
|
|
export function createWasiGfxHost(options = {}) {
|
|
const surfaces = [];
|
|
const contexts = [];
|
|
|
|
class Surface extends WasiGfxSurface {
|
|
constructor(desc) {
|
|
super(desc, options);
|
|
surfaces.push(this);
|
|
}
|
|
}
|
|
|
|
class Context extends WasiGfxFrameBufferContext {
|
|
constructor(surface) {
|
|
super(surface);
|
|
contexts.push(this);
|
|
}
|
|
}
|
|
|
|
const imports = {
|
|
'wasi-gfx:frame-buffer/frame-buffer': { Buffer: WasiGfxBuffer },
|
|
'wasi-gfx:frame-buffer/frame-buffer@0.2.0': { Buffer: WasiGfxBuffer },
|
|
'wasi-gfx:surface/surface': { Surface },
|
|
'wasi-gfx:surface/surface@0.2.0': { Surface },
|
|
'wasi-gfx:surface/surface-frame-buffer': { Context },
|
|
'wasi-gfx:surface/surface-frame-buffer@0.2.0': { Context },
|
|
};
|
|
|
|
return {
|
|
Surface,
|
|
Context,
|
|
Buffer: WasiGfxBuffer,
|
|
surfaces,
|
|
contexts,
|
|
imports,
|
|
};
|
|
}
|