-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnow.js
More file actions
147 lines (134 loc) · 3.52 KB
/
Copy pathsnow.js
File metadata and controls
147 lines (134 loc) · 3.52 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
// settings for snow
const _settings = {
maxSnowCount: 200, // max snow count
paintDelay: 500, // paint delay for resize event
screenSize: 2560, // screen size denominator
};
/**
* init snow elements
*/
function init() {
if (_snowRootElement) {
_snowRootElement = document.getElementById('snow-root');
} else {
_snowRootElement = document.createElement('div');
_snowRootElement.id = 'snow-root';
document.body.appendChild(_snowRootElement);
}
let initHtml = '';
let initCss = '';
for (let i = 1; i <= _settings.maxSnowCount; ++i) {
initHtml += '<i class="snow-item show"></i>';
const randomX = getRandom(0, 1000000) * 0.0001,
randomO = getRandom(-100000, 100000) * 0.0001,
randomT = (getRandom(3, 8) * 10).toFixed(2),
randomS = (getRandom(0, 10000) * 0.0001).toFixed(2),
randomOpacity = (getRandom(1, 10000) * 0.0001).toFixed(2);
initCss += `.snow-item:nth-child(${i}) {
opacity: ${randomOpacity};
transform: translate(${randomX.toFixed(2)}vw,-10px) scale(${randomS});
animation: fall-${i} ${getRandom(10, 30)}s -${getRandom(0, 30)}s linear infinite;
}
@keyframes fall-${i} {
0% {
opacity: 0;
}
${randomT}% {
transform: translate(${(randomX + randomO).toFixed(2)}vw, ${randomT}vh) scale(${randomS});
opacity: 0.${randomT};
}
to {
transform: translate(${(randomX + randomO / 2).toFixed(2)}vw, 105vh) scale(${randomS});
opacity: ${randomOpacity};
}
}`;
}
_snowRootElement.innerHTML = `
<style>
#snow-root {
position:fixed;
left:0;
top:0;
bottom:0;
width:100vw;
height:100vh;
overflow:hidden;
z-index:9999999;
pointer-events:none
}
.snow-item {
position: absolute;
width: 10px;
height: 10px;
background: white;
border-radius: 50%;
margin-top:-10px;
}
.snow-item.hide {
visibility: hidden;
}
.snow-item.show {
visibility: visible;
}
${initCss}
</style>
${initHtml}`;
paintSnow();
}
/**
* paintSnow
* painting snows not layout
* debounce applied
*/
const paintSnow = debounce(() => {
let snowCount = Math.round(_settings.maxSnowCount * (document.documentElement.clientWidth / _settings.screenSize));
if (snowCount > _settings.maxSnowCount) {
snowCount = _settings.maxSnowCount;
}
const showSnows = _snowRootElement.querySelectorAll('.snow-item.show');
const hideSnows = _snowRootElement.querySelectorAll('.snow-item.hide');
const updateCount = showSnows.length - snowCount;
const count = Math.abs(updateCount);
for (let i = 0; i < count; ++i) {
if (updateCount < 0) {
hideSnows[i].classList.remove('hide');
hideSnows[i].classList.add('show');
} else {
showSnows[i].classList.remove('show');
showSnows[i].classList.add('hide');
}
}
}, _settings.paintDelay);
/**
* getRandom
* get random number
* @param {*} min min
* @param {*} max max
* @returns number
*/
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
/**
* debounce
* @param {*} cb debounced callback
* @param {*} delay delay milliseconds
* @returns callback result
*/
function debounce(cb, delay = 250) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => {
cb(...args);
}, delay);
};
}
let _snowRootElement = document.getElementById('snow-root');
// init snow and event
if (!_snowRootElement) {
init();
addEventListener('resize', () => {
paintSnow();
});
}