Skip to content

Commit

Permalink
d more DOM compat to screen (offsetWidth, offsetHeight, `offset…
Browse files Browse the repository at this point in the history
…Top`, `offsetLeft`)
  • Loading branch information
TooTallNate committed Feb 25, 2024
1 parent 471ad67 commit 6c8d852
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-ligers-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nxjs-runtime': patch
---

Add more DOM compat to `screen` (`offsetWidth`, `offsetHeight`, `offsetTop`, `offsetLeft`)
50 changes: 31 additions & 19 deletions packages/runtime/src/screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ export class Screen extends EventTarget implements globalThis.Screen {
*/
declare readonly height: number;

getContext(contextId: '2d'): CanvasRenderingContext2D {
if (contextId !== '2d') {
throw new TypeError('Only "2d" rendering context is supported');
}

const i = _(this);
if (!i.context2d) {
i.context2d = new CanvasRenderingContext2D(
// @ts-expect-error Internal constructor
INTERNAL_SYMBOL,
this
);

$.framebufferInit(this);
}

return i.context2d;
}

// @ts-expect-error
addEventListener(
type: 'touchstart' | 'touchmove' | 'touchend',
Expand Down Expand Up @@ -145,31 +164,24 @@ export class Screen extends EventTarget implements globalThis.Screen {
get nodeName() {
return 'CANVAS';
}
get offsetWidth() {
return this.width;
}
get offsetHeight() {
return this.height;
}
get offsetTop() {
return 0;
}
get offsetLeft() {
return 0;
}
getAttribute(name: string): string | null {
if (name === 'width') return String(this.width);
if (name === 'height') return String(this.height);
return null;
}
setAttribute(name: string, value: string | number) {}

getContext(contextId: '2d'): CanvasRenderingContext2D {
if (contextId !== '2d') {
throw new TypeError('Only "2d" rendering context is supported');
}

const i = _(this);
if (!i.context2d) {
i.context2d = new CanvasRenderingContext2D(
// @ts-expect-error Internal constructor
INTERNAL_SYMBOL,
this
);

$.framebufferInit(this);
}

return i.context2d;
}
}
$.canvasInitClass(Screen);
def(Screen);
Expand Down

0 comments on commit 6c8d852

Please sign in to comment.