-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseFlyToTarget.ts
More file actions
295 lines (257 loc) · 8.1 KB
/
Copy pathuseFlyToTarget.ts
File metadata and controls
295 lines (257 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import { useCallback, useRef } from 'react';
import gsap from 'gsap';
import { MotionPathPlugin } from 'gsap/MotionPathPlugin';
// Ensure plugin is registered when the module is imported
if (typeof window !== 'undefined') {
gsap.registerPlugin(MotionPathPlugin);
}
type PositionType = 'center'
| 'topLeft'
| 'topCenter'
| 'topRight'
| 'leftCenter'
| 'rightCenter'
| 'bottomLeft'
| 'bottomCenter'
| 'bottomRight';
export interface FlyToTargetConfig {
// Motion parameters
motionDuration?: number;
motionEase?: string;
swoopAmount?: number;
// Target parameters
targetPosition?: PositionType;
targetPositionOffset?: { x: number; y: number };
// Scale animation parameters
scale?: boolean;
grabScale?: number;
scaleStartDelay?: number;
scaleUpDuration?: number;
scaleUpEase?: string;
scalePeak?: number;
scalePause?: number;
scaleDownDuration?: number;
scaleDownEase?: string;
scaleTarget?: number;
// Shadow parameters
enableShadow?: boolean;
baseShadowBlur?: number;
// Callbacks
onComplete?: () => void;
onStart?: () => void;
}
const DEFAULT_CONFIG: Required<FlyToTargetConfig> = {
motionDuration: 0.45,
motionEase: 'power3.out',
swoopAmount: -100,
targetPosition: 'center',
targetPositionOffset: { x: 0, y: 0 },
scale: true,
grabScale: 1.2,
scaleStartDelay: 0,
scaleUpDuration: 0.2,
scaleUpEase: 'power3.in',
scalePeak: 2.5,
scalePause: 0.15,
scaleDownDuration: 0.1,
scaleDownEase: 'none',
scaleTarget: 1.0,
enableShadow: true,
baseShadowBlur: 4,
onComplete: () => {},
onStart: () => {},
};
/**
* Hook for animating elements to fly from their current position to a target element
* with optional curved path and scale animations.
*
* @example
* const flyToTarget = useFlyToTarget();
*
* // Later, trigger the animation
* flyToTarget(dartElement, targetElement, {
* motionDuration: 0.5,
* swoopAmount: -100,
* onComplete: () => console.log('Animation complete!')
* });
*/
export function useFlyToTarget() {
const activeAnimationsRef = useRef<gsap.core.Timeline[]>([]);
const flyToTarget = useCallback((
sourceElement: HTMLElement | null,
targetElement: HTMLElement | null,
userConfig: FlyToTargetConfig = {}
) => {
if (!sourceElement || !targetElement) {
console.warn('useFlyToTarget: source or target element is null');
return null;
}
const config = { ...DEFAULT_CONFIG, ...userConfig };
// Get current position (including any transforms)
const sourceRect = sourceElement.getBoundingClientRect();
const targetRect = targetElement.getBoundingClientRect();
// Get current transform values
const currentX = gsap.getProperty(sourceElement, 'x') as number;
const currentY = gsap.getProperty(sourceElement, 'y') as number;
// Calculate target point based on targetPosition config
const getTargetPoint = (rect: DOMRect, position: PositionType): { x: number; y: number } => {
const xPositions = {
left: rect.left,
center: rect.left + rect.width / 2,
right: rect.left + rect.width,
};
const yPositions = {
top: rect.top,
center: rect.top + rect.height / 2,
bottom: rect.top + rect.height,
};
let pos: { x: number; y: number };
switch (position) {
case 'topLeft':
pos = { x: xPositions.left, y: yPositions.top };
break;
case 'topCenter':
pos = { x: xPositions.center, y: yPositions.top };
break;
case 'topRight':
pos = { x: xPositions.right, y: yPositions.top };
break;
case 'leftCenter':
pos = { x: xPositions.left, y: yPositions.center };
break;
case 'rightCenter':
pos = { x: xPositions.right, y: yPositions.center };
break;
case 'bottomLeft':
pos = { x: xPositions.left, y: yPositions.bottom };
break;
case 'bottomCenter':
pos = { x: xPositions.center, y: yPositions.bottom };
break;
case 'bottomRight':
pos = { x: xPositions.right, y: yPositions.bottom };
break;
case 'center':
default:
pos = { x: xPositions.center, y: yPositions.center };
}
// Apply target position offset
pos.x += config.targetPositionOffset.x;
pos.y += config.targetPositionOffset.y;
return pos;
};
// Calculate the target absolute position (source center to target position)
const targetPoint = getTargetPoint(targetRect, config.targetPosition);
const sourceCenter = { x: sourceRect.left + sourceRect.width / 2, y: sourceRect.top + sourceRect.height / 2 };
const targetX = currentX + (targetPoint.x - sourceCenter.x);
const targetY = currentY + (targetPoint.y - sourceCenter.y);
// Call onStart callback
config.onStart();
// Create timeline
const tl = gsap.timeline({
onComplete: config.onComplete,
});
// Build motion animation config
const motionConfig: gsap.TweenVars = {
duration: config.motionDuration,
ease: config.motionEase,
};
if (config.swoopAmount !== 0) {
// Calculate control point for bezier curve
const midX = (currentX + targetX) / 2;
const midY = (currentY + targetY) / 2;
// Calculate perpendicular offset
const dx = targetX - currentX;
const dy = targetY - currentY;
const perpX = -dy;
const perpY = dx;
const length = Math.sqrt(perpX * perpX + perpY * perpY);
const normalizedPerpX = perpX / length;
const normalizedPerpY = perpY / length;
// Control point offset by swoopAmount
const controlX = midX + normalizedPerpX * config.swoopAmount;
const controlY = midY + normalizedPerpY * config.swoopAmount;
motionConfig.motionPath = {
path: [
{ x: currentX, y: currentY },
{ x: controlX, y: controlY },
{ x: targetX, y: targetY }
],
curviness: 2,
resolution: 6,
};
} else {
motionConfig.x = targetX;
motionConfig.y = targetY;
}
// Add motion animation to timeline
tl.to(sourceElement, motionConfig, 0);
// Add scale animation if enabled
if (config.scale) {
const scaleConfig: gsap.TweenVars = {
scale: config.scalePeak,
delay: config.scaleStartDelay,
duration: config.scaleUpDuration,
ease: config.scaleUpEase,
};
// Add shadow animation if enabled
if (config.enableShadow) {
const startBlur = config.baseShadowBlur * config.grabScale;
const maxBlur = config.baseShadowBlur * config.scalePeak * 0.5;
gsap.set(sourceElement, {
boxShadow: `0px 2px ${startBlur}px rgba(0,0,0,0.3)`
});
scaleConfig.boxShadow = `0px 2px ${maxBlur}px rgba(0,0,0,0.6)`;
}
// Scale up
tl.fromTo(
sourceElement,
{ scale: config.grabScale },
scaleConfig,
0
);
// Scale down
const scaleDownConfig: gsap.TweenVars = {
delay: config.scalePause,
scale: config.scaleTarget,
duration: config.scaleDownDuration,
ease: config.scaleDownEase,
};
if (config.enableShadow) {
scaleDownConfig.boxShadow = '0px 0px 0px rgba(0,0,0,0.0)';
}
tl.to(
sourceElement,
scaleDownConfig,
`>${config.scaleStartDelay + config.scaleUpDuration}`
);
} else {
// If scale animation is disabled, still set initial scale
gsap.set(sourceElement, { scale: config.grabScale });
}
// Track active animation
activeAnimationsRef.current.push(tl);
return tl;
}, []);
/**
* Kill all active animations created by this hook
*/
const killAll = useCallback(() => {
activeAnimationsRef.current.forEach(tl => tl.kill());
activeAnimationsRef.current = [];
}, []);
/**
* Kill a specific animation timeline
*/
const kill = useCallback((timeline: gsap.core.Timeline | null) => {
if (timeline) {
timeline.kill();
activeAnimationsRef.current = activeAnimationsRef.current.filter(tl => tl !== timeline);
}
}, []);
return {
flyToTarget,
killAll,
kill,
};
}