@vertz/ui-canvas 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +65 -0
- package/dist/index.js +75 -0
- package/package.json +48 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Application, Container } from "pixi";
|
|
2
|
+
import { Signal, DisposeFn } from "@vertz/ui";
|
|
3
|
+
interface CanvasOptions {
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
backgroundColor?: number;
|
|
7
|
+
}
|
|
8
|
+
interface CanvasState {
|
|
9
|
+
app: Application;
|
|
10
|
+
root: Container;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Binds a Vertz signal to a PixiJS display object property reactively.
|
|
14
|
+
* When the signal updates, the display object property updates automatically.
|
|
15
|
+
*/
|
|
16
|
+
declare function bindSignal<T>(sig: Signal<T>, displayObject: {
|
|
17
|
+
[key: string]: unknown;
|
|
18
|
+
}, property: string, transform?: (value: T) => unknown): DisposeFn;
|
|
19
|
+
/**
|
|
20
|
+
* Creates a reactive sprite-like display object that binds to position signals.
|
|
21
|
+
* Returns the display object with bound properties and a cleanup function.
|
|
22
|
+
*/
|
|
23
|
+
declare function createReactiveSprite(options: {
|
|
24
|
+
x?: Signal<number>;
|
|
25
|
+
y?: Signal<number>;
|
|
26
|
+
rotation?: Signal<number>;
|
|
27
|
+
scaleX?: Signal<number>;
|
|
28
|
+
scaleY?: Signal<number>;
|
|
29
|
+
alpha?: Signal<number>;
|
|
30
|
+
}, displayObject: {
|
|
31
|
+
x: number;
|
|
32
|
+
y: number;
|
|
33
|
+
rotation: number;
|
|
34
|
+
scale: {
|
|
35
|
+
x: number;
|
|
36
|
+
y: number;
|
|
37
|
+
};
|
|
38
|
+
alpha: number;
|
|
39
|
+
}): {
|
|
40
|
+
displayObject: typeof displayObject;
|
|
41
|
+
dispose: DisposeFn;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Renders a PixiJS canvas to the specified container element.
|
|
45
|
+
* Returns the canvas element and a dispose function for cleanup.
|
|
46
|
+
*/
|
|
47
|
+
declare function render(container: HTMLElement, options: CanvasOptions): {
|
|
48
|
+
canvas: HTMLCanvasElement;
|
|
49
|
+
dispose: DisposeFn;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Destroy a PixiJS application and remove its canvas from the DOM.
|
|
53
|
+
*/
|
|
54
|
+
declare function destroy(app: Application, container: HTMLElement): void;
|
|
55
|
+
/**
|
|
56
|
+
* Canvas renderer for Vertz with PixiJS integration.
|
|
57
|
+
* Provides reactive primitives for canvas rendering.
|
|
58
|
+
*/
|
|
59
|
+
declare const Canvas: {
|
|
60
|
+
render: typeof render;
|
|
61
|
+
bindSignal: typeof bindSignal;
|
|
62
|
+
createReactiveSprite: typeof createReactiveSprite;
|
|
63
|
+
destroy: typeof destroy;
|
|
64
|
+
};
|
|
65
|
+
export { render, destroy, createReactiveSprite, bindSignal, CanvasState, CanvasOptions, Canvas };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// src/canvas.ts
|
|
2
|
+
import { Application } from "pixi.js";
|
|
3
|
+
import { effect } from "@vertz/ui";
|
|
4
|
+
function bindSignal(sig, displayObject, property, transform) {
|
|
5
|
+
const update = () => {
|
|
6
|
+
const value = transform ? transform(sig.value) : sig.value;
|
|
7
|
+
displayObject[property] = value;
|
|
8
|
+
};
|
|
9
|
+
update();
|
|
10
|
+
const disposeEffect = effect(() => {
|
|
11
|
+
sig.value;
|
|
12
|
+
update();
|
|
13
|
+
});
|
|
14
|
+
return disposeEffect;
|
|
15
|
+
}
|
|
16
|
+
function createReactiveSprite(options, displayObject) {
|
|
17
|
+
const cleanups = [];
|
|
18
|
+
if (options.x) {
|
|
19
|
+
cleanups.push(bindSignal(options.x, displayObject, "x"));
|
|
20
|
+
}
|
|
21
|
+
if (options.y) {
|
|
22
|
+
cleanups.push(bindSignal(options.y, displayObject, "y"));
|
|
23
|
+
}
|
|
24
|
+
if (options.rotation) {
|
|
25
|
+
cleanups.push(bindSignal(options.rotation, displayObject, "rotation"));
|
|
26
|
+
}
|
|
27
|
+
if (options.scaleX) {
|
|
28
|
+
cleanups.push(bindSignal(options.scaleX, displayObject.scale, "x"));
|
|
29
|
+
}
|
|
30
|
+
if (options.scaleY) {
|
|
31
|
+
cleanups.push(bindSignal(options.scaleY, displayObject.scale, "y"));
|
|
32
|
+
}
|
|
33
|
+
if (options.alpha) {
|
|
34
|
+
cleanups.push(bindSignal(options.alpha, displayObject, "alpha"));
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
displayObject,
|
|
38
|
+
dispose: () => cleanups.forEach((fn) => fn())
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function render(container, options) {
|
|
42
|
+
const app = new Application({
|
|
43
|
+
width: options.width,
|
|
44
|
+
height: options.height,
|
|
45
|
+
backgroundColor: options.backgroundColor ?? 0
|
|
46
|
+
});
|
|
47
|
+
container.appendChild(app.view);
|
|
48
|
+
const dispose = () => {
|
|
49
|
+
destroy(app, container);
|
|
50
|
+
};
|
|
51
|
+
return {
|
|
52
|
+
canvas: app.view,
|
|
53
|
+
dispose
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function destroy(app, container) {
|
|
57
|
+
const view = app.view;
|
|
58
|
+
if (view && container.contains(view)) {
|
|
59
|
+
container.removeChild(view);
|
|
60
|
+
}
|
|
61
|
+
app.destroy(true, { children: true, texture: true });
|
|
62
|
+
}
|
|
63
|
+
var Canvas = {
|
|
64
|
+
render,
|
|
65
|
+
bindSignal,
|
|
66
|
+
createReactiveSprite,
|
|
67
|
+
destroy
|
|
68
|
+
};
|
|
69
|
+
export {
|
|
70
|
+
render,
|
|
71
|
+
destroy,
|
|
72
|
+
createReactiveSprite,
|
|
73
|
+
bindSignal,
|
|
74
|
+
Canvas
|
|
75
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vertz/ui-canvas",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"description": "Vertz Canvas renderer with PixiJS and signals",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/vertz-dev/vertz.git",
|
|
10
|
+
"directory": "packages/ui-canvas"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public",
|
|
14
|
+
"provenance": true
|
|
15
|
+
},
|
|
16
|
+
"main": "dist/index.js",
|
|
17
|
+
"types": "dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "bunup",
|
|
29
|
+
"test": "vitest run",
|
|
30
|
+
"test:watch": "vitest",
|
|
31
|
+
"typecheck": "tsc --noEmit"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@vertz/ui": "workspace:*",
|
|
35
|
+
"pixi.js": "^8.0.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
39
|
+
"bunup": "latest",
|
|
40
|
+
"happy-dom": "^18.0.1",
|
|
41
|
+
"typescript": "^5.7.0",
|
|
42
|
+
"vitest": "^4.0.18"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=22"
|
|
46
|
+
},
|
|
47
|
+
"sideEffects": false
|
|
48
|
+
}
|