canvasengine 2.0.0-beta.31 → 2.0.0-beta.33

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.js CHANGED
@@ -1,5 +1,5 @@
1
- import { h as a } from "./index-DNDNQN-q.js";
2
- import { A as r, B as n, w as o, C as c, m as l, l as p, D as S, v as m, a5 as u, a4 as g, a2 as b, n as d, G as C, c as j, M as T, N as E, O, P as f, a3 as v, R as w, o as h, p as D, S as P, q as k, r as x, T as y, b as A, V as H, t as M, a1 as V, a0 as B, _ as G, d as N, J as R, H as q, K as U, e as z, W as I, $ as J, f as K, g as L, x as Q, j as W, i as X, y as Y, k as Z, X as _, I as $, Q as F, L as aa, Z as sa, z as ea, s as ta, U as ia, Y as ra, a as na, u as oa } from "./index-DNDNQN-q.js";
1
+ import { h as a } from "./index-BLbc2zG5.js";
2
+ import { A as r, B as n, w as o, C as c, m as l, l as p, D as S, v as m, a5 as u, a4 as g, a2 as b, n as d, G as C, c as j, M as T, N as E, O, P as f, a3 as v, R as w, o as h, p as D, S as P, q as k, r as x, T as y, b as A, V as H, t as M, a1 as V, a0 as B, _ as G, d as N, J as R, H as q, K as U, e as z, W as I, $ as J, f as K, g as L, x as Q, j as W, i as X, y as Y, k as Z, X as _, I as $, Q as F, L as aa, Z as sa, z as ea, s as ta, U as ia, Y as ra, a as na, u as oa } from "./index-BLbc2zG5.js";
3
3
  const e = a.Howler;
