<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>QwQ</title>
<link rel="stylesheet" href="css/style.css">
<style>
body {
margin: 0;
background: black;
overflow: hidden;
}
canvas {
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<canvas id="background"></canvas>
<canvas id="animation"></canvas>
<canvas id="text-overlay"></canvas>
<script>
// HELPER FUNCTIONS
const lerp = (a, b, t) => Math.abs(b - a) > 0.1 ? a + t * (b - a) : b;
function getRandomTarget(targets, textWidth, fontSize, canvasWidth, canvasHeight) {
if (targets.length > 0) {
const idx = Math.floor(Math.random() * targets.length);
const { x, y } = targets.splice(idx, 1)[0];
return {
x: x + canvasWidth / 2 - textWidth / 2,
y: y + canvasHeight / 2 - fontSize / 2
};
}
}
// CLASSES
class Shard {
constructor(x, y, hue, target) {
this.x = x;
this.y = y;
this.hue = hue;
this.lightness = 50;
this.size = 15 + Math.random() * 10;
this.angle = Math.random() * 2 * Math.PI;
this.blastSpeed = 1 + Math.random() * 6;
this.xSpeed = Math.cos(this.angle) * this.blastSpeed;
this.ySpeed = Math.sin(this.angle) * this.blastSpeed;
this.target = target;
this.ttl = 100;
this.timer = 0;
}
draw(ctx) {
ctx.fillStyle = `hsl(${this.hue}, 100%, ${this.lightness}%)`;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
ctx.fill();
}
update() {
if (this.target) {
const dx = this.target.x - this.x;
const dy = this.target.y - this.y;
const dist = Math.sqrt(dx * dx + dy * dy);
const angle = Math.atan2(dy, dx);
const tx = Math.cos(angle) * 5;
const ty = Math.sin(angle) * 5;
this.size = lerp(this.size, 1.5, 0.05);
if (dist < 5) {
this.lightness = lerp(this.lightness, 100, 0.01);
this.xSpeed = this.ySpeed = 0;
this.x = lerp(this.x, this.target.x, 0.05);
this.y = lerp(this.y, this.target.y, 0.05);
this.timer += 1;
} else if (dist < 10) {
this.lightness = lerp(this.lightness, 100, 0.01);
this.xSpeed = lerp(this.xSpeed, tx, 0.1);
this.ySpeed = lerp(this.ySpeed, ty, 0.1);
this.timer += 1;
} else {
this.xSpeed = lerp(this.xSpeed, tx, 0.02);
this.ySpeed = lerp(this.ySpeed, ty, 0.02);
}
} else {
this.ySpeed += 0.05;
this.size = lerp(this.size, 1, 0.05);
if (this.y > c2.height) {
shards.splice(shards.indexOf(this), 1);
}
}
this.x += this.xSpeed;
this.y += this.ySpeed;
}
}
class Rocket {
constructor(canvasWidth, canvasHeight) {
this.x = canvasWidth / 4 + Math.random() * (canvasWidth / 2);
this.y = canvasHeight - 15;
this.angle = Math.random() * Math.PI / 4 - Math.PI / 6;
this.blastSpeed = 6 + Math.random() * 7;
this.xSpeed = Math.sin(this.angle) * this.blastSpeed;
this.ySpeed = -Math.cos(this.angle) * this.blastSpeed;
this.hue = Math.floor(Math.random() * 360);
}
draw(ctx) {
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(Math.atan2(this.ySpeed, this.xSpeed) + Math.PI / 2);
ctx.fillStyle = `hsl(${this.hue}, 100%, 50%)`;
ctx.fillRect(0, 0, 5, 15);
ctx.restore();
}
update() {
this.x += this.xSpeed;
this.y += this.ySpeed;
this.ySpeed += 0.1;
}
explode(shards) {
for (let i = 0; i < 70; i++) {
shards.push(new Shard(this.x, this.y, this.hue, getRandomTarget(targets, textWidth, fontSize, c2.width, c2.height)));
}
}
}
// INITIALIZATION
const [c1, c2, c3] = [document.getElementById('background'), document.getElementById('animation'), document.getElementById('text-overlay')];
const [ctx1, ctx2, ctx3] = [c1.getContext('2d'), c2.getContext('2d'), c3.getContext('2d')];
let fontSize = 200;
const rockets = [];
const shards = [];
const targets = [];
const fidelity = 3;
let counter = 0;
c2.width = c3.width = window.innerWidth;
c2.height = c3.height = window.innerHeight;
const text = ' QwQ! ';
let textWidth = Infinity;
// Calculate text width and prepare canvas for the text overlay
while (textWidth > window.innerWidth) {
ctx1.font = `900 ${fontSize--}px Arial`;
textWidth = ctx1.measureText(text).width;
}
c1.width = textWidth;
c1.height = fontSize * 1.5;
ctx1.font = `900 ${fontSize}px Arial`;
ctx1.fillText(text, 0, fontSize);
const imgData = ctx1.getImageData(0, 0, c1.width, c1.height);
for (let i = 0, max = imgData.data.length; i < max; i += 4) {
const alpha = imgData.data[i + 3];
const x = Math.floor(i / 4) % imgData.width;
const y = Math.floor(i / 4 / imgData.width);
if (alpha && x % fidelity === 0 && y % fidelity === 0) {
targets.push({ x, y });
}
}
ctx3.fillStyle = '#FFF';
ctx3.shadowColor = '#FFF';
ctx3.shadowBlur = 25;
// ANIMATION LOOP
(function loop() {
ctx2.fillStyle = "rgba(0, 0, 0, .1)";
ctx2.fillRect(0, 0, c2.width, c2.height);
counter += 1;
if (counter % 15 === 0) {
rockets.push(new Rocket(c2.width, c2.height));
}
rockets.forEach((r, i) => {
r.draw(ctx2);
r.update();
if (r.ySpeed > 0) {
r.explode(shards);
rockets.splice(i, 1);
}
});
shards.forEach((s, i) => {
s.draw(ctx2);
s.update();
if (s.timer >= s.ttl || s.lightness >= 99) {
ctx3.fillRect(s.target.x, s.target.y, fidelity + 1, fidelity + 1);
shards.splice(i, 1);
}
});
requestAnimationFrame(loop);
})();
</script>
</body>
</html>
高级版烟花特效html程序
最新推荐文章于 2024-10-02 14:47:14 发布