forked from renproject/bridge-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
animationUtils.ts
127 lines (118 loc) · 2.97 KB
/
animationUtils.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
117
118
119
120
121
122
123
124
125
126
127
import { alpha } from "@material-ui/core";
import { makeStyles } from "@material-ui/core/styles";
import { black } from "./colors";
export const getShadow = (
color = black,
fadeLevel = 0.15,
x = 1,
y = 1,
blur = 2,
spread = 0
) => `${x}px ${y}px ${blur}px ${spread}px ${alpha(color, fadeLevel)}`;
export const getKeyframesName = (animationName: string) =>
`@keyframes ${animationName}`;
export const getStandardAnimation = (animationName: string) =>
`$${animationName} 2s infinite`;
export const createPulseAnimation = (
color: string,
spread = 5,
animationName = "pulse"
) => {
const initialShadow = getShadow(color, 0.5, 0, 0, 0, 0);
const throughShadow = getShadow(color, 0, 0, 0, 0, spread);
const toShadow = getShadow(color, 0, 0, 0, 0, 0);
const keyframesName = getKeyframesName(animationName);
const pulsingKeyframes = {
[keyframesName]: {
"0%": {
boxShadow: initialShadow,
},
"70%": {
boxShadow: throughShadow,
},
"100%": {
boxShadow: toShadow,
},
},
};
const pulsingStyles = {
boxShadow: initialShadow,
animation: getStandardAnimation(animationName),
};
return { pulsingKeyframes, pulsingStyles, keyframesName };
};
export const createPulseOpacityAnimation = (animationName = "pulseOpacity") => {
const keyframesName = getKeyframesName(animationName);
const initialOpacity = 0.6;
const pulsingKeyframes = {
[keyframesName]: {
"0%": {
opacity: initialOpacity,
},
"70%": {
opacity: 0.2,
},
"100%": {
opacity: initialOpacity,
},
},
};
const pulsingStyles = {
opacity: initialOpacity,
animation: getStandardAnimation(animationName),
};
return { pulsingKeyframes, pulsingStyles, keyframesName };
};
export const getTranslate3dX = (pixels = 0) => {
return `translate3d(${pixels}px, 0, 0)`;
};
export const getRotate = (degrees = 0) => {
return `rotate(${degrees}deg)`;
};
export const createShakeAnimation = (
magnitude = 1,
animationName = "shake"
) => {
const keyframesName = getKeyframesName(animationName);
const shakeKeyframesStyles = {
"0%": {
transform: getRotate(),
},
"20%": {
transform: getRotate(magnitude),
},
"40%": {
transform: getRotate(-2 * magnitude),
},
"60%": {
transform: getRotate(2 * magnitude),
},
"80%": {
transform: getRotate(-magnitude),
},
"100": {
transform: getRotate(),
},
};
const shakeKeyframes = {
[keyframesName]: shakeKeyframesStyles,
};
const shakeStyles = {
animation: `$${animationName} 0.2s ease-in-out infinite`,
transformOrigin: "50% 50%",
transform: getRotate(0),
};
return {
shakeKeyframes,
shakeStyles,
keyframesName,
shakeKeyframesStyles,
};
};
export const useShakingStyles = makeStyles(() => {
const { shakeKeyframes, shakeStyles } = createShakeAnimation(1);
return {
...shakeKeyframes,
shaking: shakeStyles,
};
});