canvasengine 2.0.0-beta.16 → 2.0.0-beta.17

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/package.json CHANGED
@@ -1,19 +1,20 @@
1
1
  {
2
2
  "name": "canvasengine",
3
- "version": "2.0.0-beta.16",
3
+ "version": "2.0.0-beta.17",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "dependencies": {
8
8
  "@barvynkoa/particle-emitter": "^0.0.1",
9
+ "@pixi/layout": "^3.0.2",
9
10
  "@signe/reactive": "^2.3.3",
10
11
  "howler": "^2.2.4",
11
12
  "pixi-filters": "^6.0.5",
12
13
  "pixi-viewport": "^6.0.3",
13
- "pixi.js": "^8.6.4",
14
+ "pixi.js": "^8.9.2",
14
15
  "popmotion": "^11.0.5",
15
16
  "rxjs": "^7.8.1",
16
- "yoga-layout": "^2.0.1"
17
+ "yoga-layout": "^3.2.1"
17
18
  },
18
19
  "devDependencies": {
19
20
  "@types/howler": "^2.2.11"
@@ -1,7 +1,11 @@
1
1
  import { effect, Signal, signal } from "@signe/reactive";
2
- import { Container, autoDetectRenderer } from "pixi.js";
3
- import { loadYoga } from "yoga-layout";
4
- import { Props, createComponent, registerComponent, Element } from "../engine/reactive";
2
+ import { Application, Container } from "pixi.js";
3
+ import {
4
+ Props,
5
+ createComponent,
6
+ registerComponent,
7
+ Element,
8
+ } from "../engine/reactive";
5
9
  import { useProps } from "../hooks/useProps";
6
10
  import { ComponentInstance, DisplayObject } from "./DisplayObject";
7
11
  import { ComponentFunction } from "../engine/signal";
@@ -10,12 +14,12 @@ import { Size } from "./types/DisplayObject";
10
14
  import { Scheduler, Tick } from "../directives/Scheduler";
11
15
 
12
16
  interface CanvasElement extends Element<ComponentInstance> {
13
- render: (rootElement: HTMLElement) => void;
17
+ render: (rootElement: HTMLElement, app?: Application) => void;
14
18
  directives: {
15
- tick: Scheduler
19
+ tick: Scheduler;
16
20
  };
17
21
  propObservables: {
18
- tick: Signal<Tick>
22
+ tick: Signal<Tick>;
19
23
  };
20
24
  }
21
25
 
@@ -30,33 +34,26 @@ export interface CanvasProps extends Props {
30
34
  isRoot?: boolean;
31
35
  tick?: any;
32
36
  class?: SignalOrPrimitive<string>;
37
+ background?: string;
33
38
  }
34
39
 
35
40
  export const Canvas: ComponentFunction<CanvasProps> = async (props = {}) => {
36
41
  let { cursorStyles, width, height, class: className } = useProps(props);
37
- const Yoga = await loadYoga();
38
42
 
39
- if (!props.width) width = signal<Size>(800)
40
- if (!props.height) height = signal<Size>(600)
41
-
42
- const renderer = await autoDetectRenderer({
43
- ...props,
44
- width: width?.(),
45
- height: height?.(),
46
- });
43
+ if (!props.width) width = signal<Size>(800);
44
+ if (!props.height) height = signal<Size>(600);
47
45
 
48
46
  const canvasSize = signal({
49
- width: renderer.width,
50
- height: renderer.height,
47
+ width: 0,
48
+ height: 0,
51
49
  });
52
50
 
53
51
  props.isRoot = true;
54
52
  const options: CanvasProps = {
55
53
  ...props,
56
54
  context: {
57
- Yoga,
58
- renderer,
59
55
  canvasSize,
56
+ app: signal(null),
60
57
  },
61
58
  width: width?.(),
62
59
  height: height?.(),
@@ -70,9 +67,15 @@ export const Canvas: ComponentFunction<CanvasProps> = async (props = {}) => {
70
67
  deltaRatio: 1,
71
68
  });
72
69
  }
70
+
73
71
  const canvasElement = createComponent("Canvas", options) as CanvasElement;
74
72
 
75
- canvasElement.render = (rootElement: HTMLElement) => {
73
+ canvasElement.render = (rootElement: HTMLElement, app?: Application) => {
74
+ if (!app) {
75
+ return;
76
+ }
77
+
78
+ const renderer = app.renderer;
76
79
  const canvasEl = renderer.view.canvas as HTMLCanvasElement;
77
80
 
78
81
  (globalThis as any).__PIXI_STAGE__ = canvasElement.componentInstance;
@@ -85,6 +88,34 @@ export const Canvas: ComponentFunction<CanvasProps> = async (props = {}) => {
85
88
  renderer.render(canvasElement.componentInstance as any);
86
89
  });
87
90
 
91
+ app.stage = canvasElement.componentInstance as any;
92
+
93
+ app.stage.layout = {
94
+ width: app.screen.width,
95
+ height: app.screen.height,
96
+ justifyContent: props.justifyContent,
97
+ alignItems: props.alignItems,
98
+ };
99
+
100
+ canvasSize.set({ width: app.screen.width, height: app.screen.height })
101
+
102
+ app.renderer.on('resize', (width: number, height: number) => {
103
+ canvasSize.set({ width, height });
104
+
105
+ if (app.stage.layout) {
106
+ app.stage.layout = {
107
+ width,
108
+ height
109
+ }
110
+ }
111
+ });
112
+
113
+ if (props.tickStart !== false) canvasElement.directives.tick.start();
114
+
115
+ app.ticker.add(() => {
116
+ canvasElement.propObservables!.tick();
117
+ });
118
+
88
119
  if (cursorStyles) {
89
120
  effect(() => {
90
121
  renderer.events.cursorStyles = cursorStyles();
@@ -97,37 +128,14 @@ export const Canvas: ComponentFunction<CanvasProps> = async (props = {}) => {
97
128
  });
98
129
  }
99
130
 
100
- const resizeCanvas = async () => {
101
- let w, h;
102
- if (width?.() === "100%" && height?.() === "100%") {
103
- const parent = canvasEl.parentElement;
104
- w = parent ? parent.clientWidth : window.innerWidth;
105
- h = parent ? parent.clientHeight : window.innerHeight;
106
- } else {
107
- w = width?.() ?? canvasEl.offsetWidth;
108
- h = height?.() ?? canvasEl.offsetHeight;
109
- }
110
- renderer.resize(w, h);
111
- canvasSize.set({ width: w, height: h });
112
- canvasElement.componentInstance.setWidth(w)
113
- canvasElement.componentInstance.setHeight(h)
114
- };
115
-
116
- // Listen for window resize events
117
- window.addEventListener("resize", resizeCanvas);
118
-
119
- // Check if a canvas already exists in the rootElement
120
- const existingCanvas = rootElement.querySelector('canvas');
131
+ const existingCanvas = rootElement.querySelector("canvas");
121
132
  if (existingCanvas) {
122
- // If it exists, replace it with the new canvas
123
133
  rootElement.replaceChild(canvasEl, existingCanvas);
124
134
  } else {
125
- // If it doesn't exist, append the new canvas
126
135
  rootElement.appendChild(canvasEl);
127
136
  }
128
137
 
129
- // Initial resize
130
- resizeCanvas();
138
+ options.context!.app.set(app)
131
139
  };
132
140
 
133
141
  return canvasElement;
@@ -25,8 +25,8 @@ export class CanvasContainer extends DisplayObject(PixiContainer) {
25
25
  this.sortableChildren = props.sortableChildren;
26
26
  }
27
27
  }
28
- onMount(args) {
29
- super.onMount(args);
28
+ async onMount(args) {
29
+ await super.onMount(args);
30
30
  const { componentInstance, props } = args;
31
31
  const { pixiChildren } = props;
32
32
  if (pixiChildren) {