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.
@@ -1,9 +1,9 @@
1
- import type { UIAnimatedElement } from "./UIAnimatedElement";
1
+ import type { UIMicroAnimatedElement } from "./UIAnimatedElement.js";
2
2
  /**
3
3
  * Configuration options for jump call animations.
4
4
  */
5
5
  export interface UIJumpCallAnimatorCallOptions {
6
- /** Height of the jump in pixels */
6
+ /** Height of the jump in world units */
7
7
  jumpHeight: number;
8
8
  /** Scale factor during jump start */
9
9
  scaleIn: number;
@@ -23,6 +23,8 @@ export interface UIJumpCallAnimatorCallOptions {
23
23
  easeIn: gsap.EaseString;
24
24
  /** GSAP easing function for jump out */
25
25
  easeOut: gsap.EaseString;
26
+ /** Called once the call ends: completed, stopped or replaced by another jump */
27
+ onComplete: () => void;
26
28
  }
27
29
  /**
28
30
  * Configuration options for stopping jump animations.
@@ -38,15 +40,17 @@ export interface UIJumpCallAnimatorStopOptions {
38
40
  */
39
41
  export declare class UIJumpCallAnimator {
40
42
  /**
41
- * Starts a jumping animation with scale and position effects.
43
+ * Starts a jumping animation with scale and position effects, squashing around the bottom
44
+ * edge. Replaces a jump running on the same element; the anchor is restored once the
45
+ * element is back at rest.
42
46
  * @param target - Single element or array of elements to animate
43
47
  * @param options - Animation configuration options
44
48
  */
45
- static jump(target: UIAnimatedElement | UIAnimatedElement[], options?: Partial<UIJumpCallAnimatorCallOptions>): void;
49
+ static jump(target: UIMicroAnimatedElement | UIMicroAnimatedElement[], options?: Partial<UIJumpCallAnimatorCallOptions>): void;
46
50
  /**
47
- * Stops the jumping animation and returns elements to rest position.
51
+ * Stops the jumping animation and returns elements to rest position and scale.
48
52
  * @param target - Single element or array of elements to stop animating
49
53
  * @param options - Stop animation configuration options
50
54
  */
51
- static stopJump(target: UIAnimatedElement | UIAnimatedElement[], options?: Partial<UIJumpCallAnimatorStopOptions>): void;
55
+ static stopJump(target: UIMicroAnimatedElement | UIMicroAnimatedElement[], options?: Partial<UIJumpCallAnimatorStopOptions>): void;
52
56
  }
@@ -0,0 +1,97 @@
1
+ import gsap from "gsap";
2
+ import { iterationsToRepeat, resolveTargets, UICallTracker } from "./miscellaneous.js";
3
+ const jumpCalls = new UICallTracker(["y", "scaleX", "scaleY", "anchorY"]);
4
+ const DEFAULT_JUMP_HEIGHT = 35;
5
+ const DEFAULT_SCALE_IN = 1.15;
6
+ const DEFAULT_SCALE_OUT = 1.15;
7
+ const DEFAULT_ITERATIONS = -1;
8
+ const DEFAULT_COOLDOWN = 3;
9
+ const DEFAULT_START_WITH_COOLDOWN = true;
10
+ const DEFAULT_DURATION_IN = 0.5;
11
+ const DEFAULT_DURATION_OUT = 0.5;
12
+ const DEFAULT_EASE_IN = "power1.out";
13
+ const DEFAULT_EASE_OUT = "power1.in";
14
+ const DEFAULT_STOP_DURATION = 0.25;
15
+ const DEFAULT_STOP_EASE = "power1.inOut";
16
+ /**
17
+ * Animator for jump call attention effects.
18
+ */
19
+ export class UIJumpCallAnimator {
20
+ /**
21
+ * Starts a jumping animation with scale and position effects, squashing around the bottom
22
+ * edge. Replaces a jump running on the same element; the anchor is restored once the
23
+ * element is back at rest.
24
+ * @param target - Single element or array of elements to animate
25
+ * @param options - Animation configuration options
26
+ */
27
+ static jump(target, options = {}) {
28
+ const { jumpHeight = DEFAULT_JUMP_HEIGHT, scaleIn = DEFAULT_SCALE_IN, scaleOut = DEFAULT_SCALE_OUT, iterations = DEFAULT_ITERATIONS, cooldown = DEFAULT_COOLDOWN, startWithCooldown = DEFAULT_START_WITH_COOLDOWN, durationIn = DEFAULT_DURATION_IN, durationOut = DEFAULT_DURATION_OUT, easeIn = DEFAULT_EASE_IN, easeOut = DEFAULT_EASE_OUT, onComplete, } = options;
29
+ for (const element of resolveTargets(target)) {
30
+ const micro = element.micro;
31
+ jumpCalls.start(micro, (rest) => {
32
+ micro.anchorY = 0;
33
+ const timeline = gsap.timeline({ repeat: iterationsToRepeat(iterations) });
34
+ const leadIn = startWithCooldown && cooldown > 0 ? cooldown : 0;
35
+ if (leadIn > 0) {
36
+ timeline.to({}, { duration: leadIn });
37
+ }
38
+ timeline
39
+ .to(micro, {
40
+ scaleX: rest.scaleX * scaleIn,
41
+ scaleY: rest.scaleY / scaleIn,
42
+ duration: durationIn * 0.5,
43
+ ease: "power1.inOut",
44
+ })
45
+ .to(micro, {
46
+ scaleX: rest.scaleX / scaleIn,
47
+ scaleY: rest.scaleY * scaleIn,
48
+ duration: durationIn * 0.25,
49
+ ease: "power1.inOut",
50
+ })
51
+ .to(micro, {
52
+ scaleX: rest.scaleX,
53
+ scaleY: rest.scaleY,
54
+ duration: durationIn * 0.25,
55
+ ease: "power1.inOut",
56
+ })
57
+ .to(micro, {
58
+ y: rest.y + jumpHeight,
59
+ duration: durationIn * 0.375,
60
+ ease: easeIn,
61
+ }, leadIn + durationIn * 0.625)
62
+ .to(micro, {
63
+ y: rest.y,
64
+ duration: durationOut * 0.375,
65
+ ease: easeOut,
66
+ })
67
+ .to(micro, {
68
+ scaleX: rest.scaleX * scaleOut,
69
+ scaleY: rest.scaleY / scaleOut,
70
+ duration: durationOut * 0.225,
71
+ ease: "power1.out",
72
+ })
73
+ .to(micro, {
74
+ scaleX: rest.scaleX,
75
+ scaleY: rest.scaleY,
76
+ duration: durationOut * 0.4,
77
+ ease: "power1.inOut",
78
+ });
79
+ if (!startWithCooldown && cooldown > 0) {
80
+ timeline.to({}, { duration: cooldown });
81
+ }
82
+ return timeline;
83
+ }, onComplete);
84
+ }
85
+ }
86
+ /**
87
+ * Stops the jumping animation and returns elements to rest position and scale.
88
+ * @param target - Single element or array of elements to stop animating
89
+ * @param options - Stop animation configuration options
90
+ */
91
+ static stopJump(target, options = {}) {
92
+ const { duration = DEFAULT_STOP_DURATION, ease = DEFAULT_STOP_EASE } = options;
93
+ for (const element of resolveTargets(target)) {
94
+ jumpCalls.stop(element.micro, duration, ease);
95
+ }
96
+ }
97
+ }
@@ -1,10 +1,12 @@
1
- import type { UIAnimatedElement } from "./UIAnimatedElement";
1
+ import type { UIMicroAnimatedElement } from "./UIAnimatedElement.js";
2
2
  /**
3
3
  * Configuration options for pulse call animations.
4
4
  */
5
5
  export interface UIPulseCallAnimatorOptions {
6
- /** Scale factor for pulse effect */
6
+ /** Scale factor for pulse effect, relative to the rest scale */
7
7
  scale: number;
8
+ /** Number of pulses in a row per iteration; 2 with short durations makes a heartbeat */
9
+ beats: number;
8
10
  /** Number of animation iterations (-1 for infinite) */
9
11
  iterations: number;
10
12
  /** Cooldown time between iterations in seconds */
@@ -19,12 +21,14 @@ export interface UIPulseCallAnimatorOptions {
19
21
  easeIn: gsap.EaseString;
20
22
  /** GSAP easing function for scale out */
21
23
  easeOut: gsap.EaseString;
24
+ /** Called once the call ends: completed, stopped or replaced by another pulse */
25
+ onComplete: () => void;
22
26
  }
23
27
  /**
24
28
  * Configuration options for stopping pulse animations.
25
29
  */
26
30
  export interface UIPulseCallAnimatorStopOptions {
27
- /** Target scale value when stopping */
31
+ /** Target scale value when stopping; the rest scale by default */
28
32
  scale: number;
29
33
  /** Duration to return to rest scale in seconds */
30
34
  duration: number;
@@ -36,15 +40,16 @@ export interface UIPulseCallAnimatorStopOptions {
36
40
  */
37
41
  export declare class UIPulseCallAnimator {
38
42
  /**
39
- * Starts a pulsing animation with scale effects.
43
+ * Starts a pulsing animation with scale effects. Replaces a pulse running on the same
44
+ * element; the scale it rests at is kept from the first one.
40
45
  * @param target - Single element or array of elements to animate
41
46
  * @param options - Animation configuration options
42
47
  */
43
- static pulse(target: UIAnimatedElement | UIAnimatedElement[], options?: Partial<UIPulseCallAnimatorOptions>): void;
48
+ static pulse(target: UIMicroAnimatedElement | UIMicroAnimatedElement[], options?: Partial<UIPulseCallAnimatorOptions>): void;
44
49
  /**
45
50
  * Stops the pulsing animation and returns elements to rest scale.
46
51
  * @param target - Single element or array of elements to stop animating
47
52
  * @param options - Stop animation configuration options
48
53
  */
49
- static stopPulse(target: UIAnimatedElement | UIAnimatedElement[], options?: Partial<UIPulseCallAnimatorStopOptions>): void;
54
+ static stopPulse(target: UIMicroAnimatedElement | UIMicroAnimatedElement[], options?: Partial<UIPulseCallAnimatorStopOptions>): void;
50
55
  }
@@ -0,0 +1,67 @@
1
+ import gsap from "gsap";
2
+ import { iterationsToRepeat, resolveTargets, UICallTracker } from "./miscellaneous.js";
3
+ const pulseCalls = new UICallTracker(["scaleX", "scaleY"]);
4
+ const DEFAULT_SCALE = 1.1;
5
+ const DEFAULT_BEATS = 1;
6
+ const DEFAULT_ITERATIONS = -1;
7
+ const DEFAULT_COOLDOWN = 3;
8
+ const DEFAULT_START_WITH_COOLDOWN = true;
9
+ const DEFAULT_DURATION_IN = 0.25;
10
+ const DEFAULT_DURATION_OUT = 0.5;
11
+ const DEFAULT_EASE_IN = "power1.inOut";
12
+ const DEFAULT_EASE_OUT = "power1.inOut";
13
+ const DEFAULT_STOP_DURATION = 0.25;
14
+ const DEFAULT_STOP_EASE = "power1.inOut";
15
+ /**
16
+ * Animator for pulse attention effects.
17
+ */
18
+ export class UIPulseCallAnimator {
19
+ /**
20
+ * Starts a pulsing animation with scale effects. Replaces a pulse running on the same
21
+ * element; the scale it rests at is kept from the first one.
22
+ * @param target - Single element or array of elements to animate
23
+ * @param options - Animation configuration options
24
+ */
25
+ static pulse(target, options = {}) {
26
+ const { scale = DEFAULT_SCALE, beats = DEFAULT_BEATS, iterations = DEFAULT_ITERATIONS, cooldown = DEFAULT_COOLDOWN, startWithCooldown = DEFAULT_START_WITH_COOLDOWN, durationIn = DEFAULT_DURATION_IN, durationOut = DEFAULT_DURATION_OUT, easeIn = DEFAULT_EASE_IN, easeOut = DEFAULT_EASE_OUT, onComplete, } = options;
27
+ for (const element of resolveTargets(target)) {
28
+ const micro = element.micro;
29
+ pulseCalls.start(micro, (rest) => {
30
+ const timeline = gsap.timeline({ repeat: iterationsToRepeat(iterations) });
31
+ if (startWithCooldown && cooldown > 0) {
32
+ timeline.to({}, { duration: cooldown });
33
+ }
34
+ for (let beat = 0; beat < Math.max(beats, 1); beat++) {
35
+ timeline
36
+ .to(micro, {
37
+ scaleX: rest.scaleX * scale,
38
+ scaleY: rest.scaleY * scale,
39
+ duration: durationIn,
40
+ ease: easeIn,
41
+ })
42
+ .to(micro, {
43
+ scaleX: rest.scaleX,
44
+ scaleY: rest.scaleY,
45
+ duration: durationOut,
46
+ ease: easeOut,
47
+ });
48
+ }
49
+ if (!startWithCooldown && cooldown > 0) {
50
+ timeline.to({}, { duration: cooldown });
51
+ }
52
+ return timeline;
53
+ }, onComplete);
54
+ }
55
+ }
56
+ /**
57
+ * Stops the pulsing animation and returns elements to rest scale.
58
+ * @param target - Single element or array of elements to stop animating
59
+ * @param options - Stop animation configuration options
60
+ */
61
+ static stopPulse(target, options = {}) {
62
+ const { scale, duration = DEFAULT_STOP_DURATION, ease = DEFAULT_STOP_EASE } = options;
63
+ for (const element of resolveTargets(target)) {
64
+ pulseCalls.stop(element.micro, duration, ease, scale === undefined ? {} : { scaleX: scale, scaleY: scale });
65
+ }
66
+ }
67
+ }
@@ -0,0 +1,31 @@
1
+ import type { UIMicroAnimatedElement } from "./UIAnimatedElement.js";
2
+ /**
3
+ * Configuration options for shake animations.
4
+ */
5
+ export interface UIShakeAnimatorOptions {
6
+ /** Horizontal displacement of the first swing in world units; later swings decay */
7
+ amplitude: number;
8
+ /** Rotation of the first swing in radians, swinging along with the displacement */
9
+ rotation: number;
10
+ /** Number of swings */
11
+ shakes: number;
12
+ /** Animation delay in seconds */
13
+ delay: number;
14
+ /** Total animation duration in seconds */
15
+ duration: number;
16
+ /** GSAP easing function of each swing */
17
+ ease: gsap.EaseString;
18
+ }
19
+ /**
20
+ * Animator for decaying shake effects, such as an error or refusal feedback.
21
+ */
22
+ export declare class UIShakeAnimator {
23
+ /**
24
+ * Shakes elements horizontally around the position they rest at, with swings decaying to
25
+ * nothing. A shake replaces the one running on the same element.
26
+ * @param target - Single element or array of elements to animate
27
+ * @param options - Animation configuration options
28
+ * @returns Promise that resolves when animation completes or is interrupted
29
+ */
30
+ static shake(target: UIMicroAnimatedElement | UIMicroAnimatedElement[], options?: Partial<UIShakeAnimatorOptions>): Promise<void>;
31
+ }
@@ -0,0 +1,49 @@
1
+ import gsap from "gsap";
2
+ import { resolveTargets, UICallTracker } from "./miscellaneous.js";
3
+ const shakeCalls = new UICallTracker(["x", "rotation"]);
4
+ const DEFAULT_AMPLITUDE = 12;
5
+ const DEFAULT_ROTATION = 0;
6
+ const DEFAULT_SHAKES = 5;
7
+ const DEFAULT_DELAY = 0;
8
+ const DEFAULT_DURATION = 0.4;
9
+ const DEFAULT_EASE = "sine.inOut";
10
+ /**
11
+ * Animator for decaying shake effects, such as an error or refusal feedback.
12
+ */
13
+ export class UIShakeAnimator {
14
+ /**
15
+ * Shakes elements horizontally around the position they rest at, with swings decaying to
16
+ * nothing. A shake replaces the one running on the same element.
17
+ * @param target - Single element or array of elements to animate
18
+ * @param options - Animation configuration options
19
+ * @returns Promise that resolves when animation completes or is interrupted
20
+ */
21
+ static shake(target, options = {}) {
22
+ const { amplitude = DEFAULT_AMPLITUDE, rotation = DEFAULT_ROTATION, shakes = DEFAULT_SHAKES, delay = DEFAULT_DELAY, duration = DEFAULT_DURATION, ease = DEFAULT_EASE, } = options;
23
+ const swings = Math.max(Math.round(shakes), 1);
24
+ const swingDuration = duration / (swings + 1);
25
+ const animations = resolveTargets(target).map((element) => new Promise((resolve) => {
26
+ const micro = element.micro;
27
+ shakeCalls.start(micro, (rest) => {
28
+ const timeline = gsap.timeline({ delay });
29
+ for (let i = 0; i < swings; i++) {
30
+ const direction = i % 2 === 0 ? 1 : -1;
31
+ const decay = 1 - i / swings;
32
+ timeline.to(micro, {
33
+ x: rest.x + amplitude * direction * decay,
34
+ rotation: rest.rotation + rotation * direction * decay,
35
+ duration: swingDuration,
36
+ ease,
37
+ });
38
+ }
39
+ return timeline.to(micro, {
40
+ x: rest.x,
41
+ rotation: rest.rotation,
42
+ duration: swingDuration,
43
+ ease,
44
+ });
45
+ }, resolve);
46
+ }));
47
+ return Promise.all(animations).then(() => undefined);
48
+ }
49
+ }
@@ -0,0 +1,49 @@
1
+ import type { UIMicroAnimatedElement } from "./UIAnimatedElement.js";
2
+ /**
3
+ * Configuration options for shine call animations.
4
+ */
5
+ export interface UIShineCallAnimatorCallOptions {
6
+ /** Horizontal length of the sweep in world units, centered on the rest position */
7
+ dx: number;
8
+ /** Vertical length of the sweep in world units, centered on the rest position */
9
+ dy: number;
10
+ /** Number of animation iterations (-1 for infinite) */
11
+ iterations: number;
12
+ /** Cooldown time between sweeps in seconds */
13
+ cooldown: number;
14
+ /** Whether to start with cooldown delay */
15
+ startWithCooldown: boolean;
16
+ /** Duration of a sweep in seconds */
17
+ duration: number;
18
+ /** GSAP easing function of a sweep */
19
+ ease: gsap.EaseString;
20
+ /** Called once the call ends: completed, stopped or replaced by another shine */
21
+ onComplete: () => void;
22
+ }
23
+ /**
24
+ * Animator for shine sweeps across an element, such as a glint running over a button.
25
+ *
26
+ * The shine is an element of its own, such as a diagonal light band image, laid at the center
27
+ * of the button and clipped by a mask following the button:
28
+ *
29
+ * ```ts
30
+ * const shine = new UIImage(layer, bandTexture, { mask: new UIMask(button) });
31
+ * UIShineCallAnimator.shine(shine, { dx: button.width * 1.5 });
32
+ * ```
33
+ *
34
+ * Between sweeps the shine waits at the start of the sweep, outside of the mask.
35
+ */
36
+ export declare class UIShineCallAnimator {
37
+ /**
38
+ * Starts sweeping the shine from one end of the vector (dx, dy) to the other, centered on
39
+ * its rest position. Replaces a shine running on the same element.
40
+ * @param target - Single shine element or array of them
41
+ * @param options - Animation configuration options
42
+ */
43
+ static shine(target: UIMicroAnimatedElement | UIMicroAnimatedElement[], options?: Partial<UIShineCallAnimatorCallOptions>): void;
44
+ /**
45
+ * Stops the sweeps at once, leaving the shine at the start of the sweep, outside of the mask.
46
+ * @param target - Single shine element or array of them
47
+ */
48
+ static stopShine(target: UIMicroAnimatedElement | UIMicroAnimatedElement[]): void;
49
+ }
@@ -0,0 +1,74 @@
1
+ import gsap from "gsap";
2
+ import { iterationsToRepeat, resolveTargets, UICallTracker } from "./miscellaneous.js";
3
+ const shineCalls = new UICallTracker(["x", "y"]);
4
+ /**
5
+ * Center of the sweep, taken from the first shine, and the start of the last sweep. Kept after a
6
+ * stop: the shine then waits at the start, which is not where it rests.
7
+ */
8
+ const shineSweeps = new WeakMap();
9
+ const DEFAULT_DX = 300;
10
+ const DEFAULT_DY = 0;
11
+ const DEFAULT_ITERATIONS = -1;
12
+ const DEFAULT_COOLDOWN = 2.5;
13
+ const DEFAULT_START_WITH_COOLDOWN = true;
14
+ const DEFAULT_DURATION = 0.6;
15
+ const DEFAULT_EASE = "power1.inOut";
16
+ /**
17
+ * Animator for shine sweeps across an element, such as a glint running over a button.
18
+ *
19
+ * The shine is an element of its own, such as a diagonal light band image, laid at the center
20
+ * of the button and clipped by a mask following the button:
21
+ *
22
+ * ```ts
23
+ * const shine = new UIImage(layer, bandTexture, { mask: new UIMask(button) });
24
+ * UIShineCallAnimator.shine(shine, { dx: button.width * 1.5 });
25
+ * ```
26
+ *
27
+ * Between sweeps the shine waits at the start of the sweep, outside of the mask.
28
+ */
29
+ export class UIShineCallAnimator {
30
+ /**
31
+ * Starts sweeping the shine from one end of the vector (dx, dy) to the other, centered on
32
+ * its rest position. Replaces a shine running on the same element.
33
+ * @param target - Single shine element or array of them
34
+ * @param options - Animation configuration options
35
+ */
36
+ static shine(target, options = {}) {
37
+ const { dx = DEFAULT_DX, dy = DEFAULT_DY, iterations = DEFAULT_ITERATIONS, cooldown = DEFAULT_COOLDOWN, startWithCooldown = DEFAULT_START_WITH_COOLDOWN, duration = DEFAULT_DURATION, ease = DEFAULT_EASE, onComplete, } = options;
38
+ for (const element of resolveTargets(target)) {
39
+ const micro = element.micro;
40
+ shineCalls.start(micro, (rest) => {
41
+ const centerX = shineSweeps.get(micro)?.centerX ?? rest.x;
42
+ const centerY = shineSweeps.get(micro)?.centerY ?? rest.y;
43
+ const startX = centerX - dx / 2;
44
+ const startY = centerY - dy / 2;
45
+ shineSweeps.set(micro, { centerX, centerY, startX, startY });
46
+ micro.x = startX;
47
+ micro.y = startY;
48
+ const timeline = gsap.timeline({ repeat: iterationsToRepeat(iterations) });
49
+ if (startWithCooldown && cooldown > 0) {
50
+ timeline.to({}, { duration: cooldown });
51
+ }
52
+ timeline
53
+ .to(micro, { x: centerX + dx / 2, y: centerY + dy / 2, duration, ease })
54
+ .set(micro, { x: startX, y: startY });
55
+ if (!startWithCooldown && cooldown > 0) {
56
+ timeline.to({}, { duration: cooldown });
57
+ }
58
+ return timeline;
59
+ }, onComplete);
60
+ }
61
+ }
62
+ /**
63
+ * Stops the sweeps at once, leaving the shine at the start of the sweep, outside of the mask.
64
+ * @param target - Single shine element or array of them
65
+ */
66
+ static stopShine(target) {
67
+ for (const element of resolveTargets(target)) {
68
+ const sweep = shineSweeps.get(element.micro);
69
+ if (sweep) {
70
+ shineCalls.stop(element.micro, 0, "none", { x: sweep.startX, y: sweep.startY });
71
+ }
72
+ }
73
+ }
74
+ }
@@ -1,9 +1,9 @@
1
- import type { UIAnimatedElement } from "./UIAnimatedElement";
1
+ import type { UIMicroAnimatedElement } from "./UIAnimatedElement.js";
2
2
  /**
3
3
  * Configuration options for spin call animations.
4
4
  */
5
5
  export interface UISpinCallAnimatorCallOptions {
6
- /** Rotation angle in radians */
6
+ /** Rotation angle in radians, relative to the rest rotation */
7
7
  rotation: number;
8
8
  /** Anchor point X coordinate (0-1) */
9
9
  anchorX: number;
@@ -21,6 +21,8 @@ export interface UISpinCallAnimatorCallOptions {
21
21
  duration: number;
22
22
  /** GSAP easing function */
23
23
  ease: gsap.EaseString;
24
+ /** Called once the call ends: completed, stopped or replaced by another spin */
25
+ onComplete: () => void;
24
26
  }
25
27
  /**
26
28
  * Configuration options for stopping spin animations.
@@ -36,15 +38,16 @@ export interface UISpinCallAnimatorStopOptions {
36
38
  */
37
39
  export declare class UISpinCallAnimator {
38
40
  /**
39
- * Starts a spinning animation with rotation oscillations.
41
+ * Starts a spinning animation with rotation oscillations. Replaces a spin running on the same
42
+ * element; the anchor is restored once the element is back at rest.
40
43
  * @param target - Single element or array of elements to animate
41
44
  * @param options - Animation configuration options
42
45
  */
43
- static spin(target: UIAnimatedElement | UIAnimatedElement[], options?: Partial<UISpinCallAnimatorCallOptions>): void;
46
+ static spin(target: UIMicroAnimatedElement | UIMicroAnimatedElement[], options?: Partial<UISpinCallAnimatorCallOptions>): void;
44
47
  /**
45
48
  * Stops the spinning animation and returns elements to rest rotation.
46
49
  * @param target - Single element or array of elements to stop animating
47
50
  * @param options - Stop animation configuration options
48
51
  */
49
- static stopSpin(target: UIAnimatedElement | UIAnimatedElement[], options?: Partial<UISpinCallAnimatorStopOptions>): void;
52
+ static stopSpin(target: UIMicroAnimatedElement | UIMicroAnimatedElement[], options?: Partial<UISpinCallAnimatorStopOptions>): void;
50
53
  }
@@ -0,0 +1,74 @@
1
+ import gsap from "gsap";
2
+ import { iterationsToRepeat, resolveTargets, UICallTracker } from "./miscellaneous.js";
3
+ const spinCalls = new UICallTracker(["rotation", "anchorX", "anchorY"]);
4
+ const DEFAULT_ROTATION = 0.0435;
5
+ const DEFAULT_ANCHOR_X = 0.5;
6
+ const DEFAULT_ANCHOR_Y = 0.5;
7
+ const DEFAULT_SPIN_COUNT = 3;
8
+ const DEFAULT_ITERATIONS = -1;
9
+ const DEFAULT_COOLDOWN = 3;
10
+ const DEFAULT_START_WITH_COOLDOWN = true;
11
+ const DEFAULT_DURATION = 1;
12
+ const DEFAULT_EASE = "power1.inOut";
13
+ const DEFAULT_STOP_DURATION = 0.25;
14
+ const DEFAULT_STOP_EASE = "power1.inOut";
15
+ /**
16
+ * Animator for spin call attention effects.
17
+ */
18
+ export class UISpinCallAnimator {
19
+ /**
20
+ * Starts a spinning animation with rotation oscillations. Replaces a spin running on the same
21
+ * element; the anchor is restored once the element is back at rest.
22
+ * @param target - Single element or array of elements to animate
23
+ * @param options - Animation configuration options
24
+ */
25
+ static spin(target, options = {}) {
26
+ const { rotation = DEFAULT_ROTATION, anchorX = DEFAULT_ANCHOR_X, anchorY = DEFAULT_ANCHOR_Y, spinCount = DEFAULT_SPIN_COUNT, iterations = DEFAULT_ITERATIONS, cooldown = DEFAULT_COOLDOWN, startWithCooldown = DEFAULT_START_WITH_COOLDOWN, duration = DEFAULT_DURATION, ease = DEFAULT_EASE, onComplete, } = options;
27
+ for (const element of resolveTargets(target)) {
28
+ const micro = element.micro;
29
+ spinCalls.start(micro, (rest) => {
30
+ micro.anchorX = anchorX;
31
+ micro.anchorY = anchorY;
32
+ const timeline = gsap.timeline({ repeat: iterationsToRepeat(iterations) });
33
+ if (startWithCooldown && cooldown > 0) {
34
+ timeline.to({}, { duration: cooldown });
35
+ }
36
+ const spinDuration = duration / (spinCount + 2);
37
+ timeline.to(micro, {
38
+ rotation: rest.rotation + rotation,
39
+ duration: spinDuration,
40
+ ease,
41
+ });
42
+ let currentRotation = rotation;
43
+ for (let i = 0; i < spinCount; i++) {
44
+ currentRotation *= -1;
45
+ timeline.to(micro, {
46
+ rotation: rest.rotation + currentRotation,
47
+ duration: spinDuration,
48
+ ease,
49
+ });
50
+ }
51
+ timeline.to(micro, {
52
+ rotation: rest.rotation,
53
+ duration: spinDuration,
54
+ ease,
55
+ });
56
+ if (!startWithCooldown && cooldown > 0) {
57
+ timeline.to({}, { duration: cooldown });
58
+ }
59
+ return timeline;
60
+ }, onComplete);
61
+ }
62
+ }
63
+ /**
64
+ * Stops the spinning animation and returns elements to rest rotation.
65
+ * @param target - Single element or array of elements to stop animating
66
+ * @param options - Stop animation configuration options
67
+ */
68
+ static stopSpin(target, options = {}) {
69
+ const { duration = DEFAULT_STOP_DURATION, ease = DEFAULT_STOP_EASE } = options;
70
+ for (const element of resolveTargets(target)) {
71
+ spinCalls.stop(element.micro, duration, ease);
72
+ }
73
+ }
74
+ }
@@ -1,11 +1,11 @@
1
- import type { UIAnimatedElement } from "./UIAnimatedElement";
1
+ import type { UIMicroAnimatedElement } from "./UIAnimatedElement.js";
2
2
  /**
3
3
  * Configuration options for swipe call animations.
4
4
  */
5
5
  export interface UISwipeCallAnimatorCallOptions {
6
- /** Horizontal displacement of the swipe vector in pixels */
6
+ /** Horizontal displacement of the swipe vector in world units */
7
7
  dx: number;
8
- /** Vertical displacement of the swipe vector in pixels */
8
+ /** Vertical displacement of the swipe vector in world units */
9
9
  dy: number;
10
10
  /** Number of animation iterations (-1 for infinite) */
11
11
  iterations: number;
@@ -19,6 +19,8 @@ export interface UISwipeCallAnimatorCallOptions {
19
19
  easeIn: gsap.EaseString;
20
20
  /** GSAP easing function for the return swipe */
21
21
  easeOut: gsap.EaseString;
22
+ /** Called once the call ends: completed, stopped or replaced by another swipe */
23
+ onComplete: () => void;
22
24
  }
23
25
  /**
24
26
  * Configuration options for stopping swipe animations.
@@ -34,15 +36,16 @@ export interface UISwipeCallAnimatorStopOptions {
34
36
  */
35
37
  export declare class UISwipeCallAnimator {
36
38
  /**
37
- * Starts an infinite pingpong swipe animation along the vector (dx, dy).
39
+ * Starts a pingpong swipe animation along the vector (dx, dy) from the rest position.
40
+ * Replaces a swipe running on the same element.
38
41
  * @param target - Single element or array of elements to animate
39
42
  * @param options - Animation configuration options
40
43
  */
41
- static swipe(target: UIAnimatedElement | UIAnimatedElement[], options?: Partial<UISwipeCallAnimatorCallOptions>): void;
44
+ static swipe(target: UIMicroAnimatedElement | UIMicroAnimatedElement[], options?: Partial<UISwipeCallAnimatorCallOptions>): void;
42
45
  /**
43
46
  * Stops the swipe animation and returns elements to rest position.
44
47
  * @param target - Single element or array of elements to stop animating
45
48
  * @param options - Stop animation configuration options
46
49
  */
47
- static stopSwipe(target: UIAnimatedElement | UIAnimatedElement[], options?: Partial<UISwipeCallAnimatorStopOptions>): void;
50
+ static stopSwipe(target: UIMicroAnimatedElement | UIMicroAnimatedElement[], options?: Partial<UISwipeCallAnimatorStopOptions>): void;
48
51
  }