-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
144 lines (121 loc) · 4.73 KB
/
Copy pathscript.js
File metadata and controls
144 lines (121 loc) · 4.73 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
/*
Javascript portfolio script
Author: Dan Shan
Created: 2025-02-23
Updated: 2026-06-07
*/
function toggleDarkMode() {
document.body.classList.toggle("dark-mode");
// Save the state in localStorage
if (document.body.classList.contains("dark-mode")) {
localStorage.setItem("darkMode", "enabled");
} else {
localStorage.setItem("darkMode", "disabled");
}
}
document.addEventListener("DOMContentLoaded", () => {
/* Apply dark mode by default unless disabled */
if (localStorage.getItem("darkMode") !== "disabled") {
document.body.classList.add("dark-mode");
}
/* View count */
const slug = 'index';
const viewCountElement = document.getElementById('view-count');
// Fetch the view count from the API
fetch(`/api/view?slug=${slug}`)
.then(res => res.json())
.then(data => {
if (viewCountElement) {
viewCountElement.textContent = `${data.count}`; // Display view count
}
})
.catch(err => {
console.error("Error fetching view count:", err);
});
const dmojSolved = document.getElementById('dmoj-solved-count');
const dmojRank = document.getElementById('dmoj-rank');
const dmojPoints = document.getElementById('dmoj-points');
const dmojRating = document.getElementById('dmoj-rating');
const dmojEndpoint = '/api/dmoj';
async function loadDmojStats() {
console.log('DMOJ fetch starting:', dmojEndpoint);
try {
const res = await fetch(dmojEndpoint);
console.log('DMOJ proxy status:', res.status, res.statusText);
const json = await res.json();
console.log('DMOJ proxy JSON:', json);
console.log('DMOJ data source:', json._source, json._error ? `(Error: ${json._error})` : '');
const user = json?.data?.object ?? json?.object;
if (!user) {
console.error('Unexpected DMOJ response shape:', json);
throw new Error('Missing data.data.object in DMOJ proxy response');
}
dmojSolved.textContent = user.problem_count ?? '--';
dmojRank.textContent = user.rank ?? '--';
dmojPoints.textContent = user.points != null ? Number(user.points).toFixed(1) : '--';
dmojRating.textContent = user.rating ?? '--';
console.log('DMOJ stats populated successfully', json._source === 'live' ? '(LIVE)' : '(CACHED)');
} catch (err) {
console.error('DMOJ proxy fetch failed:', err);
['dmoj-solved-count', 'dmoj-rank', 'dmoj-points', 'dmoj-rating'].forEach(id => {
const el = document.getElementById(id);
if (el) el.textContent = 'N/A';
});
}
}
loadDmojStats();
/* Snow Effect */
const canvas = document.getElementById("snowCanvas");
if (!canvas) {
console.error("Canvas element not found.");
return; // Stop execution if the canvas doesn't exist
}
const ctx = canvas.getContext("2d");
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
resizeCanvas(); // Set initial canvas size
window.addEventListener("resize", resizeCanvas); // Adjust canvas when window resizes
let snowflakes = [];
function createSnowflakes() {
snowflakes = []; // Clear existing flakes
for (let i = 0; i < 100; i++) {
snowflakes.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
radius: Math.random() * 5 + 1,
speed: Math.random() * 3 + 1
});
}
}
function drawSnowflakes() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowflakes.forEach((flake) => {
// Create a radial gradient for each snowflake
let gradient = ctx.createRadialGradient(flake.x, flake.y, 0, flake.x, flake.y, flake.radius);
gradient.addColorStop(0, "rgb(99, 205, 250)"); // Blue center
gradient.addColorStop(1, "rgb(255, 255, 255)"); // White edges
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
ctx.fill();
});
moveSnowflakes();
}
function moveSnowflakes() {
snowflakes.forEach((flake) => {
flake.y += flake.speed;
if (flake.y > canvas.height) {
flake.y = 0;
flake.x = Math.random() * canvas.width;
}
});
}
function animateSnow() {
drawSnowflakes();
requestAnimationFrame(animateSnow);
}
createSnowflakes();
animateSnow();
});