-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
153 lines (136 loc) · 3.88 KB
/
Copy pathscript.js
File metadata and controls
153 lines (136 loc) · 3.88 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
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
(() => {
//Slideshow
const total = 16;
let index = 0; // set manually to your preferred starting video (0–15)
let front = document.getElementById('vid_F');
let back = document.getElementById('vid_B');
front.src = `assets/Side${index + 1}.mp4`;
front.load();
front.play();
function swap() {
index = (index + 1) % total; // or replace with any logic you prefer
back.src = `assets/Side${index + 1}.mp4`;
back.load();
back.play();
back.className = 'front';
front.className = 'back';
[front, back] = [back, front];
}
front.addEventListener('click', swap);
back.addEventListener('click', swap);
//Clock
function updateClock() {
const now = new Date();
const time = now.toLocaleTimeString('en-GB', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
});
const date = now.toLocaleDateString('en-GB', {
weekday: 'long',
day: '2-digit',
month: '2-digit',
year: 'numeric'
});
document.getElementById('codes').textContent = `${time} | ${date}`;
}
updateClock();
setInterval(updateClock, 1000);
//Canvas
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let particles = [];
const opts = {
particleColor: "rgb(143, 143, 143)",
lineColor: "rgb(143, 143, 143)",
particleAmount: 50,
defaultSpeed: 0.5,
variantSpeed: 0.5,
defaultRadius: 2,
variantRadius: 2,
linkRadius: 150,
};
//Spatial Hashing
const cellSize = opts.linkRadius;
function getCell(x, y) {
return [Math.floor(x / cellSize), Math.floor(y / cellSize)].join(",");
}
const resizeCanvas = () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
};
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
class Particle {
constructor() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.speed = opts.defaultSpeed + Math.random() * opts.variantSpeed;
this.direction = Math.random() * Math.PI * 2;
this.radius = opts.defaultRadius + Math.random() * opts.variantRadius;
this.vector = {
x: Math.cos(this.direction) * this.speed,
y: Math.sin(this.direction) * this.speed,
};
}
update() {
this.x += this.vector.x;
this.y += this.vector.y;
if (this.x <= 0 || this.x >= canvas.width) this.vector.x *= -1;
if (this.y <= 0 || this.y >= canvas.height) this.vector.y *= -1;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = opts.particleColor;
ctx.fill();
}
}
const createParticles = () => {
particles = Array.from({ length: opts.particleAmount }, () => new Particle());
};
const drawLines = () => {
// Build spatial hash
const hash = {};
particles.forEach((p, i) => {
const cell = getCell(p.x, p.y);
if (!hash[cell]) hash[cell] = [];
hash[cell].push(i);
});
// For each particle, only check neighbors in adjacent cells
particles.forEach((p1, i) => {
const [cx, cy] = [Math.floor(p1.x / cellSize), Math.floor(p1.y / cellSize)];
for (let dx = -1; dx <= 1; dx++) {
for (let dy = -1; dy <= 1; dy++) {
const cell = [cx + dx, cy + dy].join(",");
if (!hash[cell]) continue;
for (const j of hash[cell]) {
if (j <= i) continue; // Avoid double checking
const p2 = particles[j];
const distance = Math.hypot(p2.x - p1.x, p2.y - p1.y);
if (distance < opts.linkRadius) {
ctx.strokeStyle = `rgba(143, 143, 143, ${1 - distance / opts.linkRadius})`;
ctx.lineWidth = 0.5;
ctx.beginPath();
ctx.moveTo(p1.x, p1.y);
ctx.lineTo(p2.x, p2.y);
ctx.stroke();
}
}
}
}
});
};
const animate = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
particles.forEach((p) => {
p.update();
p.draw();
});
drawLines();
requestAnimationFrame(animate);
};
createParticles();
animate();
})();