canvasengine 2.0.0-beta.3 → 2.0.0-beta.30
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/DebugRenderer-DcvJLrrD.js +172 -0
- package/dist/DebugRenderer-DcvJLrrD.js.map +1 -0
- package/dist/components/Button.d.ts +136 -0
- package/dist/components/Button.d.ts.map +1 -0
- package/dist/components/Canvas.d.ts +18 -0
- package/dist/components/Canvas.d.ts.map +1 -0
- package/dist/components/Container.d.ts +80 -0
- package/dist/components/Container.d.ts.map +1 -0
- package/dist/components/DOMContainer.d.ts +77 -0
- package/dist/components/DOMContainer.d.ts.map +1 -0
- package/dist/components/DOMElement.d.ts +44 -0
- package/dist/components/DOMElement.d.ts.map +1 -0
- package/dist/components/DisplayObject.d.ts +82 -0
- package/dist/components/DisplayObject.d.ts.map +1 -0
- package/dist/components/Graphic.d.ts +65 -0
- package/dist/components/Graphic.d.ts.map +1 -0
- package/dist/components/Mesh.d.ts +202 -0
- package/dist/components/Mesh.d.ts.map +1 -0
- package/dist/components/NineSliceSprite.d.ts +17 -0
- package/dist/components/NineSliceSprite.d.ts.map +1 -0
- package/dist/components/ParticleEmitter.d.ts +5 -0
- package/dist/components/ParticleEmitter.d.ts.map +1 -0
- package/dist/components/Scene.d.ts +2 -0
- package/dist/components/Scene.d.ts.map +1 -0
- package/dist/components/Sprite.d.ts +174 -0
- package/dist/components/Sprite.d.ts.map +1 -0
- package/dist/components/Text.d.ts +21 -0
- package/dist/components/Text.d.ts.map +1 -0
- package/dist/components/TilingSprite.d.ts +18 -0
- package/dist/components/TilingSprite.d.ts.map +1 -0
- package/dist/components/Video.d.ts +15 -0
- package/dist/components/Video.d.ts.map +1 -0
- package/dist/components/Viewport.d.ts +106 -0
- package/dist/components/Viewport.d.ts.map +1 -0
- package/dist/components/index.d.ts +17 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/types/DisplayObject.d.ts +106 -0
- package/dist/components/types/DisplayObject.d.ts.map +1 -0
- package/dist/components/types/MouseEvent.d.ts +4 -0
- package/dist/components/types/MouseEvent.d.ts.map +1 -0
- package/dist/components/types/Spritesheet.d.ts +366 -0
- package/dist/components/types/Spritesheet.d.ts.map +1 -0
- package/dist/components/types/index.d.ts +5 -0
- package/dist/components/types/index.d.ts.map +1 -0
- package/dist/directives/Drag.d.ts +70 -0
- package/dist/directives/Drag.d.ts.map +1 -0
- package/dist/directives/KeyboardControls.d.ts +530 -0
- package/dist/directives/KeyboardControls.d.ts.map +1 -0
- package/dist/directives/Scheduler.d.ts +36 -0
- package/dist/directives/Scheduler.d.ts.map +1 -0
- package/dist/directives/Sound.d.ts +26 -0
- package/dist/directives/Sound.d.ts.map +1 -0
- package/dist/directives/Transition.d.ts +11 -0
- package/dist/directives/Transition.d.ts.map +1 -0
- package/dist/directives/ViewportCull.d.ts +12 -0
- package/dist/directives/ViewportCull.d.ts.map +1 -0
- package/dist/directives/ViewportFollow.d.ts +19 -0
- package/dist/directives/ViewportFollow.d.ts.map +1 -0
- package/dist/directives/index.d.ts +2 -0
- package/dist/directives/index.d.ts.map +1 -0
- package/dist/engine/animation.d.ts +59 -0
- package/dist/engine/animation.d.ts.map +1 -0
- package/dist/engine/bootstrap.d.ts +16 -0
- package/dist/engine/bootstrap.d.ts.map +1 -0
- package/dist/engine/directive.d.ts +14 -0
- package/dist/engine/directive.d.ts.map +1 -0
- package/dist/engine/reactive.d.ts +95 -0
- package/dist/engine/reactive.d.ts.map +1 -0
- package/dist/engine/signal.d.ts +72 -0
- package/dist/engine/signal.d.ts.map +1 -0
- package/dist/engine/trigger.d.ts +51 -0
- package/dist/engine/trigger.d.ts.map +1 -0
- package/dist/engine/utils.d.ts +90 -0
- package/dist/engine/utils.d.ts.map +1 -0
- package/dist/hooks/addContext.d.ts +2 -0
- package/dist/hooks/addContext.d.ts.map +1 -0
- package/dist/hooks/useProps.d.ts +42 -0
- package/dist/hooks/useProps.d.ts.map +1 -0
- package/dist/hooks/useRef.d.ts +5 -0
- package/dist/hooks/useRef.d.ts.map +1 -0
- package/dist/index-C-iY-lCt.js +11080 -0
- package/dist/index-C-iY-lCt.js.map +1 -0
- package/dist/index.d.ts +15 -919
- package/dist/index.d.ts.map +1 -0
- package/dist/index.global.js +29 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +63 -2950
- package/dist/index.js.map +1 -1
- package/dist/utils/Ease.d.ts +17 -0
- package/dist/utils/Ease.d.ts.map +1 -0
- package/dist/utils/RadialGradient.d.ts +58 -0
- package/dist/utils/RadialGradient.d.ts.map +1 -0
- package/dist/utils/functions.d.ts +2 -0
- package/dist/utils/functions.d.ts.map +1 -0
- package/index.d.ts +4 -0
- package/package.json +12 -7
- package/src/components/Button.ts +269 -0
- package/src/components/Canvas.ts +53 -45
- package/src/components/Container.ts +2 -2
- package/src/components/DOMContainer.ts +123 -0
- package/src/components/DOMElement.ts +421 -0
- package/src/components/DisplayObject.ts +283 -190
- package/src/components/Graphic.ts +200 -34
- package/src/components/Mesh.ts +222 -0
- package/src/components/NineSliceSprite.ts +4 -1
- package/src/components/ParticleEmitter.ts +12 -8
- package/src/components/Sprite.ts +92 -22
- package/src/components/Text.ts +34 -14
- package/src/components/Video.ts +110 -0
- package/src/components/Viewport.ts +59 -43
- package/src/components/index.ts +7 -2
- package/src/components/types/DisplayObject.ts +30 -0
- package/src/directives/Drag.ts +357 -52
- package/src/directives/KeyboardControls.ts +3 -1
- package/src/directives/Sound.ts +94 -31
- package/src/directives/ViewportFollow.ts +35 -7
- package/src/engine/animation.ts +41 -5
- package/src/engine/bootstrap.ts +23 -3
- package/src/engine/directive.ts +2 -2
- package/src/engine/reactive.ts +472 -172
- package/src/engine/signal.ts +18 -2
- package/src/engine/trigger.ts +65 -9
- package/src/engine/utils.ts +97 -9
- package/src/hooks/useProps.ts +1 -1
- package/src/index.ts +4 -1
- package/src/utils/RadialGradient.ts +29 -0
- package/src/utils/functions.ts +7 -0
- package/testing/index.ts +12 -0
- package/tsconfig.json +17 -0
- package/vite.config.ts +39 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { effect, signal, computed } from "@signe/reactive";
|
|
2
|
+
import { FederatedPointerEvent } from "pixi.js";
|
|
3
|
+
import { h } from "../engine/signal";
|
|
4
|
+
import { useDefineProps } from "../hooks/useProps";
|
|
5
|
+
import { Container } from "./Container";
|
|
6
|
+
import { Rect } from "./Graphic";
|
|
7
|
+
import { Text } from "./Text";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Button states for visual feedback
|
|
11
|
+
*/
|
|
12
|
+
export enum ButtonState {
|
|
13
|
+
Normal = "normal",
|
|
14
|
+
Hover = "hover",
|
|
15
|
+
Pressed = "pressed",
|
|
16
|
+
Disabled = "disabled"
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Button style configuration for different visual approaches
|
|
21
|
+
*/
|
|
22
|
+
export interface ButtonStyle {
|
|
23
|
+
/** Background color for each state */
|
|
24
|
+
backgroundColor?: {
|
|
25
|
+
[ButtonState.Normal]?: string;
|
|
26
|
+
[ButtonState.Hover]?: string;
|
|
27
|
+
[ButtonState.Pressed]?: string;
|
|
28
|
+
[ButtonState.Disabled]?: string;
|
|
29
|
+
};
|
|
30
|
+
/** Border configuration */
|
|
31
|
+
border?: {
|
|
32
|
+
color?: string;
|
|
33
|
+
width?: number;
|
|
34
|
+
radius?: number;
|
|
35
|
+
};
|
|
36
|
+
/** Text styling */
|
|
37
|
+
text?: {
|
|
38
|
+
color?: string;
|
|
39
|
+
fontSize?: number;
|
|
40
|
+
fontFamily?: string;
|
|
41
|
+
};
|
|
42
|
+
/** Sprite textures for each state (alternative to backgroundColor) */
|
|
43
|
+
textures?: {
|
|
44
|
+
[ButtonState.Normal]?: string;
|
|
45
|
+
[ButtonState.Hover]?: string;
|
|
46
|
+
[ButtonState.Pressed]?: string;
|
|
47
|
+
[ButtonState.Disabled]?: string;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Properties for the Button component
|
|
53
|
+
*/
|
|
54
|
+
export interface ButtonProps {
|
|
55
|
+
/** Button text content */
|
|
56
|
+
text?: string;
|
|
57
|
+
/** Button disabled state */
|
|
58
|
+
disabled?: boolean;
|
|
59
|
+
/** Click event handler */
|
|
60
|
+
click?: (event: FederatedPointerEvent) => void;
|
|
61
|
+
/** Hover enter event handler */
|
|
62
|
+
hoverEnter?: (event: FederatedPointerEvent) => void;
|
|
63
|
+
/** Hover leave event handler */
|
|
64
|
+
hoverLeave?: (event: FederatedPointerEvent) => void;
|
|
65
|
+
/** Press down event handler */
|
|
66
|
+
pressDown?: (event: FederatedPointerEvent) => void;
|
|
67
|
+
/** Press up event handler */
|
|
68
|
+
pressUp?: (event: FederatedPointerEvent) => void;
|
|
69
|
+
/** Visual style configuration */
|
|
70
|
+
style?: ButtonStyle;
|
|
71
|
+
/** Button width */
|
|
72
|
+
width?: number;
|
|
73
|
+
/** Button height */
|
|
74
|
+
height?: number;
|
|
75
|
+
/** Button position X */
|
|
76
|
+
x?: number;
|
|
77
|
+
/** Button position Y */
|
|
78
|
+
y?: number;
|
|
79
|
+
/** Button alpha/opacity */
|
|
80
|
+
alpha?: number;
|
|
81
|
+
/** Button visibility */
|
|
82
|
+
visible?: boolean;
|
|
83
|
+
/** Button cursor */
|
|
84
|
+
cursor?: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Creates a Button component with interactive states and customizable styling.
|
|
89
|
+
*
|
|
90
|
+
* This component provides a fully interactive button with visual feedback
|
|
91
|
+
* for different states (normal, hover, pressed, disabled). It supports both
|
|
92
|
+
* sprite-based and graphics-based rendering approaches.
|
|
93
|
+
*
|
|
94
|
+
* The button is built using a Container with background and text elements,
|
|
95
|
+
* providing reactive state management and event handling.
|
|
96
|
+
*
|
|
97
|
+
* @param props - Button configuration including text, styling, and event handlers
|
|
98
|
+
* @returns A reactive Button component
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* // Simple button with text and click handler
|
|
102
|
+
* const simpleButton = Button({
|
|
103
|
+
* text: "Click Me",
|
|
104
|
+
* onClick: () => console.log("Button clicked!"),
|
|
105
|
+
* width: 150,
|
|
106
|
+
* height: 50
|
|
107
|
+
* });
|
|
108
|
+
*
|
|
109
|
+
* // Styled button with custom colors
|
|
110
|
+
* const styledButton = Button({
|
|
111
|
+
* text: "Styled Button",
|
|
112
|
+
* style: {
|
|
113
|
+
* backgroundColor: {
|
|
114
|
+
* normal: "#28a745",
|
|
115
|
+
* hover: "#218838",
|
|
116
|
+
* pressed: "#1e7e34",
|
|
117
|
+
* disabled: "#6c757d"
|
|
118
|
+
* },
|
|
119
|
+
* border: {
|
|
120
|
+
* radius: 8,
|
|
121
|
+
* width: 2,
|
|
122
|
+
* color: "#ffffff"
|
|
123
|
+
* },
|
|
124
|
+
* text: {
|
|
125
|
+
* fontSize: 18,
|
|
126
|
+
* color: "#ffffff"
|
|
127
|
+
* }
|
|
128
|
+
* }
|
|
129
|
+
* });
|
|
130
|
+
*
|
|
131
|
+
* // Sprite-based button
|
|
132
|
+
* const spriteButton = Button({
|
|
133
|
+
* text: "Play Game",
|
|
134
|
+
* style: {
|
|
135
|
+
* textures: {
|
|
136
|
+
* normal: "/assets/button-normal.png",
|
|
137
|
+
* hover: "/assets/button-hover.png",
|
|
138
|
+
* pressed: "/assets/button-pressed.png"
|
|
139
|
+
* }
|
|
140
|
+
* }
|
|
141
|
+
* });
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export function Button(props: ButtonProps) {
|
|
145
|
+
// Internal state signals
|
|
146
|
+
const currentState = signal(ButtonState.Normal);
|
|
147
|
+
const isPressed = signal(false);
|
|
148
|
+
const isHovered = signal(false);
|
|
149
|
+
|
|
150
|
+
// Define reactive props with defaults
|
|
151
|
+
const defineProps = useDefineProps(props);
|
|
152
|
+
const { text, disabled, width, height, style } = defineProps({
|
|
153
|
+
text: {
|
|
154
|
+
type: String,
|
|
155
|
+
default: ""
|
|
156
|
+
},
|
|
157
|
+
disabled: {
|
|
158
|
+
type: Boolean,
|
|
159
|
+
default: false
|
|
160
|
+
},
|
|
161
|
+
width: {
|
|
162
|
+
type: Number,
|
|
163
|
+
default: 120
|
|
164
|
+
},
|
|
165
|
+
height: {
|
|
166
|
+
type: Number,
|
|
167
|
+
default: 40
|
|
168
|
+
},
|
|
169
|
+
style: {
|
|
170
|
+
type: Object,
|
|
171
|
+
default: () => ({})
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
// Update button state based on disabled and interaction states
|
|
176
|
+
effect(() => {
|
|
177
|
+
const isDisabled = disabled();
|
|
178
|
+
const pressed = isPressed();
|
|
179
|
+
const hovered = isHovered();
|
|
180
|
+
|
|
181
|
+
if (isDisabled) {
|
|
182
|
+
currentState.set(ButtonState.Disabled);
|
|
183
|
+
} else if (pressed) {
|
|
184
|
+
currentState.set(ButtonState.Pressed);
|
|
185
|
+
} else if (hovered) {
|
|
186
|
+
currentState.set(ButtonState.Hover);
|
|
187
|
+
} else {
|
|
188
|
+
currentState.set(ButtonState.Normal);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// Event handlers
|
|
193
|
+
const eventHandlers = {
|
|
194
|
+
pointerenter: (event: FederatedPointerEvent) => {
|
|
195
|
+
if (!disabled()) {
|
|
196
|
+
isHovered.set(true);
|
|
197
|
+
props.hoverEnter?.(event);
|
|
198
|
+
}
|
|
199
|
+
},
|
|
200
|
+
pointerleave: (event: FederatedPointerEvent) => {
|
|
201
|
+
isHovered.set(false);
|
|
202
|
+
isPressed.set(false);
|
|
203
|
+
props.hoverLeave?.(event);
|
|
204
|
+
},
|
|
205
|
+
pointerdown: (event: FederatedPointerEvent) => {
|
|
206
|
+
if (!disabled()) {
|
|
207
|
+
isPressed.set(true);
|
|
208
|
+
props.pressDown?.(event);
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
pointerup: (event: FederatedPointerEvent) => {
|
|
212
|
+
if (!disabled() && isPressed()) {
|
|
213
|
+
isPressed.set(false);
|
|
214
|
+
props.pressUp?.(event);
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
pointertap: (event: FederatedPointerEvent) => {
|
|
218
|
+
if (!disabled()) {
|
|
219
|
+
props.click?.(event);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
// Return Container with h() children
|
|
225
|
+
return h(Container, {
|
|
226
|
+
x: props.x,
|
|
227
|
+
y: props.y,
|
|
228
|
+
width: props.width,
|
|
229
|
+
height: props.height,
|
|
230
|
+
alpha: props.alpha,
|
|
231
|
+
visible: props.visible,
|
|
232
|
+
cursor: props.cursor || "pointer",
|
|
233
|
+
...eventHandlers
|
|
234
|
+
}, [
|
|
235
|
+
// Background element (either sprite or graphics)
|
|
236
|
+
h(Rect, {
|
|
237
|
+
width: width,
|
|
238
|
+
height: height,
|
|
239
|
+
color: computed(() => {
|
|
240
|
+
const currentStyle = style();
|
|
241
|
+
const backgroundColor = currentStyle.backgroundColor || {
|
|
242
|
+
[ButtonState.Normal]: "#007bff",
|
|
243
|
+
[ButtonState.Hover]: "#0056b3",
|
|
244
|
+
[ButtonState.Pressed]: "#004085",
|
|
245
|
+
[ButtonState.Disabled]: "#6c757d"
|
|
246
|
+
};
|
|
247
|
+
const state = currentState();
|
|
248
|
+
return backgroundColor[state] || backgroundColor[ButtonState.Normal];
|
|
249
|
+
})
|
|
250
|
+
}),
|
|
251
|
+
|
|
252
|
+
// Text element
|
|
253
|
+
h(Text, {
|
|
254
|
+
text: text,
|
|
255
|
+
x: computed(() => width() / 2),
|
|
256
|
+
y: computed(() => height() / 2),
|
|
257
|
+
anchor: { x: 0.5, y: 0.5 },
|
|
258
|
+
style: computed(() => {
|
|
259
|
+
const currentStyle = style();
|
|
260
|
+
const textStyle = currentStyle.text || {};
|
|
261
|
+
return {
|
|
262
|
+
fontSize: textStyle.fontSize || 16,
|
|
263
|
+
fontFamily: textStyle.fontFamily || "Arial",
|
|
264
|
+
fill: textStyle.color || "#ffffff"
|
|
265
|
+
};
|
|
266
|
+
})()
|
|
267
|
+
})
|
|
268
|
+
]);
|
|
269
|
+
}
|
package/src/components/Canvas.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { effect, Signal, signal } from "@signe/reactive";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
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:
|
|
50
|
-
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
|
|
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
|
-
|
|
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
|
+
};
|