laymur-animations 0.26.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.
package/README.md CHANGED
@@ -16,6 +16,23 @@ One-shot, returning a promise: UIAppearAnimator, UIDisappearAnimator, UIClickAni
16
16
 
17
17
  Calls, running until stopped: UIPulseCallAnimator, UIJumpCallAnimator, UISpinCallAnimator, UISwipeCallAnimator, UITapCallAnimator, UIFloatCallAnimator, UIShineCallAnimator
18
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
+
19
36
  ## Documentation
20
37
 
21
38
  See [Usage Example](https://jango-git.github.io/laymur/07_animations.html) for a concrete implementation demonstration.
@@ -1,3 +1,4 @@
1
+ import type { UIDummy } from "laymur";
1
2
  import type { UIMicroAnimatedElement } from "./UIAnimatedElement.js";
2
3
  /**
3
4
  * Configuration options for click animations.
@@ -27,6 +28,22 @@ export interface UIClickAnimatorOptions {
27
28
  */
28
29
  keepHitArea: boolean;
29
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">;
30
47
  /**
31
48
  * Animator for click feedback effects.
32
49
  */
@@ -40,4 +57,28 @@ export declare class UIClickAnimator {
40
57
  * @returns Promise that resolves when animation completes or is interrupted
41
58
  */
42
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
82
+ */
83
+ static bind(element: UIClickAnimatorBindElement, options?: Partial<UIClickAnimatorBindOptions>): () => void;
43
84
  }
@@ -1,4 +1,5 @@
1
1
  import gsap from "gsap";
2
+ import { UIMode } from "laymur";
2
3
  import { resolveTargets, UICallTracker } from "./miscellaneous.js";
3
4
  const DEFAULT_X_OFFSET = 0;
4
5
  const DEFAULT_Y_OFFSET = -25;
@@ -34,6 +35,7 @@ function unlockHitArea(micro) {
34
35
  micro.affectsInput = lock.affectsInput;
35
36
  }
36
37
  }
38
+ const presses = new WeakMap();
37
39
  /**
38
40
  * Animator for click feedback effects.
39
41
  */
@@ -80,4 +82,129 @@ export class UIClickAnimator {
80
82
  }));
81
83
  return Promise.all(clicks).then(() => undefined);
82
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
+ }
83
210
  }
package/dist/index.d.ts CHANGED
@@ -1,27 +1,27 @@
1
1
  export type { UIAnimatedElement, UIMicroAnimatedElement } from "./UIAnimatedElement.js";
2
- export { UIAppearAnimator } from "./UIAppearAnimator.js";
3
2
  export type { UIAppearAnimatorOptions } from "./UIAppearAnimator.js";
3
+ export { UIAppearAnimator } from "./UIAppearAnimator.js";
4
+ export type { UIClickAnimatorBindElement, UIClickAnimatorBindOptions, UIClickAnimatorOptions, UIClickAnimatorPressOptions, UIClickAnimatorReleaseOptions, } from "./UIClickAnimator.js";
4
5
  export { UIClickAnimator } from "./UIClickAnimator.js";
5
- export type { UIClickAnimatorOptions } from "./UIClickAnimator.js";
6
- export { UIDisappearAnimator } from "./UIDisappearAnimator.js";
7
6
  export type { UIDisappearAnimatorOptions } from "./UIDisappearAnimator.js";
8
- export { UIFlashAnimator } from "./UIFlashAnimator.js";
7
+ export { UIDisappearAnimator } from "./UIDisappearAnimator.js";
9
8
  export type { UIFlashAnimatorOptions, UIFlashElement } from "./UIFlashAnimator.js";
10
- export { UIFloatCallAnimator } from "./UIFloatCallAnimator.js";
9
+ export { UIFlashAnimator } from "./UIFlashAnimator.js";
11
10
  export type { UIFloatCallAnimatorCallOptions, UIFloatCallAnimatorStopOptions, } from "./UIFloatCallAnimator.js";
