laymur-animations 0.25.0 → 0.27.0

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 (37) hide show
  1. package/README.md +23 -0
  2. package/dist/UIAnimatedElement.d.ts +10 -3
  3. package/dist/UIAnimatedElement.js +1 -0
  4. package/dist/UIAppearAnimator.d.ts +7 -3
  5. package/dist/UIAppearAnimator.js +71 -0
  6. package/dist/UIClickAnimator.d.ts +52 -4
  7. package/dist/UIClickAnimator.js +210 -0
  8. package/dist/UIDisappearAnimator.d.ts +11 -3
  9. package/dist/UIDisappearAnimator.js +53 -0
  10. package/dist/UIFlashAnimator.d.ts +39 -0
  11. package/dist/UIFlashAnimator.js +43 -0
  12. package/dist/UIFloatCallAnimator.d.ts +45 -0
  13. package/dist/UIFloatCallAnimator.js +67 -0
  14. package/dist/UIFlyToAnimator.d.ts +57 -0
  15. package/dist/UIFlyToAnimator.js +60 -0
  16. package/dist/UIJumpCallAnimator.d.ts +10 -6
  17. package/dist/UIJumpCallAnimator.js +97 -0
  18. package/dist/UIPulseCallAnimator.d.ts +11 -6
  19. package/dist/UIPulseCallAnimator.js +67 -0
  20. package/dist/UIShakeAnimator.d.ts +31 -0
  21. package/dist/UIShakeAnimator.js +49 -0
  22. package/dist/UIShineCallAnimator.d.ts +49 -0
  23. package/dist/UIShineCallAnimator.js +74 -0
  24. package/dist/UISpinCallAnimator.d.ts +8 -5
  25. package/dist/UISpinCallAnimator.js +74 -0
  26. package/dist/UISwipeCallAnimator.d.ts +9 -6
  27. package/dist/UISwipeCallAnimator.js +64 -0
  28. package/dist/UITapCallAnimator.d.ts +64 -0
  29. package/dist/UITapCallAnimator.js +83 -0
  30. package/dist/index.d.ts +27 -7
  31. package/dist/index.js +13 -509
  32. package/dist/miscellaneous.d.ts +71 -0
  33. package/dist/miscellaneous.js +163 -0
  34. package/package.json +32 -42
  35. package/dist/index.js.map +0 -1
  36. package/dist/index.min.js +0 -2
  37. package/dist/index.min.js.map +0 -1
package/README.md CHANGED
@@ -10,6 +10,29 @@ A collection of common UI animations for Laymur UI library. Includes popular ani
10
10
  npm install laymur-animations gsap
11
11
  ```
12
12
 
13
+ ## Animations
14
+
15
+ One-shot, returning a promise: UIAppearAnimator, UIDisappearAnimator, UIClickAnimator, UIShakeAnimator, UIFlyToAnimator, UIFlashAnimator
16
+
17
+ Calls, running until stopped: UIPulseCallAnimator, UIJumpCallAnimator, UISpinCallAnimator, UISwipeCallAnimator, UITapCallAnimator, UIFloatCallAnimator, UIShineCallAnimator
18
+
19
+ ## Button press
20
+
21
+ `UIClickAnimator.bind` presses a button in while a finger holds it and releases it on the click or
22
+ cancel that ends the press:
23
+
24
+ ```typescript
25
+ import { UIClickAnimator } from "laymur-animations";
26
+
27
+ const unbind = UIClickAnimator.bind(button);
28
+ button.signalPointerClicked.on(onBuy);
29
+
30
+ // Once the button is no longer needed
31
+ unbind();
32
+ ```
33
+
34
+ `UIClickAnimator.press` and `UIClickAnimator.release` do the same when driven by hand.
35
+
13
36
  ## Documentation
14
37
 
15
38
  See [Usage Example](https://jango-git.github.io/laymur/07_animations.html) for a concrete implementation demonstration.
@@ -1,10 +1,17 @@
1
- import type { UIColor, UIMicro } from "laymur";
1
+ import type { UIColor, UIMicro, UIMode } from "laymur";
2
2
  /**
3
- * Interface for UI elements that can be animated.
3
+ * Interface for UI elements whose micro transform can be animated, such as UISpine.
4
4
  */
5
- export interface UIAnimatedElement {
5
+ export interface UIMicroAnimatedElement {
6
6
  /** Micro transformation object */
7
7
  micro: UIMicro;
8
+ }
9
+ /**
10
+ * Interface for UI elements that can be animated.
11
+ */
12
+ export interface UIAnimatedElement extends UIMicroAnimatedElement {
8
13
  /** Element alpha value */
9
14
  color: UIColor;
15
+ /** Visibility and interactivity, hidden and restored by the disappear and appear animators */
16
+ mode?: UIMode;
10
17
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,4 +1,4 @@
1
- import type { UIAnimatedElement } from "./UIAnimatedElement";
1
+ import type { UIAnimatedElement } from "./UIAnimatedElement.js";
2
2
  /**
3
3
  * Configuration options for appearance animations.
4
4
  */
@@ -21,6 +21,8 @@ export interface UIAppearAnimatorOptions {
21
21
  alphaTo: number;
22
22
  /** Animation delay in seconds */
23
23
  delay: number;
24
+ /** Delay between the starts of consecutive elements in seconds */
25
+ stagger: number;
24
26
  /** Animation duration in seconds */
25
27
  duration: number;
26
28
  /** GSAP easing function */
@@ -31,10 +33,12 @@ export interface UIAppearAnimatorOptions {
31
33
  */
32
34
  export declare class UIAppearAnimator {
33
35
  /**
34
- * Animates elements appearing with position, scale, and alpha transitions.
36
+ * Animates elements appearing with position, scale, and alpha transitions. Replaces an
37
+ * appearance or disappearance running on the same element, and gives an element hidden by
38
+ * the disappear animator its mode back.
35
39
  * @param target - Single element or array of elements to animate
36
40
  * @param options - Animation configuration options
37
- * @returns Promise that resolves when animation completes
41
+ * @returns Promise that resolves when animation completes or is interrupted
38
42
  */
39
43
  static appear(target: UIAnimatedElement | UIAnimatedElement[], options?: Partial<UIAppearAnimatorOptions>): Promise<void>;
40
44
  }
@@ -0,0 +1,71 @@
1
+ import gsap from "gsap";
2
+ import { resolveTargets, restoreMode, settle, stopVisibility, trackVisibility, } from "./miscellaneous.js";
3
+ const DEFAULT_X_FROM = 0;
4
+ const DEFAULT_X_TO = 0;
5
+ const DEFAULT_Y_FROM = 0;
6
+ const DEFAULT_Y_TO = 0;
7
+ const DEFAULT_SCALE_FROM = 0.5;
8
+ const DEFAULT_SCALE_TO = 1;
9
+ const DEFAULT_ALPHA_FROM = 0;
10
+ const DEFAULT_ALPHA_TO = 1;
11
+ const DEFAULT_DELAY = 0;
12
+ const DEFAULT_STAGGER = 0;
13
+ const DEFAULT_DURATION = 0.25;
14
+ const DEFAULT_EASE = "back.out(1.7)";
15
+ /**
16
+ * Animator for element appearance effects.
17
+ */
18
+ export class UIAppearAnimator {
19
+ /**
20
+ * Animates elements appearing with position, scale, and alpha transitions. Replaces an
21
+ * appearance or disappearance running on the same element, and gives an element hidden by
22
+ * the disappear animator its mode back.
23
+ * @param target - Single element or array of elements to animate
24
+ * @param options - Animation configuration options
25
+ * @returns Promise that resolves when animation completes or is interrupted
26
+ */
27
+ static appear(target, options = {}) {
28
+ const { xFrom = DEFAULT_X_FROM, xTo = DEFAULT_X_TO, yFrom = DEFAULT_Y_FROM, yTo = DEFAULT_Y_TO, scaleFrom = DEFAULT_SCALE_FROM, scaleTo = DEFAULT_SCALE_TO, alphaFrom = DEFAULT_ALPHA_FROM, alphaTo = DEFAULT_ALPHA_TO, delay = DEFAULT_DELAY, stagger = DEFAULT_STAGGER, duration = DEFAULT_DURATION, ease = DEFAULT_EASE, } = options;
29
+ const elements = resolveTargets(target);
30
+ const colorTarget = {
31
+ duration,
32
+ ease: "power1.inOut",
33
+ };
34
+ const microTarget = { duration, ease };
35
+ if (xFrom !== xTo) {
36
+ microTarget.x = xTo;
37
+ }
38
+ if (yFrom !== yTo) {
39
+ microTarget.y = yTo;
40
+ }
41
+ if (scaleFrom !== scaleTo) {
42
+ microTarget.scaleX = scaleTo;
43
+ microTarget.scaleY = scaleTo;
44
+ }
45
+ if (alphaFrom !== alphaTo) {
46
+ colorTarget.a = alphaTo;
47
+ }
48
+ const animations = elements.map((element, index) => {
49
+ stopVisibility(element);
50
+ restoreMode(element);
51
+ if (xFrom !== xTo) {
52
+ element.micro.x = xFrom;
53
+ }
54
+ if (yFrom !== yTo) {
55
+ element.micro.y = yFrom;
56
+ }
57
+ if (scaleFrom !== scaleTo) {
58
+ element.micro.scaleX = scaleFrom;
59
+ element.micro.scaleY = scaleFrom;
60
+ }
61
+ if (alphaFrom !== alphaTo) {
62
+ element.color.a = alphaFrom;
63
+ }
64
+ const timeline = gsap.timeline({ delay: delay + index * stagger });
65
+ timeline.to(element.micro, microTarget, 0).to(element.color, colorTarget, 0);
66
+ trackVisibility(element, timeline);
67
+ return settle(timeline);
68
+ });
69
+ return Promise.all(animations).then(() => undefined);
70
+ }
71
+ }
@@ -1,4 +1,5 @@
1
- import type { UIAnimatedElement } from "./UIAnimatedElement";
1
+ import type { UIDummy } from "laymur";
2
+ import type { UIMicroAnimatedElement } from "./UIAnimatedElement.js";
2
3
  /**
3
4
  * Configuration options for click animations.
4
5
  */
@@ -21,16 +22,63 @@ export interface UIClickAnimatorOptions {
21
22
  easeIn: gsap.EaseString;
22
23
  /** GSAP easing function for click out */
23
24
  easeOut: gsap.EaseString;
25
+ /**
26
+ * Whether the hit area stays in place while the element is pressed, instead of following
27
+ * the shrinking element (see UIMicro.affectsInput); otherwise a release near the edge misses
28
+ */
29
+ keepHitArea: boolean;
24
30
  }
31
+ /**
32
+ * Configuration options for holding an element pressed.
33
+ */
34
+ export type UIClickAnimatorPressOptions = Pick<UIClickAnimatorOptions, "xOffset" | "yOffset" | "xScale" | "yScale" | "durationIn" | "easeIn" | "keepHitArea">;
35
+ /**
36
+ * Configuration options for releasing a pressed element.
37
+ */
38
+ export type UIClickAnimatorReleaseOptions = Pick<UIClickAnimatorOptions, "durationOut" | "easeOut">;
39
+ /**
40
+ * Configuration options for binding press feedback to the pointer.
41
+ */
42
+ export type UIClickAnimatorBindOptions = UIClickAnimatorPressOptions & UIClickAnimatorReleaseOptions;
43
+ /**
44
+ * Element whose own presses drive the press feedback, such as any laymur UIElement.
45
+ */
46
+ export type UIClickAnimatorBindElement = UIMicroAnimatedElement & Pick<UIDummy, "signalPointerPressed" | "signalPointerClicked" | "signalPointerCancelled">;
25
47
  /**
26
48
  * Animator for click feedback effects.
27
49
  */
28
50
  export declare class UIClickAnimator {
29
51
  /**
30
- * Animates elements with click feedback including position offset and scale effects.
52
+ * Animates elements with click feedback including position offset and scale effects,
53
+ * relative to the transform the element rests at. A click replaces the one running on the
54
+ * same element.
31
55
  * @param target - Single element or array of elements to animate
32
56
  * @param options - Animation configuration options
33
- * @returns Promise that resolves when animation completes
57
+ * @returns Promise that resolves when animation completes or is interrupted
58
+ */
59
+ static click(target: UIMicroAnimatedElement | UIMicroAnimatedElement[], options?: Partial<UIClickAnimatorOptions>): Promise<void>;
60
+ /**
61
+ * Presses elements in and holds them there until {@link release}, relative to the transform
62
+ * they rest at. Does nothing to an element already held pressed.
63
+ * @param target - Single element or array of elements to press
64
+ * @param options - Press configuration options
65
+ */
66
+ static press(target: UIMicroAnimatedElement | UIMicroAnimatedElement[], options?: Partial<UIClickAnimatorPressOptions>): void;
67
+ /**
68
+ * Releases elements held by {@link press} back to rest. A press released before it is fully
69
+ * in finishes it first, so a quick tap still shows the whole click.
70
+ * @param target - Single element or array of elements to release
71
+ * @param options - Release configuration options
72
+ * @returns Promise that resolves when the elements are back at rest or interrupted
73
+ */
74
+ static release(target: UIMicroAnimatedElement | UIMicroAnimatedElement[], options?: Partial<UIClickAnimatorReleaseOptions>): Promise<void>;
75
+ /**
76
+ * Drives the press feedback by the pointer: the element is pressed while a pointer holds it
77
+ * and released on the click or cancel that ends every press it catches. Presses passed
78
+ * through by INTERACTIVE_TRANSPARENT elements, which end with neither, are ignored.
79
+ * @param element - Element to bind
80
+ * @param options - Press and release configuration options
81
+ * @returns Function that unbinds the element, releasing it if held
34
82
  */
35
- static click(target: UIAnimatedElement | UIAnimatedElement[], options?: Partial<UIClickAnimatorOptions>): Promise<void>;
83
+ static bind(element: UIClickAnimatorBindElement, options?: Partial<UIClickAnimatorBindOptions>): () => void;
36
84
  }
@@ -0,0 +1,210 @@
1
+ import gsap from "gsap";
2
+ import { UIMode } from "laymur";
3
+ import { resolveTargets, UICallTracker } from "./miscellaneous.js";
4
+ const DEFAULT_X_OFFSET = 0;
5
+ const DEFAULT_Y_OFFSET = -25;
6
+ const DEFAULT_X_SCALE = 0.85;
7
+ const DEFAULT_Y_SCALE = 1;
8
+ const DEFAULT_DELAY = 0;
9
+ const DEFAULT_DURATION_IN = 0.125;
10
+ const DEFAULT_DURATION_OUT = 0.25;
11
+ const DEFAULT_EASE_IN = "power1.out";
12
+ const DEFAULT_EASE_OUT = "back.out";
13
+ const DEFAULT_KEEP_HIT_AREA = true;
14
+ /** A click replaces the one running on the same element, keeping its rest values */
15
+ const clickCalls = new UICallTracker(["x", "y", "scaleX", "scaleY"]);
16
+ /** Clicks in progress per micro and its affectsInput from before the first of them */
17
+ const hitAreaLocks = new WeakMap();
18
+ function lockHitArea(micro) {
19
+ const lock = hitAreaLocks.get(micro);
20
+ if (lock) {
21
+ lock.count++;
22
+ return;
23
+ }
24
+ hitAreaLocks.set(micro, { count: 1, affectsInput: micro.affectsInput });
25
+ micro.affectsInput = false;
26
+ }
27
+ function unlockHitArea(micro) {
28
+ const lock = hitAreaLocks.get(micro);
29
+ if (!lock) {
30
+ return;
31
+ }
32
+ lock.count--;
33
+ if (lock.count === 0) {
34
+ hitAreaLocks.delete(micro);
35
+ micro.affectsInput = lock.affectsInput;
36
+ }
37
+ }
38
+ const presses = new WeakMap();
39
+ /**
40
+ * Animator for click feedback effects.
41
+ */
42
+ export class UIClickAnimator {
43
+ /**
44
+ * Animates elements with click feedback including position offset and scale effects,
45
+ * relative to the transform the element rests at. A click replaces the one running on the
46
+ * same element.
47
+ * @param target - Single element or array of elements to animate
48
+ * @param options - Animation configuration options
49
+ * @returns Promise that resolves when animation completes or is interrupted
50
+ */
51
+ static click(target, options = {}) {
52
+ const { xOffset = DEFAULT_X_OFFSET, yOffset = DEFAULT_Y_OFFSET, xScale = DEFAULT_X_SCALE, yScale = DEFAULT_Y_SCALE, delay = DEFAULT_DELAY, durationIn = DEFAULT_DURATION_IN, durationOut = DEFAULT_DURATION_OUT, easeIn = DEFAULT_EASE_IN, easeOut = DEFAULT_EASE_OUT, keepHitArea = DEFAULT_KEEP_HIT_AREA, } = options;
53
+ const elements = resolveTargets(target);
54
+ const clicks = elements.map((element) => new Promise((resolve) => {
55
+ const micro = element.micro;
56
+ if (keepHitArea) {
57
+ lockHitArea(micro);
58
+ }
59
+ clickCalls.start(micro, (rest) => gsap
60
+ .timeline({ delay })
61
+ .to(micro, {
62
+ x: rest.x + xOffset,
63
+ y: rest.y + yOffset,
64
+ scaleX: rest.scaleX * xScale,
65
+ scaleY: rest.scaleY * yScale,
66
+ duration: durationIn,
67
+ ease: easeIn,
68
+ })
69
+ .to(micro, {
70
+ x: rest.x,
71
+ y: rest.y,
72
+ scaleX: rest.scaleX,
73
+ scaleY: rest.scaleY,
74
+ duration: durationOut,
75
+ ease: easeOut,
76
+ }), () => {
77
+ if (keepHitArea) {
78
+ unlockHitArea(micro);
79
+ }
80
+ resolve();
81
+ });
82
+ }));
83
+ return Promise.all(clicks).then(() => undefined);
84
+ }
85
+ /**
86
+ * Presses elements in and holds them there until {@link release}, relative to the transform
87
+ * they rest at. Does nothing to an element already held pressed.
88
+ * @param target - Single element or array of elements to press
89
+ * @param options - Press configuration options
90
+ */
91
+ static press(target, options = {}) {
92
+ const { xOffset = DEFAULT_X_OFFSET, yOffset = DEFAULT_Y_OFFSET, xScale = DEFAULT_X_SCALE, yScale = DEFAULT_Y_SCALE, durationIn = DEFAULT_DURATION_IN, easeIn = DEFAULT_EASE_IN, keepHitArea = DEFAULT_KEEP_HIT_AREA, } = options;
93
+ for (const element of resolveTargets(target)) {
94
+ const micro = element.micro;
95
+ if (presses.has(micro)) {
96
+ continue;
97
+ }
98
+ const state = {
99
+ pressed: { x: 0, y: 0, scaleX: 1, scaleY: 1 },
100
+ timeline: undefined,
101
+ durationIn,
102
+ easeIn,
103
+ keepHitArea,
104
+ };
105
+ presses.set(micro, state);
106
+ if (keepHitArea) {
107
+ lockHitArea(micro);
108
+ }
109
+ clickCalls.start(micro, (rest) => {
110
+ state.pressed = {
111
+ x: rest.x + xOffset,
112
+ y: rest.y + yOffset,
113
+ scaleX: rest.scaleX * xScale,
114
+ scaleY: rest.scaleY * yScale,
115
+ };
116
+ // The endless empty tween keeps the call running while the element is held
117
+ state.timeline = gsap
118
+ .timeline()
119
+ .to(micro, { ...state.pressed, duration: durationIn, ease: easeIn })
120
+ .to({}, { duration: 1, repeat: -1 });
121
+ return state.timeline;
122
+ }, () => {
123
+ // Replaced by a click or killed from outside rather than released
124
+ if (presses.get(micro) === state) {
125
+ presses.delete(micro);
126
+ if (keepHitArea) {
127
+ unlockHitArea(micro);
128
+ }
129
+ }
130
+ });
131
+ }
132
+ }
133
+ /**
134
+ * Releases elements held by {@link press} back to rest. A press released before it is fully
135
+ * in finishes it first, so a quick tap still shows the whole click.
136
+ * @param target - Single element or array of elements to release
137
+ * @param options - Release configuration options
138
+ * @returns Promise that resolves when the elements are back at rest or interrupted
139
+ */
140
+ static release(target, options = {}) {
141
+ const { durationOut = DEFAULT_DURATION_OUT, easeOut = DEFAULT_EASE_OUT } = options;
142
+ const releases = resolveTargets(target).map((element) => new Promise((resolve) => {
143
+ const micro = element.micro;
144
+ const state = presses.get(micro);
145
+ if (state === undefined) {
146
+ resolve();
147
+ return;
148
+ }
149
+ presses.delete(micro);
150
+ const remaining = Math.max(state.durationIn - (state.timeline?.time() ?? 0), 0);
151
+ clickCalls.start(micro, (rest) => {
152
+ const timeline = gsap.timeline();
153
+ if (remaining > 0) {
154
+ timeline.to(micro, { ...state.pressed, duration: remaining, ease: state.easeIn });
155
+ }
156
+ return timeline.to(micro, {
157
+ x: rest.x,
158
+ y: rest.y,
159
+ scaleX: rest.scaleX,
160
+ scaleY: rest.scaleY,
161
+ duration: durationOut,
162
+ ease: easeOut,
163
+ });
164
+ }, () => {
165
+ if (state.keepHitArea) {
166
+ unlockHitArea(micro);
167
+ }
168
+ resolve();
169
+ });
170
+ }));
171
+ return Promise.all(releases).then(() => undefined);
172
+ }
173
+ /**
174
+ * Drives the press feedback by the pointer: the element is pressed while a pointer holds it
175
+ * and released on the click or cancel that ends every press it catches. Presses passed
176
+ * through by INTERACTIVE_TRANSPARENT elements, which end with neither, are ignored.
177
+ * @param element - Element to bind
178
+ * @param options - Press and release configuration options
179
+ * @returns Function that unbinds the element, releasing it if held
180
+ */
181
+ static bind(element, options = {}) {
182
+ const pointers = new Set();
183
+ const onPressed = (event) => {
184
+ if (event.element.mode !== UIMode.INTERACTIVE) {
185
+ return;
186
+ }
187
+ pointers.add(event.identifier);
188
+ if (pointers.size === 1) {
189
+ UIClickAnimator.press(element, options);
190
+ }
191
+ };
192
+ const onEnded = (event) => {
193
+ if (pointers.delete(event.identifier) && pointers.size === 0) {
194
+ void UIClickAnimator.release(element, options);
195
+ }
196
+ };
197
+ element.signalPointerPressed.on(onPressed);
198
+ element.signalPointerClicked.on(onEnded);
199
+ element.signalPointerCancelled.on(onEnded);
200
+ return () => {
201
+ element.signalPointerPressed.off(onPressed);
202
+ element.signalPointerClicked.off(onEnded);
203
+ element.signalPointerCancelled.off(onEnded);
204
+ if (pointers.size > 0) {
205
+ pointers.clear();
206
+ void UIClickAnimator.release(element, options);
207
+ }
208
+ };
209
+ }
210
+ }
@@ -1,4 +1,4 @@
1
- import type { UIAnimatedElement } from "./UIAnimatedElement";
1
+ import type { UIAnimatedElement } from "./UIAnimatedElement.js";
2
2
  /**
3
3
  * Configuration options for disappearance animations.
4
4
  */
@@ -13,20 +13,28 @@ export interface UIDisappearAnimatorOptions {
13
13
  alphaTo: number;
14
14
  /** Animation delay in seconds */
15
15
  delay: number;
16
+ /** Delay between the starts of consecutive elements in seconds */
17
+ stagger: number;
16
18
  /** Animation duration in seconds */
17
19
  duration: number;
18
20
  /** GSAP easing function */
19
21
  ease: gsap.EaseString;
22
+ /**
23
+ * Whether to set the mode of the element to HIDDEN once it has disappeared, so it stops
24
+ * catching input; the appear animator gives the mode back
25
+ */
26
+ hide: boolean;
20
27
  }
21
28
  /**
22
29
  * Animator for element disappearance effects.
23
30
  */
24
31
  export declare class UIDisappearAnimator {
25
32
  /**
26
- * Animates elements disappearing with position, scale, and alpha transitions.
33
+ * Animates elements disappearing with position, scale, and alpha transitions. Replaces an
34
+ * appearance or disappearance running on the same element.
27
35
  * @param target - Single element or array of elements to animate
28
36
  * @param options - Animation configuration options
29
- * @returns Promise that resolves when animation completes
37
+ * @returns Promise that resolves when animation completes or is interrupted
30
38
  */
31
39
  static disappear(target: UIAnimatedElement | UIAnimatedElement[], options?: Partial<UIDisappearAnimatorOptions>): Promise<void>;
32
40
  }
@@ -0,0 +1,53 @@
1
+ import gsap from "gsap";
2
+ import { hideElement, isVisibilityTracked, resolveTargets, settle, stopVisibility, trackVisibility, } from "./miscellaneous.js";
3
+ const DEFAULT_DELAY = 0;
4
+ const DEFAULT_STAGGER = 0;
5
+ const DEFAULT_DURATION = 0.25;
6
+ const DEFAULT_EASE = "back.in(1.7)";
7
+ const DEFAULT_ALPHA_TO = 0;
8
+ const DEFAULT_HIDE = false;
9
+ /**
10
+ * Animator for element disappearance effects.
11
+ */
12
+ export class UIDisappearAnimator {
13
+ /**
14
+ * Animates elements disappearing with position, scale, and alpha transitions. Replaces an
15
+ * appearance or disappearance running on the same element.
16
+ * @param target - Single element or array of elements to animate
17
+ * @param options - Animation configuration options
18
+ * @returns Promise that resolves when animation completes or is interrupted
19
+ */
20
+ static disappear(target, options = {}) {
21
+ const { delay = DEFAULT_DELAY, stagger = DEFAULT_STAGGER, duration = DEFAULT_DURATION, ease = DEFAULT_EASE, alphaTo = DEFAULT_ALPHA_TO, hide = DEFAULT_HIDE, } = options;
22
+ const elements = resolveTargets(target);
23
+ const microTarget = { duration, ease };
24
+ const colorTarget = {
25
+ duration,
26
+ ease: "power1.inOut",
27
+ a: alphaTo,
28
+ };
29
+ if (options.xTo !== undefined) {
30
+ microTarget.x = options.xTo;
31
+ }
32
+ if (options.yTo !== undefined) {
33
+ microTarget.y = options.yTo;
34
+ }
35
+ if (options.scaleTo !== undefined) {
36
+ microTarget.scaleX = options.scaleTo;
37
+ microTarget.scaleY = options.scaleTo;
38
+ }
39
+ const animations = elements.map((element, index) => {
40
+ stopVisibility(element);
41
+ const timeline = gsap.timeline({ delay: delay + index * stagger });
42
+ timeline.to(element.micro, microTarget, 0).to(element.color, colorTarget, 0);
43
+ trackVisibility(element, timeline);
44
+ return settle(timeline).then((completed) => {
45
+ // An appearance may have started since the disappearance completed
46
+ if (completed && hide && isVisibilityTracked(element, timeline)) {
47
+ hideElement(element);
48
+ }
49
+ });
50
+ });
51
+ return Promise.all(animations).then(() => undefined);
52
+ }
53
+ }
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Element whose lightness can be animated, such as any laymur UIElement.
3
+ */
4
+ export interface UIFlashElement {
5
+ /** Lightness offset (0 = unchanged) */
6
+ lightness: number;
7
+ }
8
+ /**
9
+ * Configuration options for flash animations.
10
+ */
11
+ export interface UIFlashAnimatorOptions {
12
+ /** Lightness offset at the peak of the flash, added to the rest lightness */
13
+ lightness: number;
14
+ /** Number of flashes in a row */
15
+ flashes: number;
16
+ /** Animation delay in seconds */
17
+ delay: number;
18
+ /** Duration of the light up in seconds */
19
+ durationIn: number;
20
+ /** Duration of the fade back in seconds */
21
+ durationOut: number;
22
+ /** GSAP easing function for the light up */
23
+ easeIn: gsap.EaseString;
24
+ /** GSAP easing function for the fade back */
25
+ easeOut: gsap.EaseString;
26
+ }
27
+ /**
28
+ * Animator for flash highlights, such as a hit or reward feedback.
29
+ */
30
+ export declare class UIFlashAnimator {
31
+ /**
32
+ * Lights elements up and fades them back to the lightness they rest at. A flash replaces
33
+ * the one running on the same element.
34
+ * @param target - Single element or array of elements to animate
35
+ * @param options - Animation configuration options
36
+ * @returns Promise that resolves when animation completes or is interrupted
37
+ */
38
+ static flash(target: UIFlashElement | UIFlashElement[], options?: Partial<UIFlashAnimatorOptions>): Promise<void>;
39
+ }
@@ -0,0 +1,43 @@
1
+ import gsap from "gsap";
2
+ import { resolveTargets, settle } from "./miscellaneous.js";
3
+ const flashes = new WeakMap();
4
+ const DEFAULT_LIGHTNESS = 0.35;
5
+ const DEFAULT_FLASHES = 1;
6
+ const DEFAULT_DELAY = 0;
7
+ const DEFAULT_DURATION_IN = 0.08;
8
+ const DEFAULT_DURATION_OUT = 0.3;
9
+ const DEFAULT_EASE_IN = "power1.out";
10
+ const DEFAULT_EASE_OUT = "power1.in";
11
+ /**
12
+ * Animator for flash highlights, such as a hit or reward feedback.
13
+ */
14
+ export class UIFlashAnimator {
15
+ /**
16
+ * Lights elements up and fades them back to the lightness they rest at. A flash replaces
17
+ * the one running on the same element.
18
+ * @param target - Single element or array of elements to animate
19
+ * @param options - Animation configuration options
20
+ * @returns Promise that resolves when animation completes or is interrupted
21
+ */
22
+ static flash(target, options = {}) {
23
+ const { lightness = DEFAULT_LIGHTNESS, flashes: count = DEFAULT_FLASHES, delay = DEFAULT_DELAY, durationIn = DEFAULT_DURATION_IN, durationOut = DEFAULT_DURATION_OUT, easeIn = DEFAULT_EASE_IN, easeOut = DEFAULT_EASE_OUT, } = options;
24
+ const animations = resolveTargets(target).map((element) => {
25
+ const previous = flashes.get(element);
26
+ previous?.timeline.kill();
27
+ const rest = previous?.rest ?? element.lightness;
28
+ const timeline = gsap.timeline({ delay });
29
+ for (let i = 0; i < Math.max(Math.round(count), 1); i++) {
30
+ timeline
31
+ .to(element, { lightness: rest + lightness, duration: durationIn, ease: easeIn })
32
+ .to(element, { lightness: rest, duration: durationOut, ease: easeOut });
33
+ }
34
+ flashes.set(element, { rest, timeline });
35
+ return settle(timeline).then(() => {
36
+ if (flashes.get(element)?.timeline === timeline) {
37
+ flashes.delete(element);
38
+ }
39
+ });
40
+ });
41
+ return Promise.all(animations).then(() => undefined);
42
+ }
43
+ }
@@ -0,0 +1,45 @@
1
+ import type { UIMicroAnimatedElement } from "./UIAnimatedElement.js";
2
+ /**
3
+ * Configuration options for float call animations.
4
+ */
5
+ export interface UIFloatCallAnimatorCallOptions {
6
+ /** Vertical swing to each side of the rest position in world units */
7
+ amplitude: number;
8
+ /** Duration of a full up and down cycle in seconds */
9
+ period: number;
10
+ /** Rotation to each side of the rest rotation in radians; 0 for none */
11
+ rotation: number;
12
+ /** Duration of a full rotation cycle in seconds; differing from period keeps it organic */
13
+ rotationPeriod: number;
14
+ /** GSAP easing function of each half cycle */
15
+ ease: gsap.EaseString;
16
+ /** Called once the call ends: stopped or replaced by another float */
17
+ onComplete: () => void;
18
+ }
19
+ /**
20
+ * Configuration options for stopping float animations.
21
+ */
22
+ export interface UIFloatCallAnimatorStopOptions {
23
+ /** Duration to return to rest position in seconds */
24
+ duration: number;
25
+ /** GSAP easing function for stop transition */
26
+ ease: gsap.EaseString;
27
+ }
28
+ /**
29
+ * Animator for idle floating effects, such as a hovering item or logo.
30
+ */
31
+ export declare class UIFloatCallAnimator {
32
+ /**
33
+ * Starts an endless floating animation: a vertical sway with an optional rocking rotation.
34
+ * Replaces a float running on the same element.
35
+ * @param target - Single element or array of elements to animate
36
+ * @param options - Animation configuration options
37
+ */
38
+ static float(target: UIMicroAnimatedElement | UIMicroAnimatedElement[], options?: Partial<UIFloatCallAnimatorCallOptions>): void;
39
+ /**
40
+ * Stops the floating animation and returns elements to rest.
41
+ * @param target - Single element or array of elements to stop animating
42
+ * @param options - Stop animation configuration options
43
+ */
44
+ static stopFloat(target: UIMicroAnimatedElement | UIMicroAnimatedElement[], options?: Partial<UIFloatCallAnimatorStopOptions>): void;
45
+ }