canvasengine 2.0.0-beta.2 → 2.0.0-beta.21

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.
Files changed (41) hide show
  1. package/dist/index.d.ts +1289 -0
  2. package/dist/index.js +4248 -0
  3. package/dist/index.js.map +1 -0
  4. package/index.d.ts +4 -0
  5. package/package.json +5 -12
  6. package/src/components/Canvas.ts +53 -45
  7. package/src/components/Container.ts +2 -2
  8. package/src/components/DOMContainer.ts +123 -0
  9. package/src/components/DOMElement.ts +421 -0
  10. package/src/components/DisplayObject.ts +263 -189
  11. package/src/components/Graphic.ts +213 -36
  12. package/src/components/Mesh.ts +222 -0
  13. package/src/components/NineSliceSprite.ts +4 -1
  14. package/src/components/ParticleEmitter.ts +12 -8
  15. package/src/components/Sprite.ts +77 -14
  16. package/src/components/Text.ts +34 -14
  17. package/src/components/Video.ts +110 -0
  18. package/src/components/Viewport.ts +59 -43
  19. package/src/components/index.ts +6 -4
  20. package/src/components/types/DisplayObject.ts +30 -0
  21. package/src/directives/Drag.ts +357 -52
  22. package/src/directives/KeyboardControls.ts +3 -1
  23. package/src/directives/Sound.ts +94 -31
  24. package/src/directives/ViewportFollow.ts +35 -7
  25. package/src/engine/animation.ts +41 -5
  26. package/src/engine/bootstrap.ts +22 -3
  27. package/src/engine/directive.ts +2 -2
  28. package/src/engine/reactive.ts +337 -168
  29. package/src/engine/trigger.ts +65 -9
  30. package/src/engine/utils.ts +97 -9
  31. package/src/hooks/useProps.ts +1 -1
  32. package/src/index.ts +5 -1
  33. package/src/utils/RadialGradient.ts +29 -0
  34. package/src/utils/functions.ts +7 -0
  35. package/testing/index.ts +12 -0
  36. package/src/components/DrawMap/index.ts +0 -65
  37. package/src/components/Tilemap/Tile.ts +0 -79
  38. package/src/components/Tilemap/TileGroup.ts +0 -207
  39. package/src/components/Tilemap/TileLayer.ts +0 -163
  40. package/src/components/Tilemap/TileSet.ts +0 -41
  41. package/src/components/Tilemap/index.ts +0 -80
package/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ declare module '*.ce' {
2
+ const content: import("./dist/index").ComponentFunction;
3
+ export default content;
4
+ }
package/package.json CHANGED
@@ -1,22 +1,20 @@
1
1
  {
2
2
  "name": "canvasengine",
3
- "version": "2.0.0-beta.2",
3
+ "version": "2.0.0-beta.21",
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/tilemap": "^5.0.1",
10
- "@rpgjs/tiled": "^4.3.0",
11
- "@signe/reactive": "^1.0.1",
9
+ "@pixi/layout": "^3.0.2",
10
+ "@signe/reactive": "^2.3.3",
12
11
  "howler": "^2.2.4",
13
- "package.json": "^2.0.1",
14
12
  "pixi-filters": "^6.0.5",
15
13
  "pixi-viewport": "^6.0.3",
16
- "pixi.js": "^8.6.4",
14
+ "pixi.js": "^8.9.2",
17
15
  "popmotion": "^11.0.5",
18
16
  "rxjs": "^7.8.1",
19
- "yoga-layout": "^2.0.1"
17
+ "yoga-layout": "^3.2.1"
20
18
  },
