-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
144 lines (131 loc) · 4.3 KB
/
index.js
File metadata and controls
144 lines (131 loc) · 4.3 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
import React, { useState, useEffect } from "react";
import { Animated, View, Dimensions, Easing } from "react-native";
const Snowflake = (props) => {
const { width, height } = Dimensions.get("window");
let x = Math.round(Math.random() * width);
const [xAnim] = useState(new Animated.Value(x));
// Start off-screen (above the visible area) at a random offset so flakes aren't visible
const initialY = -Math.round(
Math.random() * (height + (props.size || 0) * 2)
);
const [yAnim] = useState(new Animated.Value(initialY));
// Rotation: random start angle, direction and slow duration
const startRotation = Math.round(Math.random() * 360);
const rotationDirection = Math.random() < 0.5 ? 1 : -1;
const rotationDuration = 20000 + Math.round(Math.random() * 20000); // 20-40s slow rotation
const rotationOutput = rotationDirection === 1 ? "360deg" : "-360deg";
const [rotationAnim] = useState(new Animated.Value(0));
const lifeTime = 20000;
const horizontalMoveDuringLifeTime = 6;
// Base delay spreads flakes evenly, then add a random jitter so they won't all start simultaneously
const baseDelay = (lifeTime / (props.total || 1)) * props.index;
const animationDelay = Math.floor(
baseDelay + Math.random() * (lifeTime / Math.max(1, props.total || 1))
);
// Compute a duration proportional to remaining travel distance so flakes that start higher take longer
const travelDistance = height + (props.size || 0) * 2 - initialY;
const duration = Math.max(
8000,
Math.round((travelDistance / (height + (props.size || 0) * 2)) * lifeTime)
);
// Set of animations for horizontal snowflake bouncing
const animations = [];
const maxHorizontalMove = width / 10;
for (let i = 0; i < horizontalMoveDuringLifeTime; i++) {
const horizontalMove = Math.round(Math.random() * maxHorizontalMove);
x += i % 2 === 0 ? horizontalMove : -horizontalMove;
animations.push(
Animated.timing(xAnim, {
toValue: x,
duration: lifeTime / horizontalMoveDuringLifeTime,
delay: i === 0 ? animationDelay : 0,
useNativeDriver: true,
})
);
}
useEffect(() => {
Animated.loop(
Animated.timing(yAnim, {
toValue: height + props.size * 2,
duration: duration,
easing: Easing.linear,
delay: animationDelay,
useNativeDriver: true,
})
).start();
Animated.loop(Animated.sequence(animations)).start();
// Slow continuous rotation with its own random speed/direction and start delay
Animated.loop(
Animated.timing(rotationAnim, {
toValue: 360,
duration: rotationDuration,
easing: Easing.linear,
delay: animationDelay,
useNativeDriver: true,
}),
{ iterations: -1 }
).start();
}, []);
return (
<Animated.Image
style={{
...props.style,
position: "absolute",
transform: [
{ translateX: xAnim },
{ translateY: yAnim },
// start at a random rotation and then slowly spin (direction randomized)
{ rotate: `${startRotation}deg` },
{
rotate: rotationAnim.interpolate({
inputRange: [0, 360],
outputRange: ["0deg", rotationOutput],
}),
},
],
height: props.size,
width: props.size,
opacity: 0.6,
}}
source={require("./snowflake.png")}
resizeMode="cover"
></Animated.Image>
);
};
const Snowflakes = (props) => {
if (props.snowOnlyAroundXmass) {
const currentDate = new Date();
if (
currentDate.getMonth() !== 11 || // 11 stands for December
currentDate.getDate() < 18
) {
return null;
}
}
const { width, height } = Dimensions.get("window");
const sizeOfSnowflakes =
props.sizeOfSnowflakes || Math.max(width, height) / 35;
const numberOfSnowflakes = props.numberOfSnowflakes || 10;
return (
<View
pointerEvents="none"
style={{
flex: 1,
position: "absolute",
width: "100%",
top: 0,
height: height + sizeOfSnowflakes,
}}
>
{[...Array(numberOfSnowflakes)].map((e, i) => (
<Snowflake
key={i}
index={i}
total={numberOfSnowflakes}
size={sizeOfSnowflakes}
/>
))}
</View>
);
};
export default Snowflakes;