canvasengine 2.0.0-beta.4 → 2.0.0-beta.41

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 (142) hide show
  1. package/dist/DebugRenderer-BxfW34YG.js +172 -0
  2. package/dist/DebugRenderer-BxfW34YG.js.map +1 -0
  3. package/dist/components/Button.d.ts +183 -0
  4. package/dist/components/Button.d.ts.map +1 -0
  5. package/dist/components/Canvas.d.ts +18 -0
  6. package/dist/components/Canvas.d.ts.map +1 -0
  7. package/dist/components/DOMElement.d.ts +44 -0
  8. package/dist/components/DOMElement.d.ts.map +1 -0
  9. package/dist/components/Graphic.d.ts +65 -0
  10. package/dist/components/Graphic.d.ts.map +1 -0
  11. package/dist/components/Joystick.d.ts +36 -0
  12. package/dist/components/Joystick.d.ts.map +1 -0
  13. package/dist/components/NineSliceSprite.d.ts +17 -0
  14. package/dist/components/NineSliceSprite.d.ts.map +1 -0
  15. package/dist/components/ParticleEmitter.d.ts +5 -0
  16. package/dist/components/ParticleEmitter.d.ts.map +1 -0
  17. package/dist/components/Scene.d.ts +2 -0
  18. package/dist/components/Scene.d.ts.map +1 -0
  19. package/dist/components/Text.d.ts +26 -0
  20. package/dist/components/Text.d.ts.map +1 -0
  21. package/dist/components/TilingSprite.d.ts +18 -0
  22. package/dist/components/TilingSprite.d.ts.map +1 -0
  23. package/dist/components/Video.d.ts +15 -0
  24. package/dist/components/Video.d.ts.map +1 -0
  25. package/dist/components/index.d.ts +18 -0
  26. package/dist/components/index.d.ts.map +1 -0
  27. package/dist/components/types/DisplayObject.d.ts +110 -0
  28. package/dist/components/types/DisplayObject.d.ts.map +1 -0
  29. package/dist/components/types/MouseEvent.d.ts +4 -0
  30. package/dist/components/types/MouseEvent.d.ts.map +1 -0
  31. package/dist/components/types/Spritesheet.d.ts +248 -0
  32. package/dist/components/types/Spritesheet.d.ts.map +1 -0
  33. package/dist/components/types/index.d.ts +5 -0
  34. package/dist/components/types/index.d.ts.map +1 -0
  35. package/dist/directives/Controls.d.ts +113 -0
  36. package/dist/directives/Controls.d.ts.map +1 -0
  37. package/dist/directives/ControlsBase.d.ts +198 -0
  38. package/dist/directives/ControlsBase.d.ts.map +1 -0
  39. package/dist/directives/Drag.d.ts +70 -0
  40. package/dist/directives/Drag.d.ts.map +1 -0
  41. package/dist/directives/Flash.d.ts +117 -0
  42. package/dist/directives/Flash.d.ts.map +1 -0
  43. package/dist/directives/GamepadControls.d.ts +225 -0
  44. package/dist/directives/GamepadControls.d.ts.map +1 -0
  45. package/dist/directives/JoystickControls.d.ts +172 -0
  46. package/dist/directives/JoystickControls.d.ts.map +1 -0
  47. package/dist/directives/KeyboardControls.d.ts +219 -0
  48. package/dist/directives/KeyboardControls.d.ts.map +1 -0
  49. package/dist/directives/Scheduler.d.ts +36 -0
  50. package/dist/directives/Scheduler.d.ts.map +1 -0
  51. package/dist/directives/Shake.d.ts +98 -0
  52. package/dist/directives/Shake.d.ts.map +1 -0
  53. package/dist/directives/Sound.d.ts +26 -0
  54. package/dist/directives/Sound.d.ts.map +1 -0
  55. package/dist/directives/Transition.d.ts +11 -0
  56. package/dist/directives/Transition.d.ts.map +1 -0
  57. package/dist/directives/ViewportCull.d.ts +12 -0
  58. package/dist/directives/ViewportCull.d.ts.map +1 -0
  59. package/dist/directives/ViewportFollow.d.ts +19 -0
  60. package/dist/directives/ViewportFollow.d.ts.map +1 -0
  61. package/dist/directives/index.d.ts +13 -0
  62. package/dist/directives/index.d.ts.map +1 -0
  63. package/dist/engine/animation.d.ts +73 -0
  64. package/dist/engine/animation.d.ts.map +1 -0
  65. package/dist/engine/bootstrap.d.ts +16 -0
  66. package/dist/engine/bootstrap.d.ts.map +1 -0
  67. package/dist/engine/directive.d.ts +14 -0
  68. package/dist/engine/directive.d.ts.map +1 -0
  69. package/dist/engine/reactive.d.ts +105 -0
  70. package/dist/engine/reactive.d.ts.map +1 -0
  71. package/dist/engine/signal.d.ts +72 -0
  72. package/dist/engine/signal.d.ts.map +1 -0
  73. package/dist/engine/trigger.d.ts +54 -0
  74. package/dist/engine/trigger.d.ts.map +1 -0
  75. package/dist/engine/utils.d.ts +90 -0
  76. package/dist/engine/utils.d.ts.map +1 -0
  77. package/dist/hooks/addContext.d.ts +2 -0
  78. package/dist/hooks/addContext.d.ts.map +1 -0
  79. package/dist/hooks/useProps.d.ts +42 -0
  80. package/dist/hooks/useProps.d.ts.map +1 -0
  81. package/dist/hooks/useRef.d.ts +5 -0
  82. package/dist/hooks/useRef.d.ts.map +1 -0
  83. package/dist/index-BnuKipxl.js +12568 -0
  84. package/dist/index-BnuKipxl.js.map +1 -0
  85. package/dist/index.d.ts +15 -1083
  86. package/dist/index.d.ts.map +1 -0
  87. package/dist/index.global.js +29 -0
  88. package/dist/index.global.js.map +1 -0
  89. package/dist/index.js +81 -3041
  90. package/dist/index.js.map +1 -1
  91. package/dist/utils/Ease.d.ts +17 -0
  92. package/dist/utils/Ease.d.ts.map +1 -0
  93. package/dist/utils/GlobalAssetLoader.d.ts +141 -0
  94. package/dist/utils/GlobalAssetLoader.d.ts.map +1 -0
  95. package/dist/utils/RadialGradient.d.ts +58 -0
  96. package/dist/utils/RadialGradient.d.ts.map +1 -0
  97. package/dist/utils/functions.d.ts +2 -0
  98. package/dist/utils/functions.d.ts.map +1 -0
  99. package/package.json +13 -7
  100. package/src/components/Button.ts +396 -0
  101. package/src/components/Canvas.ts +61 -45
  102. package/src/components/Container.ts +21 -2
  103. package/src/components/DOMContainer.ts +123 -0
  104. package/src/components/DOMElement.ts +421 -0
  105. package/src/components/DisplayObject.ts +350 -197
  106. package/src/components/Graphic.ts +200 -34
  107. package/src/components/Joystick.ts +363 -0
  108. package/src/components/Mesh.ts +222 -0
  109. package/src/components/NineSliceSprite.ts +4 -1
  110. package/src/components/ParticleEmitter.ts +12 -8
  111. package/src/components/Sprite.ts +306 -30
  112. package/src/components/Text.ts +125 -18
  113. package/src/components/Video.ts +110 -0
  114. package/src/components/Viewport.ts +59 -43
  115. package/src/components/index.ts +8 -2
  116. package/src/components/types/DisplayObject.ts +34 -0
  117. package/src/components/types/Spritesheet.ts +0 -118
  118. package/src/directives/Controls.ts +254 -0
  119. package/src/directives/ControlsBase.ts +266 -0
  120. package/src/directives/Drag.ts +357 -52
  121. package/src/directives/Flash.ts +419 -0
  122. package/src/directives/GamepadControls.ts +537 -0
  123. package/src/directives/JoystickControls.ts +396 -0
  124. package/src/directives/KeyboardControls.ts +66 -424
  125. package/src/directives/Shake.ts +295 -0
  126. package/src/directives/Sound.ts +94 -31
  127. package/src/directives/ViewportFollow.ts +35 -7
  128. package/src/directives/index.ts +12 -6
  129. package/src/engine/animation.ts +175 -21
  130. package/src/engine/bootstrap.ts +23 -3
  131. package/src/engine/directive.ts +2 -2
  132. package/src/engine/reactive.ts +780 -177
  133. package/src/engine/signal.ts +35 -4
  134. package/src/engine/trigger.ts +34 -7
  135. package/src/engine/utils.ts +19 -3
  136. package/src/hooks/useProps.ts +1 -1
  137. package/src/index.ts +4 -2
  138. package/src/utils/GlobalAssetLoader.ts +257 -0
  139. package/src/utils/functions.ts +7 -0
  140. package/testing/index.ts +12 -0
  141. package/tsconfig.json +17 -0
  142. package/vite.config.ts +39 -0
