-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
84 lines (69 loc) · 2.34 KB
/
script.js
File metadata and controls
84 lines (69 loc) · 2.34 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
document.addEventListener("DOMContentLoaded", () => {
const canvas = document.getElementById("animated-grid-canvas");
if (!canvas) {
console.error("Canvas element #animated-grid-canvas not found.");
return;
}
const ctx = canvas.getContext("2d");
// Grid Properties
const gridSize = 50;
const dotColor = "rgba(255, 255, 255, 0.1)";
const dotSize = 2; // Size of the dots at intersections
// Parallax Variables
let mouseX = 0;
let mouseY = 0;
let offsetX = 0;
let offsetY = 0;
const parallaxStrength = 0.05;
// Animation offset for subtle movement
let animationOffsetX = 0;
let animationOffsetY = 0;
// Resize Handler
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// No need to call drawGrid here, animate loop will handle it
}
// Drawing Function
function drawGrid() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = dotColor;
// Calculate effective offsets for parallax + animation
const currentParallaxX = offsetX * parallaxStrength;
const currentParallaxY = offsetY * parallaxStrength;
const totalOffsetX = currentParallaxX + animationOffsetX;
const totalOffsetY = currentParallaxY + animationOffsetY;
// Calculate grid starting positions with offsets
const startX = 0 - (totalOffsetX % gridSize);
const startY = 0 - (totalOffsetY % gridSize);
// Draw dots at intersections
for (let x = startX; x < canvas.width; x += gridSize) {
for (let y = startY; y < canvas.height; y += gridSize) {
ctx.beginPath();
ctx.arc(x, y, dotSize, 0, Math.PI * 2);
ctx.fill();
}
}
}
// Animation Loop
function animate() {
// Subtle independent animation
animationOffsetX += 0.05;
animationOffsetY -= 0.03;
drawGrid();
requestAnimationFrame(animate);
}
// Mouse Move Handler
function handleMouseMove(event) {
mouseX = event.clientX;
mouseY = event.clientY;
// Update parallax offsets based on mouse position relative to center of screen
offsetX = mouseX - window.innerWidth / 2;
offsetY = mouseY - window.innerHeight / 2;
}
// Initialization
resizeCanvas(); // Initial size
animate(); // Start animation loop
window.addEventListener("resize", resizeCanvas);
window.addEventListener("mousemove", handleMouseMove);
});