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
@@ -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
+ }
@@ -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
+ }