Skip to content

Commit

Permalink
chore: updates
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Dec 14, 2024
1 parent 73bdce2 commit fdfcaf9
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 14 deletions.
55 changes: 45 additions & 10 deletions napi/canvas-napi/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,14 +469,13 @@ export class NSCCanvas extends NSView {
(mtlView.layer as CAMetalLayer).isOpaque = false;
}

previousEvent: NSEvent;
previousEvent;

mouseDown(event: NSEvent) {
const canvas = this._canvas?.deref?.();
if (!canvas) {
return;
}
this.previousEvent = event;

const eventData = buildMouseEvent(this as never, event);
const pointerDown = new PointerEvent('pointerdown', {
Expand All @@ -487,6 +486,8 @@ export class NSCCanvas extends NSView {
...eventData,
});

this.previousEvent = { ...eventData };

canvas.dispatchEvent(pointerDown);

if (pointerDown[isCancelled_]) {
Expand All @@ -503,9 +504,8 @@ export class NSCCanvas extends NSView {
if (!canvas) {
return;
}
this.previousEvent = event;

const eventData = buildMouseEvent(this, event);
const eventData = buildMouseEvent(this as never, event);
const pointerDown = new PointerEvent('pointerup', {
pointerId: 1,
pointerType: 'mouse',
Expand All @@ -514,6 +514,8 @@ export class NSCCanvas extends NSView {
...eventData,
});

this.previousEvent = { ...eventData };

canvas.dispatchEvent(pointerDown);

if (pointerDown[isCancelled_]) {
Expand All @@ -526,7 +528,28 @@ export class NSCCanvas extends NSView {
}

mouseDragged(event: NSEvent) {
this.previousEvent = event;
const canvas = this._canvas?.deref?.();
if (!canvas) {
return;
}
if (!canvas._pointerCapture.get(1)) {
return;
}
const eventData = buildMouseEvent(this as never, event);

const pointerDown = new PointerEvent('pointermove', {
pointerId: 1,
pointerType: 'mouse',
isPrimary: true,
cancelable: true,
...eventData,
movementX: eventData.clientX - this.previousEvent.clientX ?? 0,
movementY: eventData.clientY - this.previousEvent.clientY ?? 0,
});

this.previousEvent = eventData;

canvas.dispatchEvent(pointerDown);
}

scrollWheel(event: NSEvent) {
Expand Down Expand Up @@ -602,7 +625,10 @@ export class NSCCanvas extends NSView {
if (!canvas) {
return;
}
const eventData = buildMouseEvent(this, event);
if (canvas._pointerCapture.get(1)) {
return;
}
const eventData = buildMouseEvent(this as never, event);

// movementX: pointer.x - previousEvent.x,
// movementY: pointer.y - previousEvent.y,
Expand Down Expand Up @@ -968,12 +994,19 @@ export class Canvas extends ViewBase {
}

getRootNode() {
return this.parentNode;
return this.parentNode as never;
}

setPointerCapture() {}
_pointerCapture: Map<number, boolean> = new Map();
_lastMoved?: NSEvent;

releasePointerCapture() {}
setPointerCapture(pointerId: number) {
this._pointerCapture.set(pointerId, true);
}

releasePointerCapture(pointerId: number) {
this._pointerCapture.set(pointerId, false);
}

get lang(): string {
return NSLocale.currentLocale.languageCode;
Expand Down Expand Up @@ -1059,7 +1092,9 @@ export class Canvas extends ViewBase {
return new DOMRect(layout.left, layout.top, layout.width, layout.height) as never;
}

focus(options) {}
focus(options) {
this._canvas.becomeFirstResponder();
}

/**
*
Expand Down
4 changes: 3 additions & 1 deletion napi/canvas-napi/examples/node/main.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {run as tiny_poly_world} from './threejs/tiny_poly_world';
import {run as tsl_galaxy} from './threejs/tsl_galaxy';
const { webgpuCube, cube } = three;
import { run as damagedHelmet } from './threejs/damaged_helmet';
import { run as simplePixi } from './pixijs/simple';
// @ts-ignore
const require = createRequire(import.meta.url);

Expand Down Expand Up @@ -1545,7 +1546,8 @@ canvas.addEventListener('ready', (event) => {
// the_frantic_run_of_the_valorous_rabbit(canvas, windowDoc);
//tiny_poly_world(canvas);
// damagedHelmet(canvas);
tsl_galaxy(canvas);
// tsl_galaxy(canvas);
simplePixi(canvas);
});

windowDoc.setAttribute('styleMask', (
Expand Down
3 changes: 2 additions & 1 deletion napi/canvas-napi/examples/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"teapot": "~1.0.0",
"wgpu-matrix": "~3.3.0",
"three": "~0.171.0",
"xhr": "~2.6.0"
"xhr": "~2.6.0",
"pixi.js": "8.6.4"
},
"devDependencies": {
"tsx": "^4.19.2"
Expand Down
42 changes: 42 additions & 0 deletions napi/canvas-napi/examples/node/pixijs/adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as Pixii from 'pixi.js';
import { Canvas } from '../../../canvas';

const NSCAdapter: Pixii.Adapter = {
createCanvas(width?: number, height?: number) {
const canvas = new Canvas();
canvas.width = width;
canvas.height = height;
return canvas as never;
},
getCanvasRenderingContext2D() {
return CanvasRenderingContext2D as never;
},
getWebGLRenderingContext() {
return WebGLRenderingContext as never;
},
getNavigator() {
return {
userAgent: '',
gpu: global.navigator.gpu,
};
},
getBaseUrl() {
return import.meta.url;
},
getFontFaceSet() {
return null; //document.fonts;
},
fetch(url: RequestInfo, options?: RequestInit) {
return fetch(url, options);
},
parseXML(xml: string) {
const parser = new DOMParser();
return parser.parseFromString(xml, 'text/xml');
},
};

Pixii.DOMAdapter.set(NSCAdapter);

Pixii.Assets.setPreferences({ preferWorkers: false });

(global as any).PIXI = (global as any).window.PIXI = (global as any).PIXI || Pixii;
107 changes: 107 additions & 0 deletions napi/canvas-napi/examples/node/pixijs/simple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import './adapter';
import type { Application } from 'pixi.js';
import * as PIXI from 'pixi.js';

export async function run(canvas) {
const app = new PIXI.Application() as Application;
canvas.width = canvas.clientWidth * window.devicePixelRatio;
canvas.height = canvas.clientHeight * window.devicePixelRatio;

try {
await app.init();
console.log('done');
} catch (e) {
console.log(e);
}
/*
console.log(app);
// grab context to present
// const ctx = canvas.getContext('webgpu');
app.ticker.add((delta) => {
//if (ctx) {
// ctx.presentSurface();
//}
});
const graphics = new PIXI.Graphics();
// Rectangle
graphics.rect(50, 50, 100, 100);
graphics.fill(0xde3249);
// Rectangle + line style 1
graphics.rect(200, 50, 100, 100);
graphics.fill(0x650a5a);
graphics.stroke({ width: 2, color: 0xfeeb77 });
// Rectangle + line style 2
graphics.rect(350, 50, 100, 100);
graphics.fill(0xc34288);
graphics.stroke({ width: 10, color: 0xffbd01 });
// Rectangle 2
graphics.rect(530, 50, 140, 100);
graphics.fill(0xaa4f08);
graphics.stroke({ width: 2, color: 0xffffff });
// Circle
graphics.circle(100, 250, 50);
graphics.fill(0xde3249, 1);
// Circle + line style 1
graphics.circle(250, 250, 50);
graphics.fill(0x650a5a, 1);
graphics.stroke({ width: 2, color: 0xfeeb77 });
// Circle + line style 2
graphics.circle(400, 250, 50);
graphics.fill(0xc34288, 1);
graphics.stroke({ width: 10, color: 0xffbd01 });
// Ellipse + line style 2
graphics.ellipse(600, 250, 80, 50);
graphics.fill(0xaa4f08, 1);
graphics.stroke({ width: 2, color: 0xffffff });
// Draw a shape
graphics.moveTo(50, 350);
graphics.lineTo(250, 350);
graphics.lineTo(100, 400);
graphics.lineTo(50, 350);
graphics.fill(0xff3300);
graphics.stroke({ width: 4, color: 0xffd900 });
// Draw a rounded rectangle
graphics.roundRect(50, 440, 100, 100, 16);
graphics.fill({
color: 0x650a5a,
alpha: 0.25
});
graphics.stroke({ width: 2, color: 0xff00ff });
// Draw star
graphics.star(360, 370, 5, 50);
graphics.fill(0x35cc5a);
graphics.stroke({ width: 2, color: 0xffffff });
// Draw star 2
graphics.star(280, 510, 7, 50);
graphics.fill(0xffcc5a);
graphics.stroke({ width: 2, color: 0xfffffd });
// Draw star 3
graphics.star(470, 450, 4, 50);
graphics.fill(0x55335a);
graphics.stroke({ width: 4, color: 0xffffff });
// Draw polygon
const path = [600, 370, 700, 460, 780, 420, 730, 570, 590, 520];
graphics.poly(path);
graphics.fill(0x3500fa);
app.stage.addChild(graphics);
*/
}
6 changes: 4 additions & 2 deletions napi/canvas-napi/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Event } from '@nativescript/foundation/dom/dom-utils.js';
// @ts-ignore
const require = createRequire(import.meta.url);

const { GPU, GPUDevice, GPUAdapter, GPUTextureUsage, GPUBufferUsage } = require('./canvas-napi.darwin-arm64.node');
const { GPU, GPUDevice, GPUAdapter, GPUTextureUsage, GPUBufferUsage, CanvasRenderingContext2D, WebGLRenderingContext } = require('./canvas-napi.darwin-arm64.node');

const { requestAnimationFrame, cancelAnimationFrame } = utils;

Expand All @@ -32,7 +32,7 @@ export class ProgressEvent extends Event {
}

function installPolyfills(window) {
window.addEventListener = () => {};
globalThis.addEventListener = window.addEventListener = () => {};
Object.defineProperty(window, 'devicePixelRatio', {
value: NSScreen.mainScreen.backingScaleFactor,
writable: true,
Expand All @@ -54,6 +54,8 @@ function installPolyfills(window) {
globalThis.GPUTextureUsage = GPUTextureUsage;
globalThis.GPUBufferUsage = GPUBufferUsage;
globalThis.ProgressEvent = ProgressEvent;
globalThis.CanvasRenderingContext2D = CanvasRenderingContext2D;
globalThis.WebGLRenderingContext = WebGLRenderingContext;
}

installPolyfills(globalThis.window);
Expand Down

0 comments on commit fdfcaf9

Please sign in to comment.