forked from itsfuad/Animation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanimation.ts
116 lines (112 loc) · 4.53 KB
/
animation.ts
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
/**
* Animates the specified HTML element to fade in.
* @param {HTMLElement} elem - The element to animate
* @param {number} durationMs - The duration of the animation in milliseconds
*/
export function fadeIn(elem: HTMLElement, durationMs: number) {
elem.style.opacity = "0";
let startTime: number = 0;
const display = elem.style.display;
elem.style.display = 'flex';
const animate = (currentTime: number) => {
if (!startTime) startTime = currentTime;
const elapsedTime = currentTime - startTime;
const progress = Math.min(elapsedTime / durationMs, 1);
elem.style.opacity = progress.toString();
if (progress < 1) {
requestAnimationFrame(animate);
}
if (progress === 1){
elem.style.display = display;
}
};
requestAnimationFrame(animate);
}
/**
* Animates the specified HTML element to fade out.
* @param {HTMLElement} elem - The element to animate
* @param {number} durationMs - The duration of the animation in milliseconds
*/
export function fadeOut(elem: HTMLElement, durationMs: number) {
elem.style.opacity = "1";
let startTime: number = 0;
const display = elem.style.display;
elem.style.display = 'flex';
const animate = (currentTime: number) => {
if (!startTime) startTime = currentTime;
const elapsedTime = currentTime - startTime;
const progress = Math.min(elapsedTime / durationMs, 1);
elem.style.opacity = (1 - progress).toString();
if (progress < 1) {
requestAnimationFrame(animate);
}
if (progress === 1){
elem.style.display = display;
}
};
requestAnimationFrame(animate);
}
/**
* Animates the specified HTML element to fly in from a given position.
* @param {HTMLElement} elem - The HTML element to animate.
* @param {{ x: number, y: number }} position - The position from which the element will fly in, specified as an object with 'x' and 'y' coordinates.
* @param {number} duration - The duration of the animation in milliseconds.
* @param {boolean} fade - Indicates whether to apply fade effect during the animation.
*/
export function flyIn(elem: HTMLElement, position: { x: number, y: number }, duration: number, fade: boolean) {
position.x = position.x || 0;
position.y = position.y || 0;
const display = elem.style.display;
elem.style.display = 'flex';
elem.style.transform = `translate(${position.x}px, ${position.y}px)`;
elem.style.opacity = "0";
let startTime: number = 0;
const animate = (currentTime: number) => {
if (!startTime) startTime = currentTime;
const elapsedTime = currentTime - startTime;
const progress = Math.min(elapsedTime / duration, 1);
elem.style.transform = `translate(${position.x * (1 - progress)}px, ${position.y * (1 - progress)}px)`;
if (fade){
elem.style.opacity = progress.toString();
}
if (progress < 1) {
requestAnimationFrame(animate);
}
if (progress === 1){
elem.style.display = display;
}
};
requestAnimationFrame(animate);
}
/**
* Animates the specified HTML element to fly out to a given position.
* @param {HTMLElement} elem - The HTML element to animate.
* @param {{ x: number, y: number }} position - The position to which the element will fly out, specified as an object with 'x' and 'y' coordinates.
* @param {number} duration - The duration of the animation in milliseconds.
* @param {boolean} fade - Indicates whether to apply fade effect during the animation.
*/
export function flyOut(elem: HTMLElement, position: { x: number, y: number }, duration: number, fade: boolean) {
position.x = position.x || 0;
position.y = position.y || 0;
const display = elem.style.display;
elem.style.display = 'flex';
elem.style.transform = `translate(${position.x}px, ${position.y}px)`;
elem.style.opacity = "1";
let startTime: number = 0;
const animate = (currentTime: number) => {
if (!startTime) startTime = currentTime;
const elapsedTime = currentTime - startTime;
const progress = Math.min(elapsedTime / duration, 1);
elem.style.transform = `translate(${position.x * progress}px, ${position.y * progress}px)`;
if (fade){
elem.style.opacity = (1 - progress).toString();
}
if (progress < 1) {
requestAnimationFrame(animate);
}
if (progress === 1){
elem.style.display = display;
}
};
requestAnimationFrame(animate);
}