adventskalender/shared/js/glitter.js aktualisiert
This commit is contained in:
@@ -1,22 +1,46 @@
|
|||||||
// Bratonien Glitter Hover Effekt – final, voll bidirektional
|
// shared/js/door-open.js
|
||||||
document.addEventListener("DOMContentLoaded", () => {
|
document.querySelectorAll(".door, .fluegel").forEach(elem => {
|
||||||
|
elem.addEventListener("click", () => {
|
||||||
// === 1. Elemente nach Tagen gruppieren ===
|
|
||||||
const groups = {};
|
|
||||||
|
|
||||||
document.querySelectorAll(".door, .fluegel, .openfield").forEach(elem => {
|
|
||||||
const day = elem.dataset.day;
|
const day = elem.dataset.day;
|
||||||
if (!groups[day]) groups[day] = [];
|
|
||||||
groups[day].push(elem);
|
|
||||||
});
|
|
||||||
|
|
||||||
// === 2. Hoverlogik für jede Gruppe ===
|
// === EINZELTÜR ===
|
||||||
Object.values(groups).forEach(elems => {
|
if (elem.classList.contains("door")) {
|
||||||
elems.forEach(elem => {
|
if (elem.classList.contains("open")) return;
|
||||||
|
elem.classList.add("open");
|
||||||
|
|
||||||
|
const openfield = document.querySelector(`.openfield[data-day="${day}"]`);
|
||||||
|
if (openfield) openfield.classList.add("open");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// === FLÜGEL ===
|
||||||
|
if (elem.classList.contains("fluegel")) {
|
||||||
|
const left = document.querySelector(`.fluegel.left[data-day="${day}"]`);
|
||||||
|
const right = document.querySelector(`.fluegel.right[data-day="${day}"]`);
|
||||||
|
|
||||||
|
// bereits geöffnet?
|
||||||
|
if (left.classList.contains("rota") && right.classList.contains("rota")) return;
|
||||||
|
|
||||||
|
if (left) left.classList.add("rota");
|
||||||
|
if (right) right.classList.add("rota");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// === HOVER LOGIK FÜR DOOR, FLÜGEL UND OPENFIELD ===
|
||||||
|
const hoverGroups = {};
|
||||||
|
document.querySelectorAll(".door, .fluegel, .openfield").forEach(elem => {
|
||||||
|
const day = elem.dataset.day;
|
||||||
|
if (!hoverGroups[day]) hoverGroups[day] = [];
|
||||||
|
|
||||||
|
hoverGroups[day].push(elem);
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.values(hoverGroups).forEach(group => {
|
||||||
|
group.forEach(elem => {
|
||||||
let canvas, ctx, particles = [], anim, hovering = false;
|
let canvas, ctx, particles = [], anim, hovering = false;
|
||||||
let cursorX = 0, cursorY = 0;
|
let cursorX = 0, cursorY = 0;
|
||||||
|
|
||||||
// Canvas für Glitzereffekt
|
|
||||||
function setupCanvas() {
|
function setupCanvas() {
|
||||||
if (canvas) return;
|
if (canvas) return;
|
||||||
canvas = document.createElement("canvas");
|
canvas = document.createElement("canvas");
|
||||||
@@ -31,14 +55,12 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
ctx = canvas.getContext("2d");
|
ctx = canvas.getContext("2d");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cursor verfolgen (für Partikel)
|
|
||||||
elem.addEventListener("mousemove", e => {
|
elem.addEventListener("mousemove", e => {
|
||||||
const rect = elem.getBoundingClientRect();
|
const rect = elem.getBoundingClientRect();
|
||||||
cursorX = (e.clientX - rect.left) * 1.4 - (elem.offsetWidth * 0.2);
|
cursorX = (e.clientX - rect.left) * 1.4 - (elem.offsetWidth * 0.2);
|
||||||
cursorY = (e.clientY - rect.top) * 1.4 - (elem.offsetHeight * 0.2);
|
cursorY = (e.clientY - rect.top) * 1.4 - (elem.offsetHeight * 0.2);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Partikel hinzufügen
|
|
||||||
function addParticles() {
|
function addParticles() {
|
||||||
const cx = cursorX || canvas.width / 2;
|
const cx = cursorX || canvas.width / 2;
|
||||||
const cy = cursorY || canvas.height / 2;
|
const cy = cursorY || canvas.height / 2;
|
||||||
@@ -58,7 +80,6 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Zeichnen
|
|
||||||
function draw() {
|
function draw() {
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
if (hovering) addParticles();
|
if (hovering) addParticles();
|
||||||
@@ -85,30 +106,28 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
anim = requestAnimationFrame(draw);
|
anim = requestAnimationFrame(draw);
|
||||||
}
|
}
|
||||||
|
|
||||||
// === HOVER START ===
|
// Hover start
|
||||||
elem.addEventListener("mouseenter", () => {
|
elem.addEventListener("mouseenter", () => {
|
||||||
setupCanvas();
|
setupCanvas();
|
||||||
hovering = true;
|
hovering = true;
|
||||||
cancelAnimationFrame(anim);
|
cancelAnimationFrame(anim);
|
||||||
draw();
|
draw();
|
||||||
|
|
||||||
// Alle Elemente des gleichen Tages gleichzeitig hervorheben
|
group.forEach(p => p.classList.add("hover-proxy"));
|
||||||
elems.forEach(el => el.classList.add("hover-proxy"));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// === HOVER ENDE ===
|
// Hover end
|
||||||
elem.addEventListener("mouseleave", () => {
|
elem.addEventListener("mouseleave", () => {
|
||||||
hovering = false;
|
hovering = false;
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (!hovering) {
|
if (!hovering) {
|
||||||
cancelAnimationFrame(anim);
|
cancelAnimationFrame(anim);
|
||||||
particles = [];
|
particles = [];
|
||||||
if (ctx) ctx.clearRect(0, 0, canvas.width, canvas.height);
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
}
|
}
|
||||||
}, 300);
|
}, 300);
|
||||||
|
|
||||||
elems.forEach(el => el.classList.remove("hover-proxy"));
|
group.forEach(p => p.classList.remove("hover-proxy"));
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user