4
4
  export {
5
5
  r as ArraySubject,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "canvasengine",
3
- "version": "2.0.0-beta.31",
3
+ "version": "2.0.0-beta.33",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -182,6 +182,7 @@ export class CanvasSprite extends DisplayObject(PixiSprite) {
182
182
  const { props, propObservables } = params;
183
183
  const tick: Signal = props.context.tick;
184
184
  const sheet = props.sheet ?? {};
185
+ const definition = props.sheet?.definition ?? {};
185
186
  this.app = props.context.app();
186
187
  if (sheet?.onFinish) {
187
188
  this.onFinish = sheet.onFinish;
@@ -190,8 +191,8 @@ export class CanvasSprite extends DisplayObject(PixiSprite) {
190
191
  if (this.destroyed) return
191
192
  this.update(value);
192
193
  });
193
- if (props.sheet?.definition) {
194
- this.spritesheet = props.sheet.definition;
194
+ if (definition) {
195
+ this.spritesheet = definition.value ?? definition;
195
196
  await this.createAnimations();
196
197
  }
197
198
  if (sheet.params) {
@@ -267,7 +268,14 @@ export class CanvasSprite extends DisplayObject(PixiSprite) {
267
268
  return texture
268
269
  }
269
270
 
270
- const sheet = props.sheet;
271
+ const sheet = props.sheet
272
+ const definition = props.sheet?.definition ?? {};
273
+
274
+ if (definition?.type === 'reset') {
275
+ this.spritesheet = definition.value ?? definition;
276
+ await this.resetAnimations();
277
+ }
278
+
271
279
  if (sheet?.params) this.sheetParams = sheet?.params;
272
280
 
273
281
  if (sheet?.playing && this.isMounted) {
@@ -287,9 +295,7 @@ export class CanvasSprite extends DisplayObject(PixiSprite) {
287
295
  if (isElement(props.texture)) {
288
296
  const textureInstance = props.texture.componentInstance;
289
297
  textureInstance.subjectInit
290
- .subscribe((value) => {
291
- console.log('a', value?.width)
292
- })
298
+ .subscribe()
293
299
  this.texture = this.renderer?.generateTexture(props.texture.componentInstance);
294
300
  } else {
295
301
  this.texture = props.texture;
@@ -337,7 +343,6 @@ export class CanvasSprite extends DisplayObject(PixiSprite) {
337
343
 
338
344
  stop() {
339
345
  this.currentAnimation = null;
340
- this.destroy();
341
346
  }
342
347
 
343
348
  play(name: string, params: any[] = []) {
@@ -353,7 +358,7 @@ export class CanvasSprite extends DisplayObject(PixiSprite) {
353
358
  );
354
359
  }
355
360
 
356
- const cloneParams = (params);
361
+ const cloneParams = structuredClone(params);
357
362
 
358
363
  this.removeChildren();
359
364
  animation.sprites = [];
@@ -393,6 +398,44 @@ export class CanvasSprite extends DisplayObject(PixiSprite) {
393
398
  });
394
399
  }
395
400
 
401
+ /**
402
+ * Resets the sprite by destroying and recreating all animations
403
+ * This method clears the current animation state, destroys existing textures,
404
+ * and recreates all animations from the spritesheet
405
+ *
406
+ * @example
407
+ * ```typescript
408
+ * // Reset all animations to their initial state
409
+ * sprite.resetAnimations();
410
+ *
411
+ * // Reset and then play a specific animation
412
+ * await sprite.resetAnimations();
413
+ * sprite.play('walk');
414
+ * ```
415
+ */
416
+ async resetAnimations(): Promise<void> {
417
+ // Stop current animation
418
+ this.stop();
419
+
420
+ // Clear all animations and textures
421
+ this.animations.clear();
422
+
423
+ // Reset animation state
424
+ this.currentAnimation = null;
425
+ this.currentAnimationContainer = null;
426
+ this.time = 0;
427
+ this.frameIndex = 0;
428
+
429
+ // Clear children
430
+ this.removeChildren();
431
+
432
+ // Recreate animations from spritesheet
433
+ if (this.spritesheet) {
434
+ await this.createAnimations();
435
+ this.play(this.sheetCurrentAnimation, [this.sheetParams]);
436
+ }
437
+ }
438
+
396
439
  update({ deltaRatio }) {
397
440
  if (
398
441
  !this.isPlaying() ||
@@ -4,6 +4,7 @@ import { DisplayObject, ComponentInstance } from "./DisplayObject";
4
4
  import { DisplayObjectProps } from "./types/DisplayObject";
5
5
  import { Signal } from "@signe/reactive";
6
6
  import { on, isTrigger } from "../engine/trigger";
7
+ import { Howl } from "howler";
7
8
 
8
9
  enum TextEffect {
9
10
  Typewriter = "typewriter",
@@ -20,6 +21,11 @@ export interface TextProps extends DisplayObjectProps {
20
21
  start?: () => void;
21
22
  onComplete?: () => void;
22
23
  skip?: () => void;
24
+ sound?: {
25
+ src: string;
26
+ volume?: number;
27
+ rate?: number;
28
+ };
23
29
  };
24
30
  context?: any; // Ensure context is available, ideally typed from a base prop or injected
25
31
  }
@@ -32,6 +38,9 @@ class CanvasText extends DisplayObject(PixiText) {
32
38
  private _wordWrapWidth: number = 0;
33
39
  private typewriterOptions: any = {};
34
40
  private skipSignal?: () => void;
41
+ private typewriterSound?: Howl;
42
+ private lastSoundTime: number = 0;
43
+ private soundDuration: number = 0; // Duration of the sound in milliseconds
35
44
 
36
45
  /**
37
46
  * Called when the component is mounted to the scene graph.
@@ -56,7 +65,13 @@ class CanvasText extends DisplayObject(PixiText) {
56
65
  this.skipTypewriter();
57
66
  });
58
67
  }
68
+ // Initialize typewriter sound if configured
69
+ if (this.typewriterOptions.sound) {
70
+ this.initializeTypewriterSound();
71
+ }
59
72
  }
73
+ // Update layout after initializing typewriter
74
+ this.updateLayout();
60
75
  }
61
76
  this.subscriptionTick = tick.observable.subscribe(() => {
62
77
  if (props.typewriter) {
@@ -70,6 +85,10 @@ class CanvasText extends DisplayObject(PixiText) {
70
85
  if (props.typewriter) {
71
86
  if (props.typewriter) {
72
87
  this.typewriterOptions = props.typewriter;
88
+ // Reinitialize sound if sound configuration changed
89
+ if (props.typewriter.sound) {
90
+ this.initializeTypewriterSound();
91
+ }
73
92
  }
74
93
  }
75
94
  if (props.text !== undefined) {
@@ -79,6 +98,8 @@ class CanvasText extends DisplayObject(PixiText) {
79
98
  this.text = "";
80
99
  this.currentIndex = 0;
81
100
  this.fullText = props.text;
101
+ // Update layout after resetting typewriter
102
+ this.updateLayout();
82
103
  }
83
104
  if (props.style) {
84
105
  for (const key in props.style) {
@@ -97,6 +118,59 @@ class CanvasText extends DisplayObject(PixiText) {
97
118
  if (props.fontFamily) {
98
119
  this.style.fontFamily = props.fontFamily;
99
120
  }
121
+
122
+ // Use the centralized layout update method
123
+ this.updateLayout();
124
+ }
125
+
126
+ get onCompleteCallback() {
127
+ return this.typewriterOptions.onComplete;
128
+ }
129
+
130
+ /**
131
+ * Initializes the typewriter sound effect using Howler.
132
+ * Creates a Howl instance with the configured sound settings.
133
+ * Calculates the sound duration to prevent overlapping sounds.
134
+ */
135
+ private initializeTypewriterSound() {
136
+ if (!this.typewriterOptions.sound?.src) return;
137
+
138
+ this.typewriterSound = new Howl({
139
+ src: [this.typewriterOptions.sound.src],
140
+ volume: this.typewriterOptions.sound.volume ?? 0.5,
141
+ rate: this.typewriterOptions.sound.rate ?? 1.0,
142
+ preload: true,
143
+ onload: () => {
144
+ // Calculate sound duration in milliseconds
145
+ if (this.typewriterSound) {
146
+ const duration = this.typewriterSound.duration();
147
+ const rate = this.typewriterOptions.sound?.rate ?? 1.0;
148
+ this.soundDuration = (duration / rate) * 1000;
149
+ }
150
+ }
151
+ });
152
+ }
153
+
154
+ /**
155
+ * Plays the typewriter sound with duration-based cooldown to prevent overlapping sounds.
156
+ * @param {number} currentTime - The current timestamp to check against sound duration.
157
+ */
158
+ private playTypewriterSound(currentTime: number) {
159
+ if (!this.typewriterSound || !this.typewriterOptions.sound) return;
160
+
161
+ // Check if enough time has passed since the last sound play
162
+ // Use the actual sound duration to prevent overlap
163
+ if (this.soundDuration > 0 && currentTime - this.lastSoundTime < this.soundDuration) return;
164
+
165
+ this.typewriterSound.play();
166
+ this.lastSoundTime = currentTime;
167
+ }
168
+
169
+ /**
170
+ * Updates the layout properties of the text component.
171
+ * This method ensures consistent width, height and word wrap behavior.
172
+ */
173
+ private updateLayout() {
100
174
  if (this._wordWrapWidth) {
101
175
  this.setWidth(this._wordWrapWidth);
102
176
  } else {
@@ -105,10 +179,6 @@ class CanvasText extends DisplayObject(PixiText) {
105
179
  this.setHeight(this.height);
106
180
  }
107
181
 
108
- get onCompleteCallback() {
109
- return this.typewriterOptions.onComplete;
110
- }
111
-
112
182
  private typewriterEffect() {
113
183
  if (this.currentIndex < this.fullText.length) {
114
184
  const nextIndex = Math.min(
@@ -118,6 +188,14 @@ class CanvasText extends DisplayObject(PixiText) {
118
188
  this.text = this.fullText.slice(0, nextIndex);
119
189
  this.currentIndex = nextIndex;
120
190
 
191
+ // Play typewriter sound if configured
192
+ if (this.typewriterOptions.sound) {
193
+ this.playTypewriterSound(Date.now());
194
+ }
195
+
196
+ // Update layout after text change to maintain proper word wrap and dimensions
197
+ this.updateLayout();
198
+
121
199
  // Check if typewriter effect is complete
122
200
  if (
123
201
  this.currentIndex === this.fullText.length &&
@@ -135,11 +213,14 @@ class CanvasText extends DisplayObject(PixiText) {
135
213
  }
136
214
  this.text = this.fullText;
137
215
  this.currentIndex = this.fullText.length;
216
+
217
+ // Update layout after setting full text to maintain proper word wrap and dimensions
218
+ this.updateLayout();
138
219
  }
139
220
 
140
221
  /**
141
222
  * Called when the component is about to be destroyed.
142
- * Unsubscribes from the tick observable.
223
+ * Unsubscribes from the tick observable and cleans up sound resources.
143
224
  * @param {Element<any>} parent - The parent element.
144
225
  * @param {() => void} [afterDestroy] - An optional callback function to be executed after the component's own destruction logic.
145
226
  */
@@ -148,6 +229,12 @@ class CanvasText extends DisplayObject(PixiText) {
148
229
  if (this.subscriptionTick) {
149
230
  this.subscriptionTick.unsubscribe();
150
231
  }
232
+ // Clean up typewriter sound
233
+ if (this.typewriterSound) {
234
+ this.typewriterSound.stop();
235
+ this.typewriterSound.unload();
236
+ this.typewriterSound = undefined;
237
+ }
151
238
  if (afterDestroy) {
152
239
  afterDestroy();
153
240
  }