adventskalender/shared/js/glitter.js aktualisiert
This commit is contained in:
@@ -1,133 +1,132 @@
|
|||||||
// shared/js/door-open.js
|
// Bratonien Glitter Hover Effekt – für .door, .openfield und .fluegel
|
||||||
document.querySelectorAll(".door, .fluegel").forEach(elem => {
|
document.querySelectorAll(".door, .openfield, .fluegel").forEach(elem => {
|
||||||
elem.addEventListener("click", () => {
|
let canvas, ctx, particles = [], anim, hovering = false;
|
||||||
const day = elem.dataset.day;
|
let cursorX = 0, cursorY = 0;
|
||||||
|
|
||||||
// === EINZELTÜR ===
|
|
||||||
if (elem.classList.contains("door")) {
|
|
||||||
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;
|
const day = elem.dataset.day;
|
||||||
if (!hoverGroups[day]) hoverGroups[day] = [];
|
let partner = null;
|
||||||
|
let extraPartner = null; // neu: für das openfield bei flügeln
|
||||||
|
|
||||||
hoverGroups[day].push(elem);
|
if (elem.classList.contains("door")) {
|
||||||
});
|
// door -> openfield
|
||||||
|
partner = document.querySelector(`.openfield[data-day="${day}"]`);
|
||||||
|
} else if (elem.classList.contains("openfield")) {
|
||||||
|
// openfield -> door
|
||||||
|
partner = document.querySelector(`.door[data-day="${day}"]`);
|
||||||
|
} else if (elem.classList.contains("fluegel")) {
|
||||||
|
const isLeft = elem.classList.contains("left");
|
||||||
|
const isRight = elem.classList.contains("right");
|
||||||
|
|
||||||
Object.values(hoverGroups).forEach(group => {
|
// fluegel -> gegenüberliegender flügel
|
||||||
group.forEach(elem => {
|
if (isLeft) {
|
||||||
let canvas, ctx, particles = [], anim, hovering = false;
|
partner = document.querySelector(`.fluegel.right[data-day="${day}"]`);
|
||||||
let cursorX = 0, cursorY = 0;
|
} else if (isRight) {
|
||||||
|
partner = document.querySelector(`.fluegel.left[data-day="${day}"]`);
|
||||||
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 => {
|
// fluegel -> zugehöriges openfield (das hat vorher gefehlt!)
|
||||||
const rect = elem.getBoundingClientRect();
|
extraPartner = document.querySelector(`.openfield[data-day="${day}"]`);
|
||||||
cursorX = (e.clientX - rect.left) * 1.4 - (elem.offsetWidth * 0.2);
|
}
|
||||||
cursorY = (e.clientY - rect.top) * 1.4 - (elem.offsetHeight * 0.2);
|
|
||||||
});
|
|
||||||
|
|
||||||
function addParticles() {
|
function setupCanvas() {
|
||||||
const cx = cursorX || canvas.width / 2;
|
if (canvas) return;
|
||||||
const cy = cursorY || canvas.height / 2;
|
canvas = document.createElement("canvas");
|
||||||
for (let i = 0; i < 4; i++) {
|
canvas.width = elem.offsetWidth * 1.4;
|
||||||
const angle = Math.random() * Math.PI * 2;
|
canvas.height = elem.offsetHeight * 1.4;
|
||||||
const speed = Math.random() * 1.5 + 0.3;
|
canvas.style.position = "absolute";
|
||||||
particles.push({
|
canvas.style.top = "-20%";
|
||||||
x: cx,
|
canvas.style.left = "-20%";
|
||||||
y: cy,
|
canvas.style.pointerEvents = "none";
|
||||||
r: Math.random() * 1.3 + 0.2,
|
canvas.style.zIndex = "10";
|
||||||
alpha: Math.random() * 0.7 + 0.3,
|
elem.appendChild(canvas);
|
||||||
vx: Math.cos(angle) * speed,
|
ctx = canvas.getContext("2d");
|
||||||
vy: Math.sin(angle) * speed,
|
}
|
||||||
life: Math.random() * 120 + 60,
|
|
||||||
decay: Math.random() * 0.015 + 0.008
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function draw() {
|
// Cursor in Element
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
elem.addEventListener("mousemove", e => {
|
||||||
if (hovering) addParticles();
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
particles.forEach(p => {
|
function addParticles() {
|
||||||
ctx.beginPath();
|
const cx = cursorX || canvas.width / 2;
|
||||||
const grad = ctx.createRadialGradient(p.x, p.y, 0, p.x, p.y, p.r * 3);
|
const cy = cursorY || canvas.height / 2;
|
||||||
grad.addColorStop(0, `rgba(200, 170, 73, ${p.alpha})`);
|
for (let i = 0; i < 4; i++) {
|
||||||
grad.addColorStop(0.3, `rgba(255, 240, 200, ${p.alpha * 0.8})`);
|
const angle = Math.random() * Math.PI * 2;
|
||||||
grad.addColorStop(1, "transparent");
|
const speed = Math.random() * 1.5 + 0.3;
|
||||||
ctx.fillStyle = grad;
|
particles.push({
|
||||||
ctx.arc(p.x, p.y, p.r * 3, 0, Math.PI * 2);
|
x: cx,
|
||||||
ctx.fill();
|
y: cy,
|
||||||
|
r: Math.random() * 1.3 + 0.2,
|
||||||
p.x += p.vx;
|
alpha: Math.random() * 0.7 + 0.3,
|
||||||
p.y += p.vy + 0.05;
|
vx: Math.cos(angle) * speed,
|
||||||
p.vx *= 0.98;
|
vy: Math.sin(angle) * speed,
|
||||||
p.vy *= 0.98;
|
life: Math.random() * 120 + 60,
|
||||||
p.alpha -= p.decay;
|
decay: Math.random() * 0.015 + 0.008
|
||||||
p.life--;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
particles = particles.filter(p => p.life > 0 && p.alpha > 0);
|
|
||||||
anim = requestAnimationFrame(draw);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Hover start
|
function draw() {
|
||||||
elem.addEventListener("mouseenter", () => {
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
setupCanvas();
|
if (hovering) addParticles();
|
||||||
hovering = true;
|
|
||||||
cancelAnimationFrame(anim);
|
|
||||||
draw();
|
|
||||||
|
|
||||||
group.forEach(p => p.classList.add("hover-proxy"));
|
particles.forEach(p => {
|
||||||
|
ctx.beginPath();
|
||||||
|
const grad = ctx.createRadialGradient(p.x, p.y, 0, p.x, p.y, p.r * 3);
|
||||||
|
// Bratonien-Gold
|
||||||
|
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--;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Hover end
|
particles = particles.filter(p => p.life > 0 && p.alpha > 0);
|
||||||
elem.addEventListener("mouseleave", () => {
|
anim = requestAnimationFrame(draw);
|
||||||
hovering = false;
|
}
|
||||||
setTimeout(() => {
|
|
||||||
if (!hovering) {
|
|
||||||
cancelAnimationFrame(anim);
|
|
||||||
particles = [];
|
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
||||||
}
|
|
||||||
}, 300);
|
|
||||||
|
|
||||||
group.forEach(p => p.classList.remove("hover-proxy"));
|
// HOVER START
|
||||||
});
|
elem.addEventListener("mouseenter", () => {
|
||||||
|
setupCanvas();
|
||||||
|
hovering = true;
|
||||||
|
cancelAnimationFrame(anim);
|
||||||
|
draw();
|
||||||
|
|
||||||
|
// wichtigsten Teil: bestehenden Hover-Style beim Partner auslösen
|
||||||
|
if (partner) {
|
||||||
|
partner.classList.add("hover-proxy");
|
||||||
|
}
|
||||||
|
if (extraPartner) {
|
||||||
|
extraPartner.classList.add("hover-proxy");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// HOVER ENDE
|
||||||
|
elem.addEventListener("mouseleave", () => {
|
||||||
|
hovering = false;
|
||||||
|
setTimeout(() => {
|
||||||
|
if (!hovering) {
|
||||||
|
cancelAnimationFrame(anim);
|
||||||
|
particles = [];
|
||||||
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
|
}
|
||||||
|
}, 300);
|
||||||
|
|
||||||
|
if (partner) {
|
||||||
|
partner.classList.remove("hover-proxy");
|
||||||
|
}
|
||||||
|
if (extraPartner) {
|
||||||
|
extraPartner.classList.remove("hover-proxy");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user