Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
116 lines (102 loc) · 2.9 KB
/
Copy pathapp.js
File metadata and controls
116 lines (102 loc) · 2.9 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
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let particleArray = [];
const mouse = {
x: null,
y: null,
radius: 100,
};
window.addEventListener("mousemove", function (event) {
mouse.x = event.x;
mouse.y = event.y;
});
ctx.fillStyle = "white";
ctx.font = "30px Verdana";
ctx.fillText("GIORGI", 130, 75);
const textCoordinates = ctx.getImageData(0, 0, 300, 100);
const adjustX = canvas.width / 2 - textCoordinates.width * 5;
const adjustY = canvas.height / 2 - textCoordinates.height * 5;
class Particle {
constructor(x, y) {
this.x = x;
this.y = y;
this.size = 2.5;
this.baseX = this.x;
this.baseY = this.y;
this.density = Math.random() * 150 + 50;
}
draw() {
ctx.fillStyle = "white";
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.closePath();
ctx.fill();
}
update() {
let dx = mouse.x - this.x;
let dy = mouse.y - this.y;
let distance = Math.sqrt(dx * dx + dy * dy);
let forceDirectionX = dx / distance;
let forceDirectionY = dy / distance;
let maxDistance = mouse.radius;
let force = (maxDistance - distance) / maxDistance;
let directionX = forceDirectionX * force * this.density;
let directionY = forceDirectionY * force * this.density;
if (distance < mouse.radius) {
this.x -= directionX;
this.y -= directionY;
} else {
if (this.x !== this.baseX) {
let dx = this.x - this.baseX;
this.x -= dx / 10;
}
if (this.y !== this.baseY) {
let dy = this.y - this.baseY;
this.y -= dy / 10;
}
}
}
}
function init() {
particleArray = [];
for (let y = 0; y < textCoordinates.height; y++) {
for (let x = 0; x < textCoordinates.width; x++) {
let alpha = textCoordinates.data[y * 4 * textCoordinates.width + x * 4 + 3];
if (alpha > 128) {
let positionX = x * 8 + adjustX;
let positionY = y * 8 + adjustY;
particleArray.push(new Particle(positionX, positionY));
}
}
}
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < particleArray.length; i++) {
particleArray[i].draw();
particleArray[i].update();
}
connect();
requestAnimationFrame(animate);
}
function connect() {
for (let a = 0; a < particleArray.length; a++) {
for (let b = a; b < particleArray.length; b++) {
let dx = particleArray[a].x - particleArray[b].x;
let dy = particleArray[a].y - particleArray[b].y;
let distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 25) {
ctx.strokeStyle = "white";
ctx.lineWidth = 0.3;
ctx.beginPath();
ctx.moveTo(particleArray[a].x, particleArray[a].y);
ctx.lineTo(particleArray[b].x, particleArray[b].y);
ctx.stroke();
}
}
}
}
init();
animate();