21
19
  "devDependencies": {
22
20
  "@types/howler": "^2.2.11"
@@ -40,11 +38,6 @@
40
38
  "publishConfig": {
41
39
  "access": "public"
42
40
  },
43
- "overrides": {
44
- "revolt-fx": {
45
- "pixi.js": "^8.6.4"
46
- }
47
- },
48
41
  "scripts": {
49
42
  "build": "tsup",
50
43
  "dev": "tsup --watch"
@@ -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) {
@@ -0,0 +1,123 @@
1
+ import { DOMContainer as PixiDOMContainer } from "pixi.js";
2
+ import {
3
+ createComponent,
4
+ Element,
5
+ registerComponent,
6
+ } from "../engine/reactive";
7
+ import { ComponentInstance, DisplayObject } from "./DisplayObject";
8
+ import { ComponentFunction, h } from "../engine/signal";
9
+ import { DisplayObjectProps } from "./types/DisplayObject";
10
+ import { CanvasDOMElement, DOMElement } from "./DOMElement";
11
+
12
+
13
+ /**
14
+ * DOMContainer class for managing DOM elements within the canvas engine
15
+ *
16
+ * This class extends the DisplayObject functionality to handle DOM elements using
17
+ * PixiJS's native DOMContainer. It provides a bridge between the canvas rendering
18
+ * system and traditional DOM manipulation with proper transform hierarchy and visibility.
19
+ *
20
+ * The DOMContainer is especially useful for rendering standard DOM elements that handle
21
+ * user input, such as `<input>` or `<textarea>`. This is often simpler and more flexible
22
+ * than trying to implement text input directly in PixiJS.
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * // Basic usage with input element
27
+ * const element = document.createElement('input');
28
+ * element.type = 'text';
29
+ * element.placeholder = 'Enter text...';
30
+ *
31
+ * const domContainer = new DOMContainer({
32
+ * element,
33
+ * x: 100,
34
+ * y: 50,
35
+ * anchor: { x: 0.5, y: 0.5 }
36
+ * });
37
+ *
38
+ * // Using different class and style formats
39
+ * const containerWithClasses = new DOMContainer({
40
+ * element: 'div',
41
+ * attrs: {
42
+ * // String format: space-separated classes
43
+ * class: 'container primary-theme',
44
+ *
45
+ * // Array format: array of class names
46
+ * // class: ['container', 'primary-theme'],
47
+ *
48
+ * // Object format: conditional classes
49
+ * // class: {
50
+ * // 'container': true,
51
+ * // 'primary-theme': true,
52
+ * // 'disabled': false
53
+ * // }
54
+ *
55
+ * // String format: CSS style string
56
+ * style: 'background-color: red; padding: 10px;',
57
+ *
58
+ * // Object format: style properties
59
+ * // style: {
60
+ * // backgroundColor: 'red',
61
+ * // padding: '10px',
62
+ * // fontSize: 16
63
+ * // }
64
+ * }
65
+ * });
66
+ * ```
67
+ */
68
+ const EVENTS = [
69
+ "click",
70
+ "mouseover",
71
+ "mouseout",
72
+ "mouseenter",
73
+ "mouseleave",
74
+ "mousemove",
75
+ "mouseup",
76
+ "mousedown",
77
+ "touchstart",
78
+ "touchend",
79
+ "touchmove",
80
+ "touchcancel",
81
+ "wheel",
82
+ "scroll",
83
+ "resize",
84
+ "focus",
85
+ "blur",
86
+ "change",
87
+ "input",
88
+ "submit",
89
+ "reset",
90
+ "keydown",
91
+ "keyup",
92
+ "keypress",
93
+ "contextmenu",
94
+ "drag",
95
+ "dragend",
96
+ "dragenter",
97
+ "dragleave",
98
+ "dragover",
99
+ "drop",
100
+ "dragstart",
101
+ "select",
102
+ "selectstart",
103
+ "selectend",
104
+ "selectall",
105
+ "selectnone",
106
+ ];
107
+
108
+ export class CanvasDOMContainer extends DisplayObject(PixiDOMContainer) {
109
+ disableLayout = true;
110
+
111
+ onInit(props: any) {
112
+ const div = h(DOMElement, { element: "div" }, props.children) as unknown as Element<CanvasDOMElement>;
113
+ this.element = div.componentInstance.element;
114
+ }
115
+ }
116
+
117
+ export interface CanvasDOMContainer extends DisplayObjectProps {}
118
+
119
+ registerComponent("DOMContainer", CanvasDOMContainer);
120
+
121
+ export const DOMContainer: ComponentFunction<any> = (props) => {
122
+ return createComponent("DOMContainer", props);
123
+ };