-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
212 lines (172 loc) · 5.83 KB
/
main.js
File metadata and controls
212 lines (172 loc) · 5.83 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
function getPostTimestamp(post) {
const source = post.publishedAt || post.date;
const parsed = new Date(source);
if (Number.isNaN(parsed.getTime())) return 0;
return parsed.getTime();
}
function normalizePosts(posts) {
return [...posts].sort((a, b) => getPostTimestamp(b) - getPostTimestamp(a));
}
function getPostLocale(post) {
if (typeof post.lang !== "string") return "en-US";
return post.lang.toLowerCase().startsWith("it") ? "it-IT" : "en-US";
}
function formatPostDate(post) {
const timestamp = getPostTimestamp(post);
if (!timestamp) return "Unknown publication date";
return new Intl.DateTimeFormat(getPostLocale(post), {
dateStyle: "medium",
timeStyle: "short",
}).format(new Date(timestamp));
}
// Home page: render latest posts from JSON.
async function renderLatestPosts() {
const latestPostsContainer = document.getElementById("latest-posts");
if (!latestPostsContainer) return;
const renderIcons = () => {
if (window.lucide) window.lucide.createIcons();
};
try {
const response = await fetch("./data/posts.json");
if (!response.ok) throw new Error("Unable to read posts.json");
const posts = normalizePosts(await response.json());
const latest = posts.slice(0, 3);
if (!latest.length) {
latestPostsContainer.innerHTML = `
<article class="card">
<h3>No articles available</h3>
<p class="muted">New posts will be published soon.</p>
</article>
`;
renderIcons();
return;
}
latestPostsContainer.innerHTML = latest
.map(
(post) => `
<article class="card">
<h3>${post.title}</h3>
<p class="muted card-meta">
<i data-lucide="calendar-days" class="icon icon-inline icon-violet" aria-hidden="true"></i>
<span>${formatPostDate(post)}</span>
</p>
<p>${post.description}</p>
<div class="tag-list">
<span class="tag">${(post.lang || "en").toUpperCase()}</span>
</div>
<a class="inline-link" href="blog/post.html?id=${post.id}">
<i data-lucide="arrow-right" class="icon icon-inline icon-accent" aria-hidden="true"></i>
<span>Read article</span>
</a>
</article>
`
)
.join("");
renderIcons();
} catch (error) {
latestPostsContainer.innerHTML = `
<article class="card">
<h3>Loading error</h3>
<p class="muted">I cannot load articles right now.</p>
</article>
`;
renderIcons();
console.error(error);
}
}
// Matrix-style animated background generated in canvas.
function initMatrixCard() {
const canvas = document.getElementById("matrix-canvas");
if (!canvas) return;
const context = canvas.getContext("2d");
if (!context) return;
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
const glyphs = ["0", "1"];
let dpr = window.devicePixelRatio || 1;
let width = 0;
let height = 0;
let fontSize = 14;
let columns = [];
let rafId = 0;
let lastFrame = 0;
function resetCanvasSize() {
const rect = canvas.getBoundingClientRect();
width = Math.max(1, Math.floor(rect.width));
height = Math.max(1, Math.floor(rect.height));
dpr = window.devicePixelRatio || 1;
canvas.width = Math.floor(width * dpr);
canvas.height = Math.floor(height * dpr);
context.setTransform(dpr, 0, 0, dpr, 0, 0);
context.textBaseline = "top";
fontSize = Math.max(13, Math.floor(width / 82));
const columnCount = Math.ceil(width / fontSize);
columns = Array.from({ length: columnCount }, () => Math.random() * -25);
context.fillStyle = "#020603";
context.fillRect(0, 0, width, height);
}
function drawStaticFrame() {
context.fillStyle = "#020603";
context.fillRect(0, 0, width, height);
context.font = `${fontSize}px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace`;
for (let x = 0; x < width; x += fontSize) {
for (let y = 0; y < height; y += fontSize) {
if (Math.random() > 0.72) {
context.fillStyle = Math.random() > 0.9 ? "#9dffb0" : "#2ddf55";
context.fillText(glyphs[Math.floor(Math.random() * 2)], x, y);
}
}
}
}
function drawFrame(timestamp) {
if (timestamp - lastFrame < 45) {
rafId = window.requestAnimationFrame(drawFrame);
return;
}
lastFrame = timestamp;
context.fillStyle = "rgba(2, 6, 3, 0.2)";
context.fillRect(0, 0, width, height);
context.font = `${fontSize}px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace`;
for (let index = 0; index < columns.length; index += 1) {
const x = index * fontSize;
const y = columns[index] * fontSize;
const isHighlight = Math.random() > 0.92;
context.fillStyle = isHighlight ? "#b2ffc4" : "#2ddf55";
context.fillText(glyphs[Math.floor(Math.random() * 2)], x, y);
if (y > height + Math.random() * height * 0.35) {
columns[index] = Math.random() * -30;
}
columns[index] += isHighlight ? 1.35 : 1;
}
rafId = window.requestAnimationFrame(drawFrame);
}
function start() {
window.cancelAnimationFrame(rafId);
if (reduceMotion.matches) {
drawStaticFrame();
return;
}
lastFrame = 0;
rafId = window.requestAnimationFrame(drawFrame);
}
function handleVisibility() {
if (document.hidden) {
window.cancelAnimationFrame(rafId);
return;
}
start();
}
resetCanvasSize();
start();
window.addEventListener("resize", () => {
resetCanvasSize();
start();
});
if (typeof reduceMotion.addEventListener === "function") {
reduceMotion.addEventListener("change", start);
}
document.addEventListener("visibilitychange", handleVisibility);
}
document.addEventListener("DOMContentLoaded", () => {
renderLatestPosts();
initMatrixCard();
});