@@ -1,10 +1,24 @@
1
- import { effect, signal, type WritableSignal } from "@signe/reactive";
1
+ import { effect, signal, type WritableSignal, type Signal } from "@signe/reactive";
2
2
  import { animate as animatePopmotion } from "popmotion";
3
+ import { Tick } from "../directives/Scheduler";
4
+ import { Subscription } from "rxjs";
3
5
 
4
- interface AnimateOptions<T> {
6
+ /**
7
+ * Gets the global tick signal from the engine context.
8
+ * This is automatically set by the Canvas component when it initializes.
9
+ *
10
+ * @returns The global tick signal if available, undefined otherwise
11
+ */
12
+ function getGlobalTickSignal(): Signal<Tick> | undefined {
13
+ return (globalThis as any).__CANVAS_ENGINE_TICK__;
14
+ }
15
+
16
+ export interface AnimateOptions<T> {
5
17
  duration?: number;
6
18
  ease?: (t: number) => number;
7
19
  onUpdate?: (value: T) => void;
20
+ onComplete?: () => void;
21
+ tick?: Signal<Tick>;
8
22
  }
9
23
 
10
24
  export interface AnimatedState<T> {
@@ -15,15 +29,65 @@ export interface AnimatedState<T> {
15
29
 
16
30
  export interface AnimatedSignal<T> extends Omit<WritableSignal<T>, 'set'> {
17
31
  (): T;
18
- set: (newValue: T) => void;
32
+ set: (newValue: T, options?: AnimateOptions<T>) => Promise<void>;
19
33
  animatedState: WritableSignal<AnimatedState<T>>;
20
34
  update: (updater: (value: T) => T) => void;
35
+ pause: () => void;
36
+ resume: () => void;
21
37
  }
22
38
 
23
39
  export function isAnimatedSignal(signal: WritableSignal<any>): boolean {
24
40
  return (signal as unknown as AnimatedSignal<any>).animatedState !== undefined;
25
41
  }
26
42
 
43
+ /**
44
+ * Creates a popmotion driver that uses the engine's tick system.
45
+ * The driver subscribes to the tick signal and calls the update function with deltaTime on each tick.
46
+ *
47
+ * @param tickSignal - The tick signal from the engine context
48
+ * @returns A driver function for popmotion
49
+ * @example
50
+ * ```ts
51
+ * const driver = createTickDriver(context.tick);
52
+ * animate({
53
+ * to: 100,
54
+ * driver: driver
55
+ * });
56
+ * ```
57
+ */
58
+ function createTickDriver(tickSignal: Signal<Tick>) {
59
+ return (update: (delta: number) => void) => {
60
+ let subscription: Subscription | undefined;
61
+ let lastTimestamp: number | null = null;
62
+
63
+ const start = () => {
64
+ if (subscription) return;
65
+
66
+ subscription = (tickSignal.observable as any).subscribe((result: any) => {
67
+ const tick = result?.value ?? result;
68
+ if (!tick) return;
69
+
70
+ if (lastTimestamp === null) {
71
+ lastTimestamp = tick.timestamp;
72
+ return;
73
+ }
74
+
75
+ const delta = tick.deltaTime;
76
+ lastTimestamp = tick.timestamp;
77
+ update(delta);
78
+ });
79
+ };
80
+
81
+ const stop = () => {
82
+ subscription?.unsubscribe();
83
+ subscription = undefined;
84
+ lastTimestamp = null;
85
+ };
86
+
87
+ return { start, stop };
88
+ };
89
+ }
90
+
27
91
  /**
28
92
  * Creates an animated signal with the given initial value and animation options.
29
93
  * It's a writable signal that can be animated using popmotion. Properties of the animated signal are:
@@ -31,16 +95,26 @@ export function isAnimatedSignal(signal: WritableSignal<any>): boolean {
31
95
  * - start: the start value of the animation.
32
96
  * - end: the end value of the animation.
33
97
  *
98
+ * If a tick signal is provided in options, the animation will use the engine's tick system.
99
+ * Otherwise, it will automatically use the global tick signal from the Canvas context if available.
100
+ * If no tick signal is available, it will use requestAnimationFrame by default.
101
+ *
34
102
  * @param initialValue The initial value of the signal.
35
- * @param options The animation options.
103
+ * @param options The animation options. Can include a `tick` signal to use a specific tick system.
36
104
  * @returns The animated signal.
37
105
  * @example
106
+ * ```ts
107
+ * // Automatically uses the Canvas tick system if available, otherwise requestAnimationFrame
38
108
  * const animatedValue = animatedSignal(0, { duration: 1000 });
39
109
  * animatedValue.set(10);
40
- * animatedValue.update((value) => value + 1);
41
- * console.log(animatedValue()); // 11
42
110
  *
43
- * animatedValue.animatedState() // { current: 10, start: 10, end: 11 }
111
+ * // Explicitly using a specific tick signal
112
+ * mount((element) => {
113
+ * const tickSignal = element.props.context.tick;
114
+ * const animatedValue = animatedSignal(0, { duration: 1000, tick: tickSignal });
115
+ * animatedValue.set(10);
116
+ * });
117
+ * ```
44
118
  */
45
119
  export function animatedSignal<T>(initialValue: T, options: AnimateOptions<T> = {}): AnimatedSignal<T> {
46
120
  const state: AnimatedState<T> = {
@@ -48,7 +122,9 @@ export function animatedSignal<T>(initialValue: T, options: AnimateOptions<T> =
48
122
  start: initialValue,
49
123
  end: initialValue,
50
124
  };
51
- let animation
125
+ const DEFAULT_DURATION = 20;
126
+ let animation: { stop: () => void } | null = null;
127
+ let isPaused = false;
52
128
 
53
129
  const publicSignal = signal(initialValue);
54
130
  const privateSignal = signal(state);
@@ -60,7 +136,8 @@ export function animatedSignal<T>(initialValue: T, options: AnimateOptions<T> =
60
136
 
61
137
  function animatedSignal(): AnimatedState<T>;
62
138
  function animatedSignal(newValue: T): void;
63
- function animatedSignal(newValue?: T): AnimatedState<T> | void {
139
+ function animatedSignal(newValue: T, animationConfig: AnimateOptions<T>): void;
140
+ function animatedSignal(newValue?: T, animationConfig: AnimateOptions<T> = {}): AnimatedState<T> | void {
64
141
  if (newValue === undefined) {
65
142
  return privateSignal();
66
143
  }
@@ -74,23 +151,43 @@ export function animatedSignal<T>(initialValue: T, options: AnimateOptions<T> =
74
151
 
75
152
  privateSignal.set(newState);
76
153
 
154
+ // Stop any running animation
77
155
  if (animation) {
78
156
  animation.stop();
157
+ animation = null;
79
158
  }
80
159
 
160
+ isPaused = false;
161
+ const mergedConfig = { ...options, ...animationConfig };
162
+ const duration = mergedConfig.duration ?? DEFAULT_DURATION;
163
+ const ease = mergedConfig.ease ?? ((t: number) => t);
164
+ const tickSignal = mergedConfig.tick || getGlobalTickSignal();
165
+
166
+ const startValue = prevState.current;
167
+ const endValue = newValue;
168
+
169
+ const onCompleteCb = animationConfig.onComplete ?? options.onComplete;
170
+
171
+
81
172
  animation = animatePopmotion({
82
- // TODO
83
- duration: 20,
84
- ...options,
85
- from: prevState.current,
86
- to: newValue,
87
- onUpdate: (value) => {
88
- privateSignal.update(s => ({ ...s, current: value as T }));
89
- if (options.onUpdate) {
90
- options.onUpdate(value as T);
91
- }
173
+ from: startValue as any,
174
+ to: endValue as any,
175
+ duration,
176
+ ease,
177
+
178
+ onUpdate: (value: any) => {
179
+ if (isPaused) return;
180
+ const nextValue = value as T;
181
+ privateSignal.update(s => ({ ...s, current: nextValue }));
182
+ mergedConfig.onUpdate?.(nextValue);
183
+ animationConfig.onUpdate?.(nextValue);
92
184
  },
185
+ onComplete: () => {
186
+ privateSignal.update(s => ({ ...s, current: endValue }));
187
+ onCompleteCb?.();
188
+ }
93
189
  });
190
+
94
191
  }
95
192
 
96
193
  const fn = function() {
@@ -105,9 +202,66 @@ export function animatedSignal<T>(initialValue: T, options: AnimateOptions<T> =
105
202
  fn.update = (updater: (value: T) => any) => {
106
203
  animatedSignal(updater(privateSignal().current));
107
204
  }
108
- fn.set = (newValue: T) => {
109
- animatedSignal(newValue);
205
+ fn.set = async (newValue: T, animationConfig: AnimateOptions<T> = {}) => {
206
+ return new Promise<void>((resolve) => {
207
+ const userOnComplete = animationConfig.onComplete;
208
+ animatedSignal(newValue, {
209
+ ...animationConfig,
210
+ onComplete: () => {
211
+ if (userOnComplete) {
212
+ userOnComplete();
213
+ } else if (options.onComplete) {
214
+ options.onComplete();
215
+ }
216
+ resolve();
217
+ }
218
+ });
219
+ });
220
+ }
221
+ fn.pause = () => {
222
+ if (isPaused) return;
223
+ isPaused = true;
224
+ if (animation) {
225
+ animation.stop();
226
+ animation = null;
227
+ }
228
+ }
229
+ fn.resume = () => {
230
+ if (!isPaused) return;
231
+ isPaused = false;
232
+ const currentState = privateSignal();
233
+ if (currentState.current !== currentState.end) {
234
+ animatedSignal(currentState.end, options);
235
+ }
110
236
  }
111
237
 
112
238
  return fn as any
239
+ }
240
+
241
+ /**
242
+ * Executes a sequence of animations. If an array is provided as an element in the sequence,
243
+ * those animations will be executed in parallel.
244
+ *
245
+ * @param sequence Array of animation functions or arrays of animation functions for parallel execution
246
+ * @returns Promise that resolves when all animations are complete
247
+ * @example
248
+ * ```ts
249
+ * await animatedSequence([
250
+ * () => value1.set(10),
251
+ * [
252
+ * () => value2.set(20),
253
+ * () => value3.set(30)
254
+ * ],
255
+ * () => value1.set(0)
256
+ * ])
257
+ * ```
258
+ */
259
+ export async function animatedSequence(sequence: ((() => Promise<void>) | (() => Promise<void>)[])[]) {
260
+ for (const item of sequence) {
261
+ if (Array.isArray(item)) {
262
+ await Promise.all(item.map(fn => fn()));
263
+ } else {
264
+ await item();
265
+ }
266
+ }
113
267
  }
@@ -1,4 +1,7 @@
1
+ import '@pixi/layout';
2
+ import { Application, ApplicationOptions } from "pixi.js";
1
3
  import { ComponentFunction, h } from "./signal";
4
+ import { useProps } from '../hooks/useProps';
2
5
 
3
6
  /**
4
7
  * Bootstraps a canvas element and renders it to the DOM.
@@ -8,12 +11,29 @@ import { ComponentFunction, h } from "./signal";
8
11
  * @returns A Promise that resolves to the rendered canvas element.
9
12
  * @throws {Error} If the provided element is not a Canvas component.
10
13
  */
11
- export const bootstrapCanvas = async (rootElement: HTMLElement | null, canvas: ComponentFunction<any>) => {
14
+ export const bootstrapCanvas = async (rootElement: HTMLElement | null, canvas: ComponentFunction<any>, options?: ApplicationOptions) => {
15
+
16
+ const app = new Application();
17
+ await app.init({
18
+ resizeTo: rootElement,
19
+ autoStart: false,
20
+ antialias: true,
21
+ ...(options ?? {})
22
+ });
12
23
  const canvasElement = await h(canvas);
13
24
  if (canvasElement.tag != 'Canvas') {
14
25
  throw new Error('Canvas is required');
15
26
  }
16
- (canvasElement as any).render(rootElement);
27
+ (canvasElement as any).render(rootElement, app);
17
28
 
18
- return canvasElement;
29
+ const { backgroundColor } = useProps(canvasElement.props, {
30
+ backgroundColor: 'black'
31
+ });
32
+
33
+ app.renderer.background.color = backgroundColor()
34
+
35
+ return {
36
+ canvasElement,
37
+ app
38
+ };
19
39
  };
@@ -3,10 +3,10 @@ import { Element } from "./reactive"
3
3
  export const directives: { [key: string]: any } = {}
4
4
 
5
5
  export abstract class Directive {
6
- abstract onDestroy();
6
+ abstract onDestroy(element: Element<any>);
7
7
  abstract onInit(element: Element<any>);
8
8
  abstract onMount(element: Element<any>);
9
- abstract onUpdate(props: any);
9
+ abstract onUpdate(props: any, element: Element<any>);
10
10
  }
11
11
 
12
12
  export function registerDirective(name: string, directive: any) {