-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
107 lines (95 loc) · 2.55 KB
/
main.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
/* To apply this animation to your website, paste the following into your HTML code:
<iframe src="https://codepen.io/tommyho/full/oNQpZmV" width=500 height=500></iframe>
*/
/*
Sources:
https://www.youtube.com/watch?v=aEptSB3fbqM
https://codepen.io/gvissing
Revised by: [email protected]
*/
/* --- System Parameters (Recommended)--- */
/* Also try
2, 3, 4, 0.1, 20 -> Sparks
2, 6, 5, 0.1, 8 -> Spray
1, 10, 20, 0.02, 300 -> Spider web
*/
let bNum = 2; // Number of bubbles created on movement (2)
let bSize = 8; // Bubble size (8)
let bSpeed = 3; // Bubble speed (3)
let bDep = 0.01; // Bubble depletion speed (0.01)
let bDist = 40; // Mucus length (40)
/* --- Main Program: DO NOT EDIT BELOW --- */
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let spots = [];
let hue = 0;
const mouse = {
x: undefined,
y: undefined
}
canvas.addEventListener("mousemove", function(event){
mouse.x = event.x;
mouse.y = event.y;
for (let i = 0; i < bNum; i++) {
spots.push(new Particle());
}
})
window.addEventListener("resize", function() {
canvas.width = innerWidth;
canvas.height = innerHeight;
init();
})
class Particle {
constructor() {
this.x = mouse.x;
this.y = mouse.y;
this.size = Math.random() * bSize + 0.1;
this.speedX = Math.random() * bSpeed - bSpeed/2;
this.speedY = Math.random() * bSpeed - bSpeed/2;
this.color = "hsl(" + hue + ", 100%, 50%)";
}
update() {
this.x += this.speedX;
this.y += this.speedY;
if (this.size > bDep) this.size -= bDep;
}
draw() {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
}
}
function handleParticle() {
for (let i = 0; i < spots.length; i++) {
spots[i].update();
spots[i].draw();
for (let j = i; j < spots.length; j++) {
const dx = spots[i].x - spots[j].x;
const dy = spots[i].y - spots[j].y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < bDist) {
ctx.beginPath();
ctx.strokeStyle = spots[i].color;
ctx.lineWidth = spots[i].size / 3;
ctx.moveTo(spots[i].x, spots[i].y);
ctx.lineTo(spots[j].x, spots[j].y);
//ctx.bezierCurveTo(spots[j].x, spots[j].y, spots[j].x, spots[i].y, spots[j].x, spots[j].y);
ctx.stroke();
}
}
if (spots[i].size <= bDep) {
spots.splice(i, 1);
i--;
}
}
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
handleParticle();
hue++;
requestAnimationFrame(animate);
}
animate()