@hilux/ngx-motion 1.1.1 → 2.0.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 +289 -269
- package/fesm2022/hilux-ngx-motion.mjs +2030 -0
- package/fesm2022/hilux-ngx-motion.mjs.map +1 -0
- package/package.json +35 -31
- package/types/hilux-ngx-motion.d.ts +777 -0
- package/ng-package.json +0 -7
- package/src/lib/components/motion-one-presence.component.ts +0 -116
- package/src/lib/constants/motion-defaults.constants.ts +0 -41
- package/src/lib/directives/motion-animate.directive.ts +0 -55
- package/src/lib/directives/motion-if.directive.ts +0 -138
- package/src/lib/directives/motion-one.directive.backup.ts +0 -1591
- package/src/lib/directives/motion-one.directive.ts +0 -1154
- package/src/lib/directives/motion-presence.directive.ts +0 -145
- package/src/lib/services/motion-animation-options.service.ts +0 -142
- package/src/lib/services/motion-animation.service.ts +0 -86
- package/src/lib/services/motion-browser-safe.ts +0 -64
- package/src/lib/services/motion-gesture.service.ts +0 -141
- package/src/lib/services/motion-inview.service.ts +0 -91
- package/src/lib/services/motion-one.service.ts +0 -424
- package/src/lib/services/motion-stagger.service.ts +0 -70
- package/src/lib/services/motion-timeline.service.ts +0 -182
- package/src/lib/types/motion-animation.types.ts +0 -46
- package/src/lib/utils/color.utils.ts +0 -54
- package/src/lib/utils/easing.utils.ts +0 -77
- package/src/lib/utils/index.ts +0 -7
- package/src/lib/utils/style-extraction.utils.ts +0 -70
- package/src/lib/utils/timing.utils.ts +0 -61
- package/src/lib/utils/transform.utils.ts +0 -112
- package/src/lib/utils/variant.utils.ts +0 -55
- package/src/public-api.ts +0 -21
- package/tsconfig.lib.json +0 -17
- package/tsconfig.lib.prod.json +0 -11
- package/tsconfig.spec.json +0 -15
|
@@ -1,424 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Injectable,
|
|
3
|
-
Inject,
|
|
4
|
-
signal,
|
|
5
|
-
PLATFORM_ID
|
|
6
|
-
} from '@angular/core';
|
|
7
|
-
import { DOCUMENT, isPlatformBrowser } from '@angular/common';
|
|
8
|
-
import { fromEvent, Subscription, Subject, Observable, timer } from 'rxjs';
|
|
9
|
-
import { throttleTime, debounceTime, take, map } from 'rxjs/operators';
|
|
10
|
-
import { Router } from '@angular/router';
|
|
11
|
-
|
|
12
|
-
// Define the interface for motion elements
|
|
13
|
-
export interface MotionElement {
|
|
14
|
-
elementId: string;
|
|
15
|
-
getDuration(): number;
|
|
16
|
-
runInitAnimation(): void;
|
|
17
|
-
runExitAnimation(): void;
|
|
18
|
-
cancel(): void;
|
|
19
|
-
route?: string;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
@Injectable({
|
|
23
|
-
providedIn: 'root',
|
|
24
|
-
})
|
|
25
|
-
export class MotionOneService {
|
|
26
|
-
private scrollSubscription: Subscription;
|
|
27
|
-
private motionElements: MotionElement[] = [];
|
|
28
|
-
scrollYProgress = signal(0);
|
|
29
|
-
totalExitDuration: number = 0;
|
|
30
|
-
|
|
31
|
-
// Subject that emits when all exit animations for a route are complete
|
|
32
|
-
private exitAnimationsComplete = new Subject<string>();
|
|
33
|
-
|
|
34
|
-
// Subject that emits when all enter animations for a route are complete
|
|
35
|
-
private enterAnimationsComplete = new Subject<string>();
|
|
36
|
-
|
|
37
|
-
// Track active exit animations by route
|
|
38
|
-
private activeExitAnimations: Map<string, number> = new Map();
|
|
39
|
-
|
|
40
|
-
// Track active enter animations by route
|
|
41
|
-
private activeEnterAnimations: Map<string, number> = new Map();
|
|
42
|
-
|
|
43
|
-
constructor(
|
|
44
|
-
private router: Router,
|
|
45
|
-
@Inject(DOCUMENT) private document: Document,
|
|
46
|
-
@Inject(PLATFORM_ID) private platformId: Object
|
|
47
|
-
) {
|
|
48
|
-
this.setupScrollListener();
|
|
49
|
-
this.scrollSubscription = new Subscription();
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
ngOnDestroy() {
|
|
53
|
-
if (this.scrollSubscription) {
|
|
54
|
-
this.scrollSubscription.unsubscribe();
|
|
55
|
-
}
|
|
56
|
-
this.exitAnimationsComplete.complete();
|
|
57
|
-
this.enterAnimationsComplete.complete();
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* TODO: In a future task, we want to use Angular framework's exit delay function
|
|
62
|
-
* to handle exit animations properly instead of this custom implementation.
|
|
63
|
-
*
|
|
64
|
-
* DISABLED: Exit duration calculation is disabled to prevent navigation blocking.
|
|
65
|
-
*/
|
|
66
|
-
private updateTotalExitDuration() {
|
|
67
|
-
// Always set to 0 to prevent navigation blocking
|
|
68
|
-
// TODO: Replace with Angular framework's exit delay mechanism in future tasks
|
|
69
|
-
this.totalExitDuration = 0;
|
|
70
|
-
|
|
71
|
-
// DISABLED - Original implementation commented out:
|
|
72
|
-
// if (!isPlatformBrowser(this.platformId)) return;
|
|
73
|
-
//
|
|
74
|
-
// this.totalExitDuration = this.motionElements.reduce((total, element) => {
|
|
75
|
-
// return total + element.getDuration();
|
|
76
|
-
// }, 0);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
private setupScrollListener() {
|
|
80
|
-
if (!isPlatformBrowser(this.platformId)) return;
|
|
81
|
-
|
|
82
|
-
this.scrollSubscription = fromEvent(window, 'scroll')
|
|
83
|
-
.pipe(
|
|
84
|
-
throttleTime(16), // Limit to about 60fps
|
|
85
|
-
debounceTime(1), // Wait for scroll to settle
|
|
86
|
-
)
|
|
87
|
-
.subscribe(() => {
|
|
88
|
-
this.updateScrollProgress();
|
|
89
|
-
});
|
|
90
|
-
this.updateScrollProgress();
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
private updateScrollProgress() {
|
|
94
|
-
if (!isPlatformBrowser(this.platformId)) return;
|
|
95
|
-
|
|
96
|
-
const doc = this.document.documentElement;
|
|
97
|
-
const scrollTop = window.scrollY || doc.scrollTop;
|
|
98
|
-
const scrollHeight = doc.scrollHeight - doc.clientHeight;
|
|
99
|
-
const progress =
|
|
100
|
-
scrollHeight > 0 ? Math.round((scrollTop / scrollHeight) * 100) : 0;
|
|
101
|
-
this.scrollYProgress.set(progress);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
registerMotionElement(element: MotionElement) {
|
|
105
|
-
this.motionElements.push(element);
|
|
106
|
-
this.updateTotalExitDuration();
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
unregisterMotionElement(element: MotionElement) {
|
|
110
|
-
this.motionElements = this.motionElements.filter((e) => e !== element);
|
|
111
|
-
this.updateTotalExitDuration();
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
getMotionElements(): MotionElement[] {
|
|
115
|
-
return this.motionElements;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
getAllElementsByRoute(route: string): MotionElement[] {
|
|
119
|
-
const elements = this.motionElements.filter(element => element.route === route);
|
|
120
|
-
//console.log(`[MotionOneService] Found ${elements.length} elements for route ${route} (out of ${this.motionElements.length} total)`);
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
return elements;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
cancelAllAnimations() {
|
|
128
|
-
if (!isPlatformBrowser(this.platformId)) return;
|
|
129
|
-
|
|
130
|
-
this.motionElements.forEach((motion) => {
|
|
131
|
-
motion.cancel();
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
runAllEnterAnimations(): void {
|
|
136
|
-
if (!isPlatformBrowser(this.platformId)) return;
|
|
137
|
-
|
|
138
|
-
this.motionElements.forEach((motion) => {
|
|
139
|
-
motion.runInitAnimation();
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
runAllExitAnimations(): void {
|
|
144
|
-
if (!isPlatformBrowser(this.platformId)) return;
|
|
145
|
-
|
|
146
|
-
this.motionElements.forEach((motion) => {
|
|
147
|
-
console.log(motion)
|
|
148
|
-
motion.runExitAnimation();
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Get an observable that emits true when all exit animations for a route are complete
|
|
154
|
-
* @param route The route to check for animation completion
|
|
155
|
-
* @returns Observable that emits true when animations are complete
|
|
156
|
-
*/
|
|
157
|
-
getExitAnimationsComplete(route: string): Observable<boolean> {
|
|
158
|
-
if (!isPlatformBrowser(this.platformId)) {
|
|
159
|
-
// If not in browser, immediately emit true
|
|
160
|
-
return timer(0).pipe(
|
|
161
|
-
take(1),
|
|
162
|
-
map(() => true)
|
|
163
|
-
);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// Get the exit duration for the route
|
|
167
|
-
const exitDuration = this.getLongestExitDurationForRoute(route);
|
|
168
|
-
// console.log(`[MotionOneService] Setting up exit animations complete for route ${route} with duration ${exitDuration}s`);
|
|
169
|
-
|
|
170
|
-
// If no animations or duration is 0, immediately emit true
|
|
171
|
-
if (exitDuration === 0 || this.getAllElementsByRoute(route).length === 0) {
|
|
172
|
-
// console.log(`[MotionOneService] No exit animations for route ${route}, emitting complete immediately`);
|
|
173
|
-
return timer(0).pipe(
|
|
174
|
-
take(1),
|
|
175
|
-
map(() => true)
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// Otherwise, wait for the exit duration and then emit true
|
|
180
|
-
return timer(exitDuration * 1000).pipe(
|
|
181
|
-
take(1),
|
|
182
|
-
map(() => true)
|
|
183
|
-
);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Get an observable that emits true when all enter animations for a route are complete
|
|
188
|
-
* @param route The route to check for animation completion
|
|
189
|
-
* @returns Observable that emits true when animations are complete
|
|
190
|
-
*/
|
|
191
|
-
getEnterAnimationsComplete(route: string): Observable<boolean> {
|
|
192
|
-
if (!isPlatformBrowser(this.platformId)) {
|
|
193
|
-
// If not in browser, immediately emit true
|
|
194
|
-
return timer(0).pipe(
|
|
195
|
-
take(1),
|
|
196
|
-
map(() => true)
|
|
197
|
-
);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// Get the enter duration for the route
|
|
201
|
-
const enterDuration = this.getLongestEnterDurationForRoute(route);
|
|
202
|
-
// console.log(`[MotionOneService] Setting up enter animations complete for route ${route} with duration ${enterDuration}s`);
|
|
203
|
-
|
|
204
|
-
// If no animations or duration is 0, immediately emit true
|
|
205
|
-
if (enterDuration === 0 || this.getAllElementsByRoute(route).length === 0) {
|
|
206
|
-
// console.log(`[MotionOneService] No enter animations for route ${route}, emitting complete immediately`);
|
|
207
|
-
return timer(0).pipe(
|
|
208
|
-
take(1),
|
|
209
|
-
map(() => true)
|
|
210
|
-
);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// Otherwise, wait for the enter duration and then emit true
|
|
214
|
-
return timer(enterDuration * 1000).pipe(
|
|
215
|
-
take(1),
|
|
216
|
-
map(() => true)
|
|
217
|
-
);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Start tracking exit animations for a route
|
|
222
|
-
* @param route The route to track
|
|
223
|
-
*/
|
|
224
|
-
startExitAnimationsForRoute(route: string): void {
|
|
225
|
-
if (!isPlatformBrowser(this.platformId)) return;
|
|
226
|
-
|
|
227
|
-
const elements = this.getAllElementsByRoute(route);
|
|
228
|
-
this.activeExitAnimations.set(route, elements.length);
|
|
229
|
-
|
|
230
|
-
//console.log(`[MotionOneService] Starting exit animations for route ${route} with ${elements.length} elements`);
|
|
231
|
-
|
|
232
|
-
// If no elements, immediately mark as complete
|
|
233
|
-
if (elements.length === 0) {
|
|
234
|
-
this.completeExitAnimationsForRoute(route);
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// Run exit animations for all elements in this route
|
|
239
|
-
elements.forEach(element => {
|
|
240
|
-
element.runExitAnimation();
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
// Get the longest duration and set a timer to mark animations as complete
|
|
244
|
-
const exitDuration = this.getLongestExitDurationForRoute(route);
|
|
245
|
-
setTimeout(() => {
|
|
246
|
-
this.completeExitAnimationsForRoute(route);
|
|
247
|
-
}, exitDuration * 1000);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* Start tracking enter animations for a route
|
|
252
|
-
* @param route The route to track
|
|
253
|
-
*/
|
|
254
|
-
startEnterAnimationsForRoute(route: string): void {
|
|
255
|
-
if (!isPlatformBrowser(this.platformId)) return;
|
|
256
|
-
|
|
257
|
-
const elements = this.getAllElementsByRoute(route);
|
|
258
|
-
this.activeEnterAnimations.set(route, elements.length);
|
|
259
|
-
|
|
260
|
-
// console.log(`[MotionOneService] Starting enter animations for route ${route} with ${elements.length} elements`);
|
|
261
|
-
|
|
262
|
-
// If no elements, immediately mark as complete
|
|
263
|
-
if (elements.length === 0) {
|
|
264
|
-
this.completeEnterAnimationsForRoute(route);
|
|
265
|
-
return;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
// Run enter animations for all elements in this route
|
|
269
|
-
elements.forEach(element => {
|
|
270
|
-
element.runInitAnimation();
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
// Get the longest duration and set a timer to mark animations as complete
|
|
274
|
-
const enterDuration = this.getLongestEnterDurationForRoute(route);
|
|
275
|
-
setTimeout(() => {
|
|
276
|
-
this.completeEnterAnimationsForRoute(route);
|
|
277
|
-
}, enterDuration * 1000);
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* Mark exit animations for a route as complete
|
|
282
|
-
* @param route The route to mark as complete
|
|
283
|
-
*/
|
|
284
|
-
private completeExitAnimationsForRoute(route: string): void {
|
|
285
|
-
console.log(`[MotionOneService] Exit animations complete for route ${route}`);
|
|
286
|
-
this.activeExitAnimations.delete(route);
|
|
287
|
-
this.exitAnimationsComplete.next(route);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Mark enter animations for a route as complete
|
|
292
|
-
* @param route The route to mark as complete
|
|
293
|
-
*/
|
|
294
|
-
private completeEnterAnimationsForRoute(route: string): void {
|
|
295
|
-
console.log(`[MotionOneService] Enter animations complete for route ${route}`);
|
|
296
|
-
this.activeEnterAnimations.delete(route);
|
|
297
|
-
this.enterAnimationsComplete.next(route);
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
/**
|
|
301
|
-
* Check if exit animations for a route are complete
|
|
302
|
-
* @param route The route to check
|
|
303
|
-
* @returns True if animations are complete or not running
|
|
304
|
-
*/
|
|
305
|
-
areExitAnimationsComplete(route: string): boolean {
|
|
306
|
-
return !this.activeExitAnimations.has(route);
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* Check if enter animations for a route are complete
|
|
311
|
-
* @param route The route to check
|
|
312
|
-
* @returns True if animations are complete or not running
|
|
313
|
-
*/
|
|
314
|
-
areEnterAnimationsComplete(route: string): boolean {
|
|
315
|
-
return !this.activeEnterAnimations.has(route);
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
runAllExitAnimationsForRoute(route: string): MotionElement[] {
|
|
319
|
-
if (!isPlatformBrowser(this.platformId)) return [];
|
|
320
|
-
|
|
321
|
-
// Start tracking animations for this route
|
|
322
|
-
this.startExitAnimationsForRoute(route);
|
|
323
|
-
|
|
324
|
-
return this.getAllElementsByRoute(route);
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
runAllEnterAnimationsForRoute(route: string): MotionElement[] {
|
|
328
|
-
if (!isPlatformBrowser(this.platformId)) return [];
|
|
329
|
-
|
|
330
|
-
// Start tracking animations for this route
|
|
331
|
-
this.startEnterAnimationsForRoute(route);
|
|
332
|
-
|
|
333
|
-
return this.getAllElementsByRoute(route);
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* TODO: In a future task, we want to use Angular framework's exit delay function
|
|
338
|
-
* (e.g., route guards with canDeactivate or route transition animations)
|
|
339
|
-
* to handle exit animations properly instead of this custom implementation.
|
|
340
|
-
*
|
|
341
|
-
* DISABLED: Exit duration calculation is disabled to prevent navigation blocking.
|
|
342
|
-
* Navigation should be immediate and not wait for exit animations.
|
|
343
|
-
*/
|
|
344
|
-
getLongestExitDurationForRoute(route: string): number {
|
|
345
|
-
// Always return 0 to prevent navigation blocking
|
|
346
|
-
// TODO: Replace with Angular framework's exit delay mechanism in future tasks
|
|
347
|
-
return 0;
|
|
348
|
-
|
|
349
|
-
// DISABLED - Original implementation commented out:
|
|
350
|
-
// if (!isPlatformBrowser(this.platformId)) return 0;
|
|
351
|
-
//
|
|
352
|
-
// // console.log(`[MotionOneService] Getting longest exit duration for route ${route}`);
|
|
353
|
-
//
|
|
354
|
-
// if (this.motionElements.length === 0) {
|
|
355
|
-
// // console.log('[MotionOneService] No motion elements registered');
|
|
356
|
-
// return 0;
|
|
357
|
-
// }
|
|
358
|
-
//
|
|
359
|
-
// const elements = this.getAllElementsByRoute(route);
|
|
360
|
-
// if (elements.length === 0) {
|
|
361
|
-
// // console.log(`[MotionOneService] No elements found for route ${route}`);
|
|
362
|
-
// return 0;
|
|
363
|
-
// }
|
|
364
|
-
//
|
|
365
|
-
// const durations = elements.map((motion) => {
|
|
366
|
-
// const duration = motion.getDuration();
|
|
367
|
-
// // console.log(`[MotionOneService] Element ${motion.elementId} has exit duration: ${duration}s`);
|
|
368
|
-
// return duration;
|
|
369
|
-
// });
|
|
370
|
-
//
|
|
371
|
-
// const maxDuration = durations.length > 0 ? Math.max(...durations) : 0;
|
|
372
|
-
// console.log(`[MotionOneService] Longest exit duration for route ${route}: ${maxDuration}s`);
|
|
373
|
-
// return maxDuration;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
getLongestEnterDurationForRoute(route: string): number {
|
|
377
|
-
if (!isPlatformBrowser(this.platformId)) return 0;
|
|
378
|
-
|
|
379
|
-
// console.log(`[MotionOneService] Getting longest enter duration for route ${route}`);
|
|
380
|
-
|
|
381
|
-
if (this.motionElements.length === 0) {
|
|
382
|
-
// console.log('[MotionOneService] No motion elements registered');
|
|
383
|
-
return 0;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
const elements = this.getAllElementsByRoute(route);
|
|
387
|
-
if (elements.length === 0) {
|
|
388
|
-
// console.log(`[MotionOneService] No elements found for route ${route}`);
|
|
389
|
-
return 0;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
const durations = elements.map((motion) => {
|
|
393
|
-
const duration = motion.getDuration();
|
|
394
|
-
// console.log(`[MotionOneService] Element ${motion.elementId} has enter duration: ${duration}s`);
|
|
395
|
-
return duration;
|
|
396
|
-
});
|
|
397
|
-
|
|
398
|
-
const maxDuration = durations.length > 0 ? Math.max(...durations) : 0;
|
|
399
|
-
console.log(`[MotionOneService] Longest enter duration for route ${route}: ${maxDuration}s`);
|
|
400
|
-
return maxDuration;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
getLongestEnterDuration(): number {
|
|
404
|
-
if (!isPlatformBrowser(this.platformId)) return 0;
|
|
405
|
-
|
|
406
|
-
// console.log(`[MotionOneService] Getting longest enter duration from ${this.motionElements.length} elements`);
|
|
407
|
-
|
|
408
|
-
if (this.motionElements.length === 0) {
|
|
409
|
-
// console.log('[MotionOneService] No motion elements registered');
|
|
410
|
-
return 0;
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
const durations = this.motionElements.map((motion) => {
|
|
414
|
-
const duration = motion.getDuration();
|
|
415
|
-
// console.log(`[MotionOneService] Element ${motion.elementId} has duration: ${duration}s`);
|
|
416
|
-
return duration;
|
|
417
|
-
});
|
|
418
|
-
|
|
419
|
-
const maxDuration = durations.length > 0 ? Math.max(...durations) : 0;
|
|
420
|
-
console.log(`[MotionOneService] Longest enter duration: ${maxDuration}s`);
|
|
421
|
-
return maxDuration;
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { Injectable } from '@angular/core';
|
|
2
|
-
import { TransitionOptions } from '../types/motion-animation.types';
|
|
3
|
-
import { calculateStaggerDelay, calculateTotalStaggerDuration } from '../utils/timing.utils';
|
|
4
|
-
|
|
5
|
-
export interface StaggerConfig {
|
|
6
|
-
childCount: number;
|
|
7
|
-
staggerValue: number;
|
|
8
|
-
staggerDirection: number;
|
|
9
|
-
delayChildren: number;
|
|
10
|
-
parentDelay: number;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
@Injectable({
|
|
14
|
-
providedIn: 'root'
|
|
15
|
-
})
|
|
16
|
-
export class MotionStaggerService {
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Calculate the delay for a specific child in a stagger sequence
|
|
20
|
-
*/
|
|
21
|
-
calculateChildDelay(
|
|
22
|
-
index: number,
|
|
23
|
-
config: StaggerConfig
|
|
24
|
-
): number {
|
|
25
|
-
const staggerDelay = calculateStaggerDelay(
|
|
26
|
-
index,
|
|
27
|
-
config.childCount,
|
|
28
|
-
config.staggerValue,
|
|
29
|
-
config.staggerDirection
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
return config.parentDelay + config.delayChildren + staggerDelay;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Calculate delays for all children in a stagger sequence
|
|
37
|
-
*/
|
|
38
|
-
calculateAllChildDelays(config: StaggerConfig): number[] {
|
|
39
|
-
const delays: number[] = [];
|
|
40
|
-
|
|
41
|
-
for (let i = 0; i < config.childCount; i++) {
|
|
42
|
-
delays.push(this.calculateChildDelay(i, config));
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return delays;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Calculate the total duration for a stagger animation sequence
|
|
50
|
-
*/
|
|
51
|
-
calculateTotalDuration(config: StaggerConfig): number {
|
|
52
|
-
return calculateTotalStaggerDuration(
|
|
53
|
-
config.childCount,
|
|
54
|
-
config.staggerValue,
|
|
55
|
-
config.parentDelay,
|
|
56
|
-
config.delayChildren
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Determine the stagger index based on direction
|
|
62
|
-
*/
|
|
63
|
-
getStaggerIndex(index: number, childCount: number, direction: number): number {
|
|
64
|
-
if (direction >= 0) {
|
|
65
|
-
return index; // Forward: 0, 1, 2, 3...
|
|
66
|
-
} else {
|
|
67
|
-
return childCount - 1 - index; // Reverse: 3, 2, 1, 0...
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import { Injectable } from '@angular/core';
|
|
2
|
-
import type { Target } from 'motion';
|
|
3
|
-
import { TimelineStep, TransitionOptions, VariantWithTransition } from '../types/motion-animation.types';
|
|
4
|
-
import { MotionAnimationService } from './motion-animation.service';
|
|
5
|
-
import { isColorProperty, normalizeColorValue } from '../utils/color.utils';
|
|
6
|
-
import { getEasing, getRepeatValue, mapRepeatTypeToDirection } from '../utils/easing.utils';
|
|
7
|
-
import { normalizeTimes } from '../utils/timing.utils';
|
|
8
|
-
|
|
9
|
-
@Injectable({
|
|
10
|
-
providedIn: 'root'
|
|
11
|
-
})
|
|
12
|
-
export class MotionTimelineService {
|
|
13
|
-
|
|
14
|
-
constructor(private motionAnimation: MotionAnimationService) {}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Execute a timeline with orchestrated per-property animations
|
|
18
|
-
*/
|
|
19
|
-
runTimeline(
|
|
20
|
-
element: HTMLElement,
|
|
21
|
-
timeline: TimelineStep[],
|
|
22
|
-
targetState: VariantWithTransition,
|
|
23
|
-
baseDelay: number,
|
|
24
|
-
duration: number,
|
|
25
|
-
easing: any,
|
|
26
|
-
repeat: boolean,
|
|
27
|
-
onComplete?: () => void
|
|
28
|
-
): { controls: any[]; maxEnd: number } {
|
|
29
|
-
const controls: any[] = [];
|
|
30
|
-
let maxEnd = 0;
|
|
31
|
-
|
|
32
|
-
// Also animate any non-timeline props from the target state (e.g., opacity)
|
|
33
|
-
const timelinePropsSet = new Set(timeline.map(s => s.prop));
|
|
34
|
-
const nonTimelineTarget: any = {};
|
|
35
|
-
|
|
36
|
-
Object.keys(targetState).forEach(k => {
|
|
37
|
-
if (k === 'transition' || k === 'at') return;
|
|
38
|
-
if (!timelinePropsSet.has(k)) {
|
|
39
|
-
nonTimelineTarget[k] = targetState[k];
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
if (Object.keys(nonTimelineTarget).length > 0) {
|
|
44
|
-
const options: any = {
|
|
45
|
-
duration,
|
|
46
|
-
delay: baseDelay,
|
|
47
|
-
easing,
|
|
48
|
-
ease: easing,
|
|
49
|
-
repeat: repeat ? Infinity : 0
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
const ctl = this.motionAnimation.animate(element as unknown as Target, nonTimelineTarget, options);
|
|
53
|
-
controls.push(ctl);
|
|
54
|
-
const nonEnd = (options.delay ?? 0) + (options.duration ?? duration);
|
|
55
|
-
if (nonEnd > maxEnd) maxEnd = nonEnd;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Execute each timeline step
|
|
59
|
-
timeline.forEach(step => {
|
|
60
|
-
const start = baseDelay + (step.atTime ?? 0) + (step.delay ?? 0);
|
|
61
|
-
const stepDuration = step.duration;
|
|
62
|
-
const value = step.keyframes !== undefined ? step.keyframes : step.to;
|
|
63
|
-
const easeInput = step.ease ?? easing;
|
|
64
|
-
|
|
65
|
-
const ease = getEasing(easeInput, this.motionAnimation);
|
|
66
|
-
const options: any = {
|
|
67
|
-
duration: stepDuration,
|
|
68
|
-
delay: start,
|
|
69
|
-
easing: ease,
|
|
70
|
-
ease: ease
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
// Apply repeat controls if provided
|
|
74
|
-
if (step.repeat !== undefined) {
|
|
75
|
-
options.repeat = step.repeat === true ? Infinity : step.repeat;
|
|
76
|
-
}
|
|
77
|
-
if (step.repeatDelay !== undefined) {
|
|
78
|
-
options.repeatDelay = step.repeatDelay;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const dir = mapRepeatTypeToDirection(step.repeatType);
|
|
82
|
-
if (dir) options.direction = dir;
|
|
83
|
-
if (step.repeatType) options.repeatType = step.repeatType;
|
|
84
|
-
if (step.times) options.offset = normalizeTimes(step.times, stepDuration);
|
|
85
|
-
|
|
86
|
-
const end = start + stepDuration;
|
|
87
|
-
if (end > maxEnd) maxEnd = end;
|
|
88
|
-
|
|
89
|
-
let v = value;
|
|
90
|
-
if (isColorProperty(step.prop)) {
|
|
91
|
-
v = normalizeColorValue(v);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const target = { [step.prop]: v } as any;
|
|
95
|
-
const control = this.motionAnimation.animate(element as unknown as Target, target, options);
|
|
96
|
-
controls.push(control);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
// Set up completion callback
|
|
100
|
-
if (onComplete) {
|
|
101
|
-
setTimeout(() => onComplete(), maxEnd * 1000);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return { controls, maxEnd };
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Execute per-property animations with different timing for each property
|
|
109
|
-
*/
|
|
110
|
-
runPerPropertyAnimations(
|
|
111
|
-
element: HTMLElement,
|
|
112
|
-
targetState: VariantWithTransition,
|
|
113
|
-
baseDelay: number,
|
|
114
|
-
duration: number,
|
|
115
|
-
easing: any,
|
|
116
|
-
repeat: boolean,
|
|
117
|
-
onComplete?: () => void
|
|
118
|
-
): { controls: any[]; maxEnd: number } {
|
|
119
|
-
const controls: any[] = [];
|
|
120
|
-
const transitionMap = targetState.transition as Record<string, TransitionOptions>;
|
|
121
|
-
const props = Object.keys(targetState).filter(k => k !== 'transition' && k !== 'at');
|
|
122
|
-
|
|
123
|
-
let maxEnd = 0;
|
|
124
|
-
|
|
125
|
-
props.forEach((prop) => {
|
|
126
|
-
const value = (targetState as any)[prop];
|
|
127
|
-
const conf = (transitionMap?.[prop] || {}) as TransitionOptions;
|
|
128
|
-
const delay = baseDelay + (conf.delay ?? 0);
|
|
129
|
-
const ease = getEasing(conf.ease ?? easing, this.motionAnimation);
|
|
130
|
-
const propDuration = conf.duration ?? duration;
|
|
131
|
-
|
|
132
|
-
const options: any = {
|
|
133
|
-
duration: propDuration,
|
|
134
|
-
delay,
|
|
135
|
-
easing: ease,
|
|
136
|
-
ease: ease,
|
|
137
|
-
repeat: conf.repeat !== undefined ? getRepeatValue(conf, repeat) : (repeat ? Infinity : 0),
|
|
138
|
-
repeatDelay: conf.repeatDelay
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
const dir = mapRepeatTypeToDirection(conf.repeatType);
|
|
142
|
-
if (dir) options.direction = dir;
|
|
143
|
-
if (conf.repeatType) options.repeatType = conf.repeatType;
|
|
144
|
-
|
|
145
|
-
const times = (conf as any).times as number[] | undefined;
|
|
146
|
-
if (times && Array.isArray(times)) {
|
|
147
|
-
options.offset = normalizeTimes(times, propDuration);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
const end = delay + propDuration;
|
|
151
|
-
if (end > maxEnd) maxEnd = end;
|
|
152
|
-
|
|
153
|
-
let finalValue = value;
|
|
154
|
-
if (isColorProperty(prop)) {
|
|
155
|
-
finalValue = normalizeColorValue(value);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const target = { [prop]: finalValue } as any;
|
|
159
|
-
const control = this.motionAnimation.animate(element as unknown as Target, target, options);
|
|
160
|
-
controls.push(control);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
// Set up completion callback
|
|
164
|
-
if (onComplete) {
|
|
165
|
-
setTimeout(() => onComplete(), maxEnd * 1000);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
return { controls, maxEnd };
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Check if a transition is a per-property map
|
|
173
|
-
*/
|
|
174
|
-
isPerPropertyTransition(transition: any): boolean {
|
|
175
|
-
if (!transition || typeof transition !== 'object') return false;
|
|
176
|
-
|
|
177
|
-
// A per-property transition map doesn't have top-level duration/delay/ease
|
|
178
|
-
return transition.duration === undefined &&
|
|
179
|
-
transition.delay === undefined &&
|
|
180
|
-
transition.ease === undefined;
|
|
181
|
-
}
|
|
182
|
-
}
|