canvasengine 2.0.0-beta.31 → 2.0.0-beta.32

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-19a35G23.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-19a35G23.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.32",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -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
  }