Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update WebGL background animation for theme changes #6

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions script.js

This file was deleted.

2 changes: 2 additions & 0 deletions theme-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ function toggleTheme() {
if (currentTheme === 'dark') {
body.classList.replace('dark-theme', 'light-theme');
localStorage.setItem('theme', 'light');
document.dispatchEvent(new CustomEvent('themeChange', { detail: { theme: 'light' } }));
} else {
body.classList.replace('light-theme', 'dark-theme');
localStorage.setItem('theme', 'dark');
document.dispatchEvent(new CustomEvent('themeChange', { detail: { theme: 'dark' } }));
}
}

Expand Down
34 changes: 16 additions & 18 deletions webgl-animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,32 @@
let theme = 'light'; // Default theme

function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
noStroke();
createCanvas(windowWidth, windowHeight);
noFill();
}

function drawMorphicLines() {
stroke(theme === 'light' ? 0 : 255);
beginShape();
for (let i = 0; i < width; i += 10) {
let y = noise(i * 0.01, frameCount * 0.01) * height;
vertex(i, y);
}
endShape();
}

function draw() {
background(theme === 'light' ? 255 : 0);
let dirX = (mouseX / width - 0.5) * 2;
let dirY = (mouseY / height - 0.5) * 2;
directionalLight(250, 250, 250, -dirX, -dirY, -1);
translate(-1.5 * width / 4, 0, 0);
for (let i = 0; i < 3; i++) {
push();
translate(i * width / 4, 0, 0);
rotateY(frameCount * 0.02);
box(100);
pop();
}
drawMorphicLines();
}

// Listen for theme changes
document.addEventListener('themeChange', function(e) {
theme = e.detail.theme;
draw(); // Redraw with the new theme
});

// Create a depression effect on mouse click
// Remove the sphere effect on mouse click
function mousePressed() {
push();
translate(mouseX - width / 2, mouseY - height / 2);
sphere(20);
pop();
// This function intentionally left blank
}