forked from vaghasiyautsav/LabInventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
165 lines (130 loc) · 4.23 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
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
(function() {
var ctx= new AudioContext();
function createSound(size,fr, delay, type, vol) {
for(let i=0; i < size; i++) {
let osc= ctx.createOscillator(),
gain= ctx.createGain();
setTimeout(function() {
osc.frequency.value= fr*i;
gain.gain.value= vol;
osc.type= type;
osc.connect(gain);
gain.connect(ctx.destination);
osc.start();
setTimeout(function() {
console.log(osc.frequency.value);
let gVal= gain.gain.value,
smooth;
function reduceGain() {
gVal-=0.02;
if(gVal > 0) {
smooth= requestAnimationFrame(reduceGain);
} else {
osc.stop();
cancelAnimationFrame(smooth);
}
gain.gain.value= gVal/7;
}
reduceGain();
}, delay);
cir.classList.toggle("pop");
}, i*delay);
}
}
function getRandomColor() {
var colors= ["#456", "#890", "#634", "#299", "tomato", "#fb3"],
idx= Math.floor(colors.length*Math.random());
return (colors[idx]);
}
function animateIt(el, dur, delay) {
var animateEl= el.animate([
{
opacity: 0,
transform: "translate(-50%, -50%) scale(0)"
},
{
opacity: 1,
transform: "translate(-50%, -50%) scale(1)"
},
{
opacity: 0,
transform: "translate(-50%, -50%) scale(1.1)"
}
],
{
duration: dur,
easing: "ease-out",
fill: "forwards",
delay: delay || 0
});
return animateEl;
}
function createBubble() {
var ns= "http://www.w3.org/2000/svg",
bubble= document.createElement("div"),
bubbleDummy= document.createElement("div"),
heart= document.createElementNS(ns, "svg"),
heartPath= document.createElementNS(ns, "path");
heart.setAttribute("viewBox", "0 0 600 500")
heartPath.setAttribute("d", "M300,150 C500,10 600,300 300,400 C0,300 100,10 300,150");
bubble.classList.add("bubble");
bubble.style.color= getRandomColor();
bubbleDummy.classList.add("bubble-dummy");
heart.classList.add("heart");
heart.appendChild(heartPath);
bubble.appendChild(bubbleDummy);
bubble.appendChild(heart);
document.body.appendChild(bubble);
return {
setPosition: function(x,y) {
bubble.style.left= x +"px";
bubble.style.top= y + "px";
},
_animate: function() {
var animateBubble= animateIt(bubbleDummy, 1200),
animateHeart= animateIt(heart, 2000);
console.log(animateBubble)
return {
bubbleDur: 1200,
heartDur: 2000
}
},
remove: function(el) {
bubble.remove();
}
}
}
document.body.addEventListener("click", handleDown, false);
document.body.addEventListener("touchstart", handleDown, false);
function handleDown(e) {
var _x= e.pageX,
_y= e.pageY;
var bubble= createBubble();
bubble.setPosition(_x, _y);
var animation= bubble._animate(),
totalDelay= animation.bubbleDur+ animation.heartDur;
if(e.type) {
createSound(20,5000,1,"sawtooth",1);
}
setTimeout(()=> {
bubble.remove();
console.log("removed");
}, totalDelay);
console.log(animation);
}
var w= document.body.clientWidth,
h= document.body.clientHeight;
function bubbleUp() {
var de= {
pageX: Math.random()*w,
pageY: Math.random()*h
}
handleDown(de);
bblUp= setTimeout(bubbleUp, 200);
}
bubbleUp();
window.addEventListener("resize", function() {
w= document.body.clientWidth,
h= document.body.clientHeight;
}, false);
})();