adventskalender/shared/js/glitter.js aktualisiert
This commit is contained in:
@@ -1,124 +1,114 @@
|
|||||||
// Bratonien Glitter Hover Effekt – für .door, .openfield und .fluegel
|
// Bratonien Glitter Hover Effekt – strikt nach data-day gruppiert
|
||||||
document.querySelectorAll(".door, .openfield, .fluegel").forEach(elem => {
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
let canvas, ctx, particles = [], anim, hovering = false;
|
|
||||||
let cursorX = 0, cursorY = 0;
|
|
||||||
|
|
||||||
const day = elem.dataset.day;
|
// Alle relevanten Elemente einsammeln
|
||||||
let partners = [];
|
const elems = document.querySelectorAll(".door, .openfield, .fluegel");
|
||||||
|
|
||||||
// === Beziehungstabelle ===
|
// nach Tag gruppieren
|
||||||
if (elem.classList.contains("door")) {
|
const groupsByDay = {};
|
||||||
partners.push(document.querySelector(`.openfield[data-day="${day}"]`));
|
elems.forEach(el => {
|
||||||
}
|
const day = el.dataset.day;
|
||||||
|
if (!groupsByDay[day]) {
|
||||||
if (elem.classList.contains("openfield")) {
|
groupsByDay[day] = [];
|
||||||
partners.push(document.querySelector(`.door[data-day="${day}"]`));
|
|
||||||
partners.push(document.querySelector(`.fluegel.left[data-day="${day}"]`));
|
|
||||||
partners.push(document.querySelector(`.fluegel.right[data-day="${day}"]`));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (elem.classList.contains("fluegel")) {
|
|
||||||
const isLeft = elem.classList.contains("left");
|
|
||||||
const isRight = elem.classList.contains("right");
|
|
||||||
|
|
||||||
if (isLeft)
|
|
||||||
partners.push(document.querySelector(`.fluegel.right[data-day="${day}"]`));
|
|
||||||
if (isRight)
|
|
||||||
partners.push(document.querySelector(`.fluegel.left[data-day="${day}"]`));
|
|
||||||
|
|
||||||
// jeder Flügel verbindet sich zusätzlich mit dem openfield
|
|
||||||
partners.push(document.querySelector(`.openfield[data-day="${day}"]`));
|
|
||||||
}
|
|
||||||
|
|
||||||
partners = partners.filter(Boolean); // ungültige raus
|
|
||||||
|
|
||||||
// === Canvas für Glitzer ===
|
|
||||||
function setupCanvas() {
|
|
||||||
if (canvas) return;
|
|
||||||
canvas = document.createElement("canvas");
|
|
||||||
canvas.width = elem.offsetWidth * 1.4;
|
|
||||||
canvas.height = elem.offsetHeight * 1.4;
|
|
||||||
canvas.style.position = "absolute";
|
|
||||||
canvas.style.top = "-20%";
|
|
||||||
canvas.style.left = "-20%";
|
|
||||||
canvas.style.pointerEvents = "none";
|
|
||||||
canvas.style.zIndex = "10";
|
|
||||||
elem.appendChild(canvas);
|
|
||||||
ctx = canvas.getContext("2d");
|
|
||||||
}
|
|
||||||
|
|
||||||
elem.addEventListener("mousemove", e => {
|
|
||||||
const rect = elem.getBoundingClientRect();
|
|
||||||
cursorX = (e.clientX - rect.left) * 1.4 - (elem.offsetWidth * 0.2);
|
|
||||||
cursorY = (e.clientY - rect.top) * 1.4 - (elem.offsetHeight * 0.2);
|
|
||||||
});
|
|
||||||
|
|
||||||
function addParticles() {
|
|
||||||
const cx = cursorX || canvas.width / 2;
|
|
||||||
const cy = cursorY || canvas.height / 2;
|
|
||||||
for (let i = 0; i < 4; i++) {
|
|
||||||
const angle = Math.random() * Math.PI * 2;
|
|
||||||
const speed = Math.random() * 1.5 + 0.3;
|
|
||||||
particles.push({
|
|
||||||
x: cx,
|
|
||||||
y: cy,
|
|
||||||
r: Math.random() * 1.3 + 0.2,
|
|
||||||
alpha: Math.random() * 0.7 + 0.3,
|
|
||||||
vx: Math.cos(angle) * speed,
|
|
||||||
vy: Math.sin(angle) * speed,
|
|
||||||
life: Math.random() * 120 + 60,
|
|
||||||
decay: Math.random() * 0.015 + 0.008
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
groupsByDay[day].push(el);
|
||||||
|
|
||||||
function draw() {
|
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
||||||
if (hovering) addParticles();
|
|
||||||
|
|
||||||
particles.forEach(p => {
|
|
||||||
ctx.beginPath();
|
|
||||||
const grad = ctx.createRadialGradient(p.x, p.y, 0, p.x, p.y, p.r * 3);
|
|
||||||
grad.addColorStop(0, `rgba(200,170,73,${p.alpha})`);
|
|
||||||
grad.addColorStop(0.3, `rgba(255,240,200,${p.alpha * 0.8})`);
|
|
||||||
grad.addColorStop(1, "transparent");
|
|
||||||
ctx.fillStyle = grad;
|
|
||||||
ctx.arc(p.x, p.y, p.r * 3, 0, Math.PI * 2);
|
|
||||||
ctx.fill();
|
|
||||||
|
|
||||||
p.x += p.vx;
|
|
||||||
p.y += p.vy + 0.05;
|
|
||||||
p.vx *= 0.98;
|
|
||||||
p.vy *= 0.98;
|
|
||||||
p.alpha -= p.decay;
|
|
||||||
p.life--;
|
|
||||||
});
|
|
||||||
|
|
||||||
particles = particles.filter(p => p.life > 0 && p.alpha > 0);
|
|
||||||
anim = requestAnimationFrame(draw);
|
|
||||||
}
|
|
||||||
|
|
||||||
// === Hover Start ===
|
|
||||||
elem.addEventListener("mouseenter", () => {
|
|
||||||
setupCanvas();
|
|
||||||
hovering = true;
|
|
||||||
cancelAnimationFrame(anim);
|
|
||||||
draw();
|
|
||||||
|
|
||||||
partners.forEach(p => p.classList.add("hover-proxy"));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// === Hover Ende ===
|
// für jede Tagesgruppe Hover + Glitzer setzen
|
||||||
elem.addEventListener("mouseleave", () => {
|
Object.values(groupsByDay).forEach(group => {
|
||||||
hovering = false;
|
group.forEach(elem => {
|
||||||
setTimeout(() => {
|
let canvas, ctx, particles = [], anim, hovering = false;
|
||||||
if (!hovering) {
|
let cursorX = 0, cursorY = 0;
|
||||||
cancelAnimationFrame(anim);
|
|
||||||
particles = [];
|
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
||||||
}
|
|
||||||
}, 300);
|
|
||||||
|
|
||||||
partners.forEach(p => p.classList.remove("hover-proxy"));
|
function setupCanvas() {
|
||||||
|
if (canvas) return;
|
||||||
|
canvas = document.createElement("canvas");
|
||||||
|
canvas.width = elem.offsetWidth * 1.4;
|
||||||
|
canvas.height = elem.offsetHeight * 1.4;
|
||||||
|
canvas.style.position = "absolute";
|
||||||
|
canvas.style.top = "-20%";
|
||||||
|
canvas.style.left = "-20%";
|
||||||
|
canvas.style.pointerEvents = "none";
|
||||||
|
canvas.style.zIndex = "10";
|
||||||
|
elem.appendChild(canvas);
|
||||||
|
ctx = canvas.getContext("2d");
|
||||||
|
}
|
||||||
|
|
||||||
|
elem.addEventListener("mousemove", e => {
|
||||||
|
const rect = elem.getBoundingClientRect();
|
||||||
|
cursorX = (e.clientX - rect.left) * 1.4 - (elem.offsetWidth * 0.2);
|
||||||
|
cursorY = (e.clientY - rect.top) * 1.4 - (elem.offsetHeight * 0.2);
|
||||||
|
});
|
||||||
|
|
||||||
|
function addParticles() {
|
||||||
|
const cx = cursorX || canvas.width / 2;
|
||||||
|
const cy = cursorY || canvas.height / 2;
|
||||||
|
for (let i = 0; i < 4; i++) {
|
||||||
|
const angle = Math.random() * Math.PI * 2;
|
||||||
|
const speed = Math.random() * 1.5 + 0.3;
|
||||||
|
particles.push({
|
||||||
|
x: cx,
|
||||||
|
y: cy,
|
||||||
|
r: Math.random() * 1.3 + 0.2,
|
||||||
|
alpha: Math.random() * 0.7 + 0.3,
|
||||||
|
vx: Math.cos(angle) * speed,
|
||||||
|
vy: Math.sin(angle) * speed,
|
||||||
|
life: Math.random() * 120 + 60,
|
||||||
|
decay: Math.random() * 0.015 + 0.008
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function draw() {
|
||||||
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
|
if (hovering) addParticles();
|
||||||
|
|
||||||
|
particles.forEach(p => {
|
||||||
|
ctx.beginPath();
|
||||||
|
const grad = ctx.createRadialGradient(p.x, p.y, 0, p.x, p.y, p.r * 3);
|
||||||
|
grad.addColorStop(0, `rgba(200, 170, 73, ${p.alpha})`);
|
||||||
|
grad.addColorStop(0.3, `rgba(255, 240, 200, ${p.alpha * 0.8})`);
|
||||||
|
grad.addColorStop(1, "transparent");
|
||||||
|
ctx.fillStyle = grad;
|
||||||
|
ctx.arc(p.x, p.y, p.r * 3, 0, Math.PI * 2);
|
||||||
|
ctx.fill();
|
||||||
|
|
||||||
|
p.x += p.vx;
|
||||||
|
p.y += p.vy + 0.05;
|
||||||
|
p.vx *= 0.98;
|
||||||
|
p.vy *= 0.98;
|
||||||
|
p.alpha -= p.decay;
|
||||||
|
p.life--;
|
||||||
|
});
|
||||||
|
|
||||||
|
particles = particles.filter(p => p.life > 0 && p.alpha > 0);
|
||||||
|
anim = requestAnimationFrame(draw);
|
||||||
|
}
|
||||||
|
|
||||||
|
// HOVER START
|
||||||
|
elem.addEventListener("mouseenter", () => {
|
||||||
|
setupCanvas();
|
||||||
|
hovering = true;
|
||||||
|
cancelAnimationFrame(anim);
|
||||||
|
draw();
|
||||||
|
|
||||||
|
// WICHTIG: alle Elemente dieses Tages bekommen hover-proxy
|
||||||
|
group.forEach(p => p.classList.add("hover-proxy"));
|
||||||
|
});
|
||||||
|
|
||||||
|
// HOVER ENDE
|
||||||
|
elem.addEventListener("mouseleave", () => {
|
||||||
|
hovering = false;
|
||||||
|
setTimeout(() => {
|
||||||
|
if (!hovering) {
|
||||||
|
cancelAnimationFrame(anim);
|
||||||
|
particles = [];
|
||||||
|
if (ctx) ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
|
}
|
||||||
|
}, 300);
|
||||||
|
|
||||||
|
group.forEach(p => p.classList.remove("hover-proxy"));
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user