-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
131 lines (107 loc) · 3.51 KB
/
script.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
const wrapper = document.getElementById("wrapper");
const wWidth = parseFloat(window.getComputedStyle(wrapper).width, 10)
const wHeight = parseFloat(window.getComputedStyle(wrapper).height, 10);
var globalZ = -10;
const colors = [
"rgb(102, 7, 8)",
"rgb(164, 22, 26)",
"rgb(186, 24, 27)",
"rgb(229, 56, 59)",
"rgb(177, 167, 166)",
"rgb(211, 211, 211)",
"rgb(245, 243, 244)",
];
// add listeners to all initial shapes
function setClick(){
var shapes = document.getElementsByClassName("shape");
for(let i = 0; i < shapes.length; i++){
shapes[i].addEventListener("click", handleClick);
}
}
function setColors(){
var shapes = document.getElementsByClassName("shape");
for(let i = 0; i < shapes.length; i++){
Object.assign(shapes[i].style, {backgroundColor: randColor()});
}
}
const nextTick = new Promise(res => queueMicrotask(() => setTimeout(res,0)));
async function handleClick(e){
const shape = e.target;
// make sure div is interactable
if(shape.getAttribute("data-interactable") != 'true') { return; }
// get style data for clicked shape
const style = window.getComputedStyle(shape);
const width = parseFloat(style.width, 10);
const height = parseFloat(style.height, 10);
const x = parseFloat(style.left, 10);
const y = parseFloat(style.top, 10);
// prevent one to be clicked while animation happening
shape.setAttribute("data-interactable", "false");
// if wider than tall
if(width > height){
shape.style.width = width/2 + "px";
newShapeDiv(
x + "px", y + "px", width + "px", height + "px",
x + (width / 2) + "px",
y + "px",
width / 2 + "px",
height + "px",
style.backgroundColor
);
// if taller than wide
}else{
shape.style.height = (height * 50 / wHeight)+ "%";
newShapeDiv(
x + "px", y + "px", width + "px", height + "px",
x + "px",
y + (height / 2) + "px",
width + "px",
height / 2 + "px",
style.backgroundColor
);
}
await new Promise(r => setTimeout(r, 750));
shape.setAttribute("data-interactable", "true");
}
function randColor(){
return colors[Math.floor(Math.random() * colors.length)];
}
async function newShapeDiv(px, py, pw, ph, x, y, w, h, col){
const div = document.createElement("div");
div.classList.add("shape");
div.setAttribute("data-transition", "spawn");
let newCol = col;
// generate new color
while(newCol == col){
newCol = randColor();
//console.log(`${newCol} ${col} ${newCol == col}`);
}
Object.assign(div.style, {
backgroundColor: newCol,
left: px,
top: py,
width: pw,
height :ph,
transition: "left, top, height, width, border-radius, transform",
transitionTimingFunction: "ease",
transitionDuration: "750ms",
zIndex: -1200
});
wrapper.append(div);
// wait a tick so animation happens
await new Promise(r => setTimeout(r, 0));
div.setAttribute("data-transition", "static");
Object.assign(div.style, {
left: x,
top: y,
width: w,
height: h,
});
// add clickevent to new shape
await new Promise(r => setTimeout(r, 750));
Object.assign(div.style, {zIndex: 1200});
div.setAttribute("data-interactable", "true");
div.addEventListener("click", handleClick);
}
setClick();
setColors();