Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
64 lines (52 loc) · 1.81 KB
/
Copy pathscript.js
File metadata and controls
64 lines (52 loc) · 1.81 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
console.log('script loaded');
const canvas = document.getElementById('bg-canvas');
const ctx = canvas.getContext('2d');
const card = document.getElementById('card');
const light = document.querySelector('.mouse-light');
function resize() {
canvas.width = innerWidth;
canvas.height = innerHeight;
}
addEventListener('resize', resize);
resize();
const pts = [...Array(90)].map(() => ({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
vx: (Math.random() - .5) * .5,
vy: (Math.random() - .5) * .5
}));
(function anim() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (const p of pts) {
p.x += p.vx;
p.y += p.vy;
if (p.x < 0 || p.x > canvas.width) p.vx *= -1;
if (p.y < 0 || p.y > canvas.height) p.vy *= -1;
ctx.fillStyle = '#67e8ff';
ctx.beginPath();
ctx.arc(p.x, p.y, 2, 0, 6.28);
ctx.fill();
}
for (let i = 0; i < pts.length; i++) {
for (let j = i + 1; j < pts.length; j++) {
let a = pts[i], b = pts[j];
let d = Math.hypot(a.x - b.x, a.y - b.y);
if (d < 130) {
ctx.strokeStyle = 'rgba(103,232,255,' + ((130 - d) / 130 * .18) + ')';
ctx.beginPath();
ctx.moveTo(a.x, a.y);
ctx.lineTo(b.x, b.y);
ctx.stroke();
}
}
}
requestAnimationFrame(anim);
})();
document.addEventListener('mousemove', e => {
light.style.setProperty('--x', e.clientX + 'px');
light.style.setProperty('--y', e.clientY + 'px');
const rx = (.5 - e.clientY / innerHeight) * 10;
const ry = (e.clientX / innerWidth - .5) * 10;
card.style.transform =
`perspective(1000px) rotateX(${rx}deg) rotateY(${ry}deg)`;
});