12
- export { UIFlyToAnimator } from "./UIFlyToAnimator.js";
11
+ export { UIFloatCallAnimator } from "./UIFloatCallAnimator.js";
13
12
  export type { UIFlyToAnimatorOptions, UIFlyToElement, UIFlyToTarget } from "./UIFlyToAnimator.js";
14
- export { UIJumpCallAnimator } from "./UIJumpCallAnimator.js";
13
+ export { UIFlyToAnimator } from "./UIFlyToAnimator.js";
15
14
  export type { UIJumpCallAnimatorCallOptions, UIJumpCallAnimatorStopOptions, } from "./UIJumpCallAnimator.js";
16
- export { UIPulseCallAnimator } from "./UIPulseCallAnimator.js";
15
+ export { UIJumpCallAnimator } from "./UIJumpCallAnimator.js";
17
16
  export type { UIPulseCallAnimatorOptions, UIPulseCallAnimatorStopOptions, } from "./UIPulseCallAnimator.js";
18
- export { UIShakeAnimator } from "./UIShakeAnimator.js";
17
+ export { UIPulseCallAnimator } from "./UIPulseCallAnimator.js";
19
18
  export type { UIShakeAnimatorOptions } from "./UIShakeAnimator.js";
20
- export { UIShineCallAnimator } from "./UIShineCallAnimator.js";
19
+ export { UIShakeAnimator } from "./UIShakeAnimator.js";
21
20
  export type { UIShineCallAnimatorCallOptions } from "./UIShineCallAnimator.js";
22
- export { UISpinCallAnimator } from "./UISpinCallAnimator.js";
21
+ export { UIShineCallAnimator } from "./UIShineCallAnimator.js";
23
22
  export type { UISpinCallAnimatorCallOptions, UISpinCallAnimatorStopOptions, } from "./UISpinCallAnimator.js";
24
- export { UISwipeCallAnimator } from "./UISwipeCallAnimator.js";
23
+ export { UISpinCallAnimator } from "./UISpinCallAnimator.js";
25
24
  export type { UISwipeCallAnimatorCallOptions, UISwipeCallAnimatorStopOptions, } from "./UISwipeCallAnimator.js";
26
- export { UITapCallAnimator } from "./UITapCallAnimator.js";
25
+ export { UISwipeCallAnimator } from "./UISwipeCallAnimator.js";
27
26
  export type { UITapCallAnimatorCallOptions, UITapCallAnimatorStopOptions, } from "./UITapCallAnimator.js";
27
+ export { UITapCallAnimator } from "./UITapCallAnimator.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "laymur-animations",
3
- "version": "0.26.0",
3
+ "version": "0.27.0",
4
4
  "description": "Common animations for the Laymur UI library",
5
5
  "keywords": [
6
6
  "laymur",
@@ -20,6 +20,8 @@
20
20
  "scripts": {
21
21
  "clean": "rm -rf dist",
22
22
  "build": "npm run clean && tsc -p tsconfig.json",
23
+ "lint": "eslint src",
24
+ "format": "prettier --write . && eslint src --fix",
23
25
  "prepublishOnly": "npm run build"
24
26
  },
25
27
  "main": "./dist/index.js",
@@ -39,13 +41,15 @@
39
41
  },
40
42
  "peerDependencies": {
41
43
  "gsap": ">=3.13.0 <4.0.0",
42
- "laymur": ">=0.26.0 <0.27.0"
44
+ "laymur": ">=0.27.0 <0.28.0"
43
45
  },
44
46
  "devDependencies": {
45
47
  "@types/three": "^0.157.0",
46
- "@typescript-eslint/eslint-plugin": "^8.32.0",
47
- "@typescript-eslint/parser": "^8.32.0",
48
- "eslint": "^9.26.0",
48
+ "@typescript-eslint/eslint-plugin": "^8.72.1",
49
+ "@typescript-eslint/parser": "^8.72.1",
50
+ "eslint": "^10.14.0",
51
+ "eslint-plugin-simple-import-sort": "^12.1.1",
52
+ "prettier": "^3.9.10",
49
53
  "typescript": "^5.8.3"
50
54
  }
51
55
  }