JavaScript
JAVASCRIPT Kopieren document.addEventListener("DOMContentLoaded", () => {
const canvas = document.createElement("canvas");
canvas.style.position = "fixed";
canvas.style.top = "0";
canvas.style.left = "0";
canvas.style.width = "100%";
canvas.style.height = "100%";
canvas.style.pointerEvents = "none";
canvas.style.zIndex = "9999";
document.body.appendChild(canvas);
const ctx = canvas.getContext("2d");
let width;
let height;
const particles = [];
const symbols = [
"🎃",
"🦇",
"👻",
"🕷️",
"🕸️",
"💀"
];
const particleCount = 40;
// Bildschirmgröße
function resize() {
width = canvas.width = window.innerWidth;
height = canvas.height = window.innerHeight;
}
window.addEventListener("resize", resize);
resize();
// Neues Halloween-Partikel
function createParticle(startAtTop = false) {
return {
x: Math.random() * width,
y: startAtTop
? -50
: Math.random() * height,
size: Math.random() * 20 + 15,
speed: Math.random() * 1.5 + 0.5,
drift: Math.random() * 1.5 - 0.75,
rotation: Math.random() * Math.PI * 2,
rotationSpeed:
(Math.random() - 0.5) * 0.04,
opacity:
Math.random() * 0.5 + 0.5,
symbol:
symbols[
Math.floor(
Math.random() * symbols.length
)
]
};
}
// Partikel erstellen
for (let i = 0; i < particleCount; i++) {
particles.push(createParticle());
}
// Partikel zeichnen
function drawParticle(particle) {
ctx.save();
ctx.globalAlpha = particle.opacity;
ctx.translate(
particle.x,
particle.y
);
ctx.rotate(
particle.rotation
);
ctx.font = `${particle.size}px serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(
particle.symbol,
0,
0
);
ctx.restore();
}
// Animation
function animate() {
ctx.clearRect(
0,
0,
width,
height
);
particles.forEach((particle) => {
// Fallen
particle.y += particle.speed;
// Leichte Bewegung nach links/rechts
particle.x += particle.drift;
// Drehen
particle.rotation +=
particle.rotationSpeed;
// Wenn unten angekommen
if (particle.y > height + 50) {
Object.assign(
particle,
createParticle(true)
);
}
// Seitenbegrenzung
if (particle.x < -50) {
particle.x = width + 50;
}
if (particle.x > width + 50) {
particle.x = -50;
}
drawParticle(particle);
});
requestAnimationFrame(animate);
}
// Animation starten
animate();
});
Effekt anpassen
Die wichtigsten Einstellungen findest du direkt am Anfang des JavaScripts:
JAVASCRIPT Kopieren /*
const symbols = ["🎃", "🦇", "👻", "🕷️", "🕸️"];
const particleCount = 35;
*/
Mehr Halloween-Elemente:
JAVASCRIPT Kopieren /*
const symbols = [
"🎃",
"🦇",
"👻",
"🕷️",
"🕸️",
"💀",
"☠️"
];
*/
Mehr bzw. weniger Partikel:
JAVASCRIPT Kopieren // const particleCount = 60;
Schnellerer Fall:
JAVASCRIPT Kopieren // speed: Math.random() * 3 + 1,
Der Effekt läuft über requestAnimationFrame() und benötigt keine externe JavaScript-Bibliothek .