Skip to content

Commit 7df5b9a

Browse files
Bikram GoleBikram Gole
authored andcommitted
Optimize Liquid Glass performance
1 parent b1e05c5 commit 7df5b9a

2 files changed

Lines changed: 125 additions & 3 deletions

File tree

script.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,28 @@ revealElements.forEach((el) => revealObserver.observe(el));
1919
const canvas = document.getElementById("starfield");
2020
const ctx = canvas?.getContext("2d");
2121
let stars = [];
22+
let lastStarFrame = 0;
23+
24+
function getStarFieldProfile() {
25+
const isLiquidGlass = document.body.dataset.theme === "liquidglass";
26+
const isConstrained = window.matchMedia?.("(max-width: 820px)")?.matches
27+
|| window.matchMedia?.("(pointer: coarse)")?.matches
28+
|| false;
29+
30+
if (isLiquidGlass && isConstrained) return { density: 0.42, frameBudget: 56 };
31+
if (isLiquidGlass) return { density: 0.55, frameBudget: 46 };
32+
if (isConstrained) return { density: 0.72, frameBudget: 28 };
33+
return { density: 1, frameBudget: 18 };
34+
}
2235

2336
function resizeCanvas() {
2437
if (!canvas || !ctx) return;
2538
canvas.width = window.innerWidth;
2639
canvas.height = window.innerHeight;
2740

28-
const count = Math.min(140, Math.floor((canvas.width * canvas.height) / 17500));
41+
const profile = getStarFieldProfile();
42+
const baseCount = Math.min(140, Math.floor((canvas.width * canvas.height) / 17500));
43+
const count = Math.max(24, Math.floor(baseCount * profile.density));
2944
stars = Array.from({ length: count }, () => ({
3045
x: Math.random() * canvas.width,
3146
y: Math.random() * canvas.height,
@@ -35,12 +50,27 @@ function resizeCanvas() {
3550
}));
3651
}
3752

38-
function drawStars() {
53+
function drawStars(timestamp = 0) {
3954
if (!canvas || !ctx) return;
55+
const profile = getStarFieldProfile();
56+
const elapsed = timestamp - lastStarFrame;
57+
58+
if (document.hidden) {
59+
requestAnimationFrame(drawStars);
60+
return;
61+
}
62+
63+
if (elapsed < profile.frameBudget) {
64+
requestAnimationFrame(drawStars);
65+
return;
66+
}
67+
68+
const frameFactor = Math.min(3, Math.max(0.9, elapsed / 16.67));
69+
lastStarFrame = timestamp;
4070
ctx.clearRect(0, 0, canvas.width, canvas.height);
4171

4272
stars.forEach((star) => {
43-
star.y += star.speed;
73+
star.y += star.speed * frameFactor;
4474
if (star.y > canvas.height + 4) {
4575
star.y = -4;
4676
star.x = Math.random() * canvas.width;
@@ -68,6 +98,10 @@ tiltElements.forEach((card) => {
6898
};
6999

70100
card.addEventListener("mousemove", (event) => {
101+
if (document.body.dataset.theme === "liquidglass") {
102+
reset();
103+
return;
104+
}
71105
const rect = card.getBoundingClientRect();
72106
const dx = (event.clientX - rect.left) / rect.width - 0.5;
73107
const dy = (event.clientY - rect.top) / rect.height - 0.5;
@@ -700,6 +734,7 @@ function applyTheme(theme, notify = false) {
700734

701735
setThemeInUrl(selected);
702736
updateInternalLinks();
737+
resizeCanvas();
703738
if (notify) showToast(`Theme changed: ${selected}`);
704739
}
705740

@@ -1707,6 +1742,10 @@ async function loadRepos() {
17071742
card.style.removeProperty("--my");
17081743
};
17091744
card.addEventListener("mousemove", (event) => {
1745+
if (document.body.dataset.theme === "liquidglass") {
1746+
reset();
1747+
return;
1748+
}
17101749
const rect = card.getBoundingClientRect();
17111750
const dx = (event.clientX - rect.left) / rect.width - 0.5;
17121751
const dy = (event.clientY - rect.top) / rect.height - 0.5;

styles.css

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,6 +2405,89 @@ body[data-theme="liquidglass"] .site-footer {
24052405
padding: 0.52rem 0.8rem;
24062406
}
24072407

2408+
/* Performance pass: keep Liquid Glass style, reduce GPU-heavy compositing. */
2409+
body[data-theme="liquidglass"] .hero::before,
2410+
body[data-theme="liquidglass"] .panel::before,
2411+
body[data-theme="liquidglass"] .chaos-card::before,
2412+
body[data-theme="liquidglass"] .favorite-card::before,
2413+
body[data-theme="liquidglass"] .fact-card::before,
2414+
body[data-theme="liquidglass"] .glass-card::before,
2415+
body[data-theme="liquidglass"] .repo-card::before,
2416+
body[data-theme="liquidglass"] .contact-item::before,
2417+
body[data-theme="liquidglass"] .mini-terminal::before,
2418+
body[data-theme="liquidglass"] .quiz-board::before,
2419+
body[data-theme="liquidglass"] .command-shell::before,
2420+
body[data-theme="liquidglass"] .hero::after,
2421+
body[data-theme="liquidglass"] .panel::after {
2422+
content: none;
2423+
}
2424+
2425+
body[data-theme="liquidglass"] .site-header,
2426+
body[data-theme="liquidglass"] .hero,
2427+
body[data-theme="liquidglass"] .panel,
2428+
body[data-theme="liquidglass"] .command-shell,
2429+
body[data-theme="liquidglass"] .mini-terminal,
2430+
body[data-theme="liquidglass"] .mini-terminal-screen,
2431+
body[data-theme="liquidglass"] .quiz-board,
2432+
body[data-theme="liquidglass"] .terminal,
2433+
body[data-theme="liquidglass"] .chaos-card,
2434+
body[data-theme="liquidglass"] .favorite-card,
2435+
body[data-theme="liquidglass"] .fact-card,
2436+
body[data-theme="liquidglass"] .glass-card,
2437+
body[data-theme="liquidglass"] .repo-card,
2438+
body[data-theme="liquidglass"] .contact-item,
2439+
body[data-theme="liquidglass"] .btn,
2440+
body[data-theme="liquidglass"] .egg-btn,
2441+
body[data-theme="liquidglass"] .theme-top-select,
2442+
body[data-theme="liquidglass"] .theme-cycle-btn,
2443+
body[data-theme="liquidglass"] .easter-toast {
2444+
backdrop-filter: none;
2445+
}
2446+
2447+
body[data-theme="liquidglass"] .site-header {
2448+
box-shadow: 0 10px 24px rgba(2, 10, 28, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.12);
2449+
}
2450+
2451+
body[data-theme="liquidglass"] .hero,
2452+
body[data-theme="liquidglass"] .panel,
2453+
body[data-theme="liquidglass"] .command-shell,
2454+
body[data-theme="liquidglass"] .mini-terminal,
2455+
body[data-theme="liquidglass"] .quiz-board,
2456+
body[data-theme="liquidglass"] .chaos-card,
2457+
body[data-theme="liquidglass"] .favorite-card,
2458+
body[data-theme="liquidglass"] .fact-card,
2459+
body[data-theme="liquidglass"] .glass-card,
2460+
body[data-theme="liquidglass"] .repo-card,
2461+
body[data-theme="liquidglass"] .contact-item {
2462+
box-shadow: 0 8px 20px rgba(2, 10, 28, 0.24), inset 0 1px 0 rgba(255, 255, 255, 0.1);
2463+
}
2464+
2465+
body[data-theme="liquidglass"] .hero:hover,
2466+
body[data-theme="liquidglass"] .panel:hover,
2467+
body[data-theme="liquidglass"] .favorite-card:hover,
2468+
body[data-theme="liquidglass"] .fact-card:hover,
2469+
body[data-theme="liquidglass"] .glass-card:hover,
2470+
body[data-theme="liquidglass"] .repo-card:hover,
2471+
body[data-theme="liquidglass"] .contact-item:hover,
2472+
body[data-theme="liquidglass"] .chaos-card:hover {
2473+
box-shadow: 0 10px 22px rgba(5, 18, 41, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.12);
2474+
}
2475+
2476+
body[data-theme="liquidglass"] .favorite-card:hover,
2477+
body[data-theme="liquidglass"] .fact-card:hover,
2478+
body[data-theme="liquidglass"] .glass-card:hover,
2479+
body[data-theme="liquidglass"] .repo-card:hover,
2480+
body[data-theme="liquidglass"] .contact-item:hover,
2481+
body[data-theme="liquidglass"] .chaos-card:hover {
2482+
transform: translateY(-1px);
2483+
}
2484+
2485+
body[data-theme="liquidglass"] .chips span,
2486+
body[data-theme="liquidglass"] .theme-top-select,
2487+
body[data-theme="liquidglass"] .theme-cycle-btn {
2488+
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2);
2489+
}
2490+
24082491
.easter-toast.show {
24092492
opacity: 1;
24102493
transform: translateX(-50%) translateY(0);

0 commit comments

Comments
 (0)