Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
151 lines (126 loc) · 3.64 KB
/
Copy pathscript.js
File metadata and controls
151 lines (126 loc) · 3.64 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
const canvas = document.querySelector("#graph-bg");
const ctx = canvas.getContext("2d");
const themeToggle = document.querySelector(".theme-toggle");
const scrollSpeed = 0.2;
const pointSpacing = 38;
let width = 0;
let height = 0;
let pixelRatio = 1;
let frame = 0;
let animationId = 0;
let points = [];
let nextPointX = 0;
const wave = {
base: 0.52 + Math.random() * 0.12,
amplitude: 0.12 + Math.random() * 0.06,
frequency: 0.008 + Math.random() * 0.005,
secondaryFrequency: 0.0035 + Math.random() * 0.003,
phase: Math.random() * Math.PI * 2,
secondaryPhase: Math.random() * Math.PI * 2,
};
function readColor(name) {
return getComputedStyle(document.documentElement).getPropertyValue(name).trim();
}
function setTheme(theme, persist = true) {
document.documentElement.dataset.theme = theme;
if (persist) {
try {
localStorage.setItem("theme", theme);
} catch {
// Storage can be unavailable in strict privacy modes.
}
}
themeToggle.setAttribute("aria-label", `Switch to ${theme === "dark" ? "light" : "dark"} theme`);
themeToggle.querySelector(".theme-toggle__text").textContent = theme === "dark" ? "Light" : "Dark";
}
function resizeCanvas() {
pixelRatio = Math.min(window.devicePixelRatio || 1, 2);
width = window.innerWidth;
height = window.innerHeight;
canvas.width = Math.floor(width * pixelRatio);
canvas.height = Math.floor(height * pixelRatio);
canvas.style.width = `${width}px`;
canvas.style.height = `${height}px`;
ctx.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
points = [];
nextPointX = Math.floor((frame * scrollSpeed) / pointSpacing) * pointSpacing;
}
function drawGrid() {
const step = 48;
ctx.strokeStyle = readColor("--grid");
ctx.lineWidth = 1;
for (let x = 0; x <= width; x += step) {
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, height);
ctx.stroke();
}
for (let y = 0; y <= height; y += step) {
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(width, y);
ctx.stroke();
}
}
function drawFunction(scroll) {
ctx.beginPath();
for (let x = -20; x <= width + 20; x += 4) {
const y = curveY(x + scroll);
if (x === -20) {
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
}
}
ctx.strokeStyle = readColor("--graph");
ctx.lineWidth = 2;
ctx.stroke();
}
function curveY(x) {
const amplitude = Math.min(120, height * wave.amplitude);
return (
height * wave.base +
Math.sin(x * wave.frequency + wave.phase) * amplitude * 0.72 +
Math.cos(x * wave.secondaryFrequency + wave.secondaryPhase) * amplitude * 0.36
);
}
function drawPoints(scroll) {
while (nextPointX < scroll + width + pointSpacing) {
points.push(nextPointX);
nextPointX += pointSpacing;
}
while (points.length > 0 && points[0] < scroll - pointSpacing) {
points.shift();
}
for (const pointX of points) {
const x = pointX - scroll;
const y = curveY(pointX);
ctx.beginPath();
ctx.arc(x, y, 2.2, 0, Math.PI * 2);
ctx.fillStyle = readColor("--point");
ctx.fill();
}
}
function render() {
const scroll = frame * scrollSpeed;
ctx.clearRect(0, 0, width, height);
drawGrid();
drawFunction(scroll);
drawPoints(scroll);
frame += 1;
animationId = window.requestAnimationFrame(render);
}
resizeCanvas();
setTheme(document.documentElement.dataset.theme, false);
render();
window.addEventListener("resize", () => {
resizeCanvas();
window.cancelAnimationFrame(animationId);
render();
});
themeToggle.addEventListener("click", () => {
const nextTheme = document.documentElement.dataset.theme === "dark" ? "light" : "dark";
setTheme(nextTheme);
window.cancelAnimationFrame(animationId);
render();
});