laymur-animations 0.25.0 → 0.26.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.
package/README.md CHANGED
@@ -10,6 +10,12 @@ 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
+
13
19
  ## Documentation
14
20
 
15
21
  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,4 @@
1
- import type { UIAnimatedElement } from "./UIAnimatedElement";
1
+ import type { UIMicroAnimatedElement } from "./UIAnimatedElement.js";
2
2
  /**
3
3
  * Configuration options for click animations.
4
4
  */
@@ -21,16 +21,23 @@ export interface UIClickAnimatorOptions {
21
21
  easeIn: gsap.EaseString;
22
22
  /** GSAP easing function for click out */
23
23
  easeOut: gsap.EaseString;
24
+ /**
25
+ * Whether the hit area stays in place while the element is pressed, instead of following
26
+ * the shrinking element (see UIMicro.affectsInput); otherwise a release near the edge misses
27
+ */
28
+ keepHitArea: boolean;
24
29
  }
25
30
  /**
26
31
  * Animator for click feedback effects.
27
32
  */
28
33
  export declare class UIClickAnimator {
29
34
  /**
30
- * Animates elements with click feedback including position offset and scale effects.
35
+ * Animates elements with click feedback including position offset and scale effects,
36
+ * relative to the transform the element rests at. A click replaces the one running on the
37
+ * same element.
31
38
  * @param target - Single element or array of elements to animate
32
39
  * @param options - Animation configuration options
33
- * @returns Promise that resolves when animation completes
40
+ * @returns Promise that resolves when animation completes or is interrupted
34
41
  */
35
- static click(target: UIAnimatedElement | UIAnimatedElement[], options?: Partial<UIClickAnimatorOptions>): Promise<void>;
42
+ static click(target: UIMicroAnimatedElement | UIMicroAnimatedElement[], options?: Partial<UIClickAnimatorOptions>): Promise<void>;
36
43
  }
@@ -0,0 +1,83 @@
1
+ import gsap from "gsap";
2
+ import { resolveTargets, UICallTracker } from "./miscellaneous.js";
3
+ const DEFAULT_X_OFFSET = 0;
4
+ const DEFAULT_Y_OFFSET = -25;
5
+ const DEFAULT_X_SCALE = 0.85;
6
+ const DEFAULT_Y_SCALE = 1;
7
+ const DEFAULT_DELAY = 0;
8
+ const DEFAULT_DURATION_IN = 0.125;
9
+ const DEFAULT_DURATION_OUT = 0.25;
10
+ const DEFAULT_EASE_IN = "power1.out";
11
+ const DEFAULT_EASE_OUT = "back.out";
12
+ const DEFAULT_KEEP_HIT_AREA = true;
13
+ /** A click replaces the one running on the same element, keeping its rest values */
14
+ const clickCalls = new UICallTracker(["x", "y", "scaleX", "scaleY"]);
15
+ /** Clicks in progress per micro and its affectsInput from before the first of them */
16
+ const hitAreaLocks = new WeakMap();
17
+ function lockHitArea(micro) {
18
+ const lock = hitAreaLocks.get(micro);
19
+ if (lock) {
20
+ lock.count++;
21
+ return;
22
+ }
23
+ hitAreaLocks.set(micro, { count: 1, affectsInput: micro.affectsInput });
24
+ micro.affectsInput = false;
25
+ }
26
+ function unlockHitArea(micro) {
27
+ const lock = hitAreaLocks.get(micro);
28
+ if (!lock) {
29
+ return;
30
+ }
31
+ lock.count--;
32
+ if (lock.count === 0) {
33
+ hitAreaLocks.delete(micro);
34
+ micro.affectsInput = lock.affectsInput;
35
+ }
36
+ }
37
+ /**
38
+ * Animator for click feedback effects.
39
+ */
40
+ export class UIClickAnimator {
41
+ /**
42
+ * Animates elements with click feedback including position offset and scale effects,
43
+ * relative to the transform the element rests at. A click replaces the one running on the
44
+ * same element.
45
+ * @param target - Single element or array of elements to animate
46
+ * @param options - Animation configuration options
47
+ * @returns Promise that resolves when animation completes or is interrupted
48
+ */
49
+ static click(target, options = {}) {
50
+ 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;
51
+ const elements = resolveTargets(target);
52
+ const clicks = elements.map((element) => new Promise((resolve) => {
53
+ const micro = element.micro;
54
+ if (keepHitArea) {
55
+ lockHitArea(micro);
56
+ }
57
+ clickCalls.start(micro, (rest) => gsap
58
+ .timeline({ delay })
59
+ .to(micro, {
60
+ x: rest.x + xOffset,
61
+ y: rest.y + yOffset,
62
+ scaleX: rest.scaleX * xScale,
63
+ scaleY: rest.scaleY * yScale,
64
+ duration: durationIn,
65
+ ease: easeIn,
66
+ })
67
+ .to(micro, {
68
+ x: rest.x,
69
+ y: rest.y,
70
+ scaleX: rest.scaleX,
71
+ scaleY: rest.scaleY,
72
+ duration: durationOut,
73
+ ease: easeOut,
74
+ }), () => {
75
+ if (keepHitArea) {
76
+ unlockHitArea(micro);
77
+ }
78
+ resolve();
79
+ });
80
+ }));
81
+ return Promise.all(clicks).then(() => undefined);
82
+ }
83
+ }
@@ -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
+ }
@@ -0,0 +1,67 @@
1
+ import gsap from "gsap";
2
+ import { resolveTargets, UICallTracker } from "./miscellaneous.js";
3
+ const floatCalls = new UICallTracker(["y", "rotation"]);
4
+ const DEFAULT_AMPLITUDE = 10;
5
+ const DEFAULT_PERIOD = 2;
6
+ const DEFAULT_ROTATION = 0.03;
7
+ const DEFAULT_ROTATION_PERIOD = 3;
8
+ const DEFAULT_EASE = "sine.inOut";
9
+ const DEFAULT_STOP_DURATION = 0.4;
10
+ const DEFAULT_STOP_EASE = "power1.inOut";
11
+ /**
12
+ * Animator for idle floating effects, such as a hovering item or logo.
13
+ */
14
+ export class UIFloatCallAnimator {
15
+ /**
16
+ * Starts an endless floating animation: a vertical sway with an optional rocking rotation.
17
+ * Replaces a float running on the same element.
18
+ * @param target - Single element or array of elements to animate
19
+ * @param options - Animation configuration options
20
+ */
21
+ static float(target, options = {}) {
22
+ const { amplitude = DEFAULT_AMPLITUDE, period = DEFAULT_PERIOD, rotation = DEFAULT_ROTATION, rotationPeriod = DEFAULT_ROTATION_PERIOD, ease = DEFAULT_EASE, onComplete, } = options;
23
+ for (const element of resolveTargets(target)) {
24
+ const micro = element.micro;
25
+ floatCalls.start(micro, (rest) => {
26
+ // From rest a quarter cycle to one side, then endless half cycles side to side
27
+ const timeline = gsap.timeline();
28
+ timeline
29
+ .to(micro, { y: rest.y + amplitude, duration: period / 4, ease: "sine.out" }, 0)
30
+ .to(micro, {
31
+ y: rest.y - amplitude,
32
+ duration: period / 2,
33
+ ease,
34
+ repeat: -1,
35
+ yoyo: true,
36
+ });
37
+ if (rotation !== 0) {
38
+ timeline
39
+ .to(micro, {
40
+ rotation: rest.rotation + rotation,
41
+ duration: rotationPeriod / 4,
42
+ ease: "sine.out",
43
+ }, 0)
44
+ .to(micro, {
45
+ rotation: rest.rotation - rotation,
46
+ duration: rotationPeriod / 2,
47
+ ease,
48
+ repeat: -1,
49
+ yoyo: true,
50
+ }, rotationPeriod / 4);
51
+ }
52
+ return timeline;
53
+ }, onComplete);
54
+ }
55
+ }
56
+ /**
57
+ * Stops the floating animation and returns elements to rest.
58
+ * @param target - Single element or array of elements to stop animating
59
+ * @param options - Stop animation configuration options
60
+ */
61
+ static stopFloat(target, options = {}) {
62
+ const { duration = DEFAULT_STOP_DURATION, ease = DEFAULT_STOP_EASE } = options;
63
+ for (const element of resolveTargets(target)) {
64
+ floatCalls.stop(element.micro, duration, ease);
65
+ }
66
+ }
67
+ }
@@ -0,0 +1,57 @@
1
+ import type { UIColor } from "laymur";
2
+ import type { UIMicroAnimatedElement } from "./UIAnimatedElement.js";
3
+ /**
4
+ * Element that can fly: its layout center is where the flight starts from.
5
+ */
6
+ export interface UIFlyToElement extends UIMicroAnimatedElement {
7
+ /** Center X coordinate in world units, without the micro transform */
8
+ readonly centerX: number;
9
+ /** Center Y coordinate in world units, without the micro transform */
10
+ readonly centerY: number;
11
+ /** Element color, faded when alphaTo is given */
12
+ color?: UIColor;
13
+ }
14
+ /**
15
+ * Where to fly: the center of an element, or a point in layer coordinates.
16
+ */
17
+ export type UIFlyToTarget = {
18
+ readonly centerX: number;
19
+ readonly centerY: number;
20
+ } | {
21
+ readonly x: number;
22
+ readonly y: number;
23
+ };
24
+ /**
25
+ * Configuration options for fly-to animations.
26
+ */
27
+ export interface UIFlyToAnimatorOptions {
28
+ /** Height of the arc above the straight path at its middle in world units; 0 for a line */
29
+ arcHeight: number;
30
+ /** Target scale value on arrival; the scale is kept when not given */
31
+ scaleTo: number;
32
+ /** Target alpha value on arrival, for elements with a color; kept when not given */
33
+ alphaTo: number;
34
+ /** Animation delay in seconds */
35
+ delay: number;
36
+ /** Delay between the starts of consecutive elements in seconds */
37
+ stagger: number;
38
+ /** Animation duration in seconds */
39
+ duration: number;
40
+ /** GSAP easing function along the path */
41
+ ease: gsap.EaseString;
42
+ }
43
+ /**
44
+ * Animator for flights to a target, such as collected coins flying to a counter.
45
+ */
46
+ export declare class UIFlyToAnimator {
47
+ /**
48
+ * Flies elements along an arc to the target through their micro offset; they stay there,
49
+ * so reset their micro transform once they are done with. A flight replaces the one
50
+ * running on the same element.
51
+ * @param target - Single element or array of elements to fly
52
+ * @param destination - Element or point to fly to
53
+ * @param options - Animation configuration options
54
+ * @returns Promise that resolves when animation completes or is interrupted
55
+ */
56
+ static flyTo(target: UIFlyToElement | UIFlyToElement[], destination: UIFlyToTarget, options?: Partial<UIFlyToAnimatorOptions>): Promise<void>;
57
+ }
@@ -0,0 +1,60 @@
1
+ import gsap from "gsap";
2
+ import { resolveTargets, settle } from "./miscellaneous.js";
3
+ const flights = new WeakMap();
4
+ const DEFAULT_ARC_HEIGHT = 100;
5
+ const DEFAULT_DELAY = 0;
6
+ const DEFAULT_STAGGER = 0.05;
7
+ const DEFAULT_DURATION = 0.6;
8
+ const DEFAULT_EASE = "power2.in";
9
+ /**
10
+ * Animator for flights to a target, such as collected coins flying to a counter.
11
+ */
12
+ export class UIFlyToAnimator {
13
+ /**
14
+ * Flies elements along an arc to the target through their micro offset; they stay there,
15
+ * so reset their micro transform once they are done with. A flight replaces the one
16
+ * running on the same element.
17
+ * @param target - Single element or array of elements to fly
18
+ * @param destination - Element or point to fly to
19
+ * @param options - Animation configuration options
20
+ * @returns Promise that resolves when animation completes or is interrupted
21
+ */
22
+ static flyTo(target, destination, options = {}) {
23
+ const { arcHeight = DEFAULT_ARC_HEIGHT, scaleTo, alphaTo, delay = DEFAULT_DELAY, stagger = DEFAULT_STAGGER, duration = DEFAULT_DURATION, ease = DEFAULT_EASE, } = options;
24
+ const destinationX = "centerX" in destination ? destination.centerX : destination.x;
25
+ const destinationY = "centerY" in destination ? destination.centerY : destination.y;
26
+ const animations = resolveTargets(target).map((element, index) => {
27
+ const micro = element.micro;
28
+ flights.get(micro)?.kill();
29
+ const fromX = micro.x;
30
+ const fromY = micro.y;
31
+ const toX = destinationX - element.centerX;
32
+ const toY = destinationY - element.centerY;
33
+ const progress = { value: 0 };
34
+ const timeline = gsap.timeline({ delay: delay + index * stagger });
35
+ timeline.to(progress, {
36
+ value: 1,
37
+ duration,
38
+ ease,
39
+ onUpdate: () => {
40
+ const t = progress.value;
41
+ micro.x = fromX + (toX - fromX) * t;
42
+ micro.y = fromY + (toY - fromY) * t + arcHeight * 4 * t * (1 - t);
43
+ },
44
+ }, 0);
45
+ if (scaleTo !== undefined) {
46
+ timeline.to(micro, { scaleX: scaleTo, scaleY: scaleTo, duration, ease }, 0);
47
+ }
48
+ if (alphaTo !== undefined && element.color !== undefined) {
49
+ timeline.to(element.color, { a: alphaTo, duration, ease }, 0);
50
+ }
51
+ flights.set(micro, timeline);
52
+ return settle(timeline).then(() => {
53
+ if (flights.get(micro) === timeline) {
54
+ flights.delete(micro);
55
+ }
56
+ });
57
+ });
58
+ return Promise.all(animations).then(() => undefined);
59
+ }
60
+ }