-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
193 lines (168 loc) · 5.98 KB
/
index.js
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
"use strict"
let count = 0
function delay(ms) {
//delay without setTimeout
let start = Date.now()
while (Date.now() - start < ms) {
//do nothing
}
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //Максимум не включается, минимум включается
}
function reAnimate() {
//reset animation
let elem = document.getElementById("inner")
let style = elem.style.animation
elem.style.animation = "none"
elem.offsetHeight
elem.style.animation = style
}
function reposition() {
let element = document.getElementById("circle")
console.log("reposition!")
let translation = 35
let top = 50
let left = 50
top += getRandomInt(-translation, translation)
left += getRandomInt(-translation, translation)
console.log(`reposition: ${top} ${left}`)
element.style.top = `${top}%`
element.style.left = `${left}%`
}
function click() {
console.log("click!")
count += 1
cssVariablesSet()
reposition()
reAnimate()
document.getElementById("score").innerText = `${count}`
}
function events() {
let animated = document.getElementById("inner")
animated.addEventListener("animationiteration", () => {
animEnd()
/*element.classList.remove("animated")
setTimeout(function(){
element.classList.add("animated");
},1)
element.classList.add("animated")*/
})
let circle = document.getElementById("circle")
circle.addEventListener("click", () => {
click()
})
circle.addEventListener("click", pop)
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/MaterialCatToy/serviceWorker.js', {scope: "/MaterialCatToy/"})
.then(function (registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}).catch(function (err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
} else {
console.log("ServiceWorker not supported")
}
}
window.onload = events
function animEnd() {
reposition()
/*let element = document.getElementById("inner")
element.classList.remove("animated")
element.classList.add("animated")*/
}
function cssVariablesSet() {
let container = document.getElementById("var-container")
console.log(count)
let speed = 1 + (count / 100)
let transitionSpeed = 1 + (count / 25)
let circleInner = document.getElementById("inner")
console.log(`speed: ${5 / speed}s`)
circleInner.offsetHeight
circleInner.style.animationDuration = `${5 / speed}s`
circleInner.offsetHeight
let circle = document.getElementById("circle")
circle.offsetHeight
circle.style.transitionDuration = `${0.25 / transitionSpeed}s`
circle.offsetHeight
}
//popping animation
function pop(e) {
let amount = 30;
console.log(`type: ${e.target.dataset.type}`)
switch (e.target.dataset.type) {
case 'shadow':
case 'line':
amount = 60;
break;
}
if (e.clientX === 0 && e.clientY === 0) {
const bbox = e.target.getBoundingClientRect();
const x = bbox.left + bbox.width / 2;
const y = bbox.top + bbox.height / 2;
for (let i = 0; i < 30; i++) {
createParticle(x, y, e.target.dataset.type);
}
} else {
for (let i = 0; i < amount; i++) {
createParticle(e.clientX, e.clientY, e.target.dataset.type);
}
}
}
function createParticle(x, y, type) {
const particle = document.createElement('div');
particle.classList.add('particle');
document.body.appendChild(particle);
let width = Math.floor(Math.random() * 30 + 8);
let height = width;
let destinationX = (Math.random() - 0.5) * 500;
let destinationY = (Math.random() - 0.5) * 500;
let rotation = Math.random() * 520;
let delay = Math.random() * 200;
switch (type) {
case 'square':
particle.style.background = `hsl(${Math.random() * 50 + 200}, 70%, 60%)`; // Цвет квадратов
particle.style.border = '1px solid white'; // Бордюр квадратов
break;
case 'symbol':
particle.innerText = ['❤', "🐱", "🐈", "🐁"][Math.floor(Math.random() * 4)]; // Символы
particle.style.color = `hsl(${Math.random() * 50 + 200}, 70%, 60%)`; // Цвет символов
particle.style.fontSize = `${Math.random() * 24 + 2}px`; // Размер символов
width = height = 'auto';
break;
case 'shadow':
var color = `hsl(${Math.random() * 50 + 200}, 70%, 50%)`; // Цвет
particle.style.boxShadow = `0 0 ${Math.floor(Math.random() * 10 + 10)}px ${color}`; // Тень
particle.style.background = color;
particle.style.borderRadius = '50%'; // Радиус
width = height = Math.random() * 5 + 4; // Размеры
break;
}
particle.style.width = `${width}px`;
particle.style.height = `${height}px`;
const animation = particle.animate([
{
transform: `translate(-50%, -50%) translate(${x}px, ${y}px) rotate(0deg)`,
opacity: 1
},
{
transform: `translate(-50%, -50%) translate(${x + destinationX}px, ${y + destinationY}px) rotate(${rotation}deg)`,
opacity: 0
}
], {
duration: Math.random() * 1000 + 5000, // Продолжительность всех эффектов
easing: 'cubic-bezier(0, .9, .57, 1)',
delay: delay
});
animation.onfinish = removeParticle;
}
function removeParticle(e) {
e.srcElement.effect.target.remove();
}
if (document.body.animate) {
document.querySelectorAll('button').forEach(button => button.addEventListener('click', pop));
}