如何用前端html實(shí)現(xiàn)2024煙花效果

用HTML、CSS和JavaScript編寫的網(wǎng)頁,主要用于展示“2024新年快樂!”的文字形式煙花效果。下面是對代碼主要部分的分析:
HTML結(jié)構(gòu)
- 包含三個
<canvas>元素,用于繪制動畫。 - 引入百度統(tǒng)計的腳本。
CSS樣式
- 設(shè)置
body的背景為黑色,并使得canvas元素絕對定位,覆蓋整個頁面。
JavaScript 功能
百度統(tǒng)計腳本:頁面開始時引入了百度統(tǒng)計的腳本,用于網(wǎng)頁訪問數(shù)據(jù)分析。
獲取URL參數(shù):GetRequest函數(shù)用于解析URL中的查詢字符串參數(shù)。
煙花碎片(Shard)類:
每個Shard代表煙花爆炸后的一個碎片。
包含碎片的位置、顏色、大小、速度等屬性。
draw方法用于在canvas上繪制碎片。
update方法用于更新碎片的位置和狀態(tài)。
火箭(Rocket)類:
表示發(fā)射的煙花火箭。
包含火箭的位置、速度、顏色等屬性。
draw方法用于在canvas上繪制火箭。
update方法用于更新火箭的位置。
explode方法用于模擬火箭爆炸,生成多個Shard實(shí)例。
初始化和動畫循環(huán):
獲取所有canvas元素和對應(yīng)的2D渲染上下文。
根據(jù)屏幕大小調(diào)整字體大小,以適應(yīng)屏幕寬度,并在一個canvas上繪制“2024新年快樂!”文字。
通過讀取繪制的文字的像素數(shù)據(jù),確定煙花爆炸的目標(biāo)位置。
使用requestAnimationFrame創(chuàng)建動畫循環(huán),不斷更新和繪制火箭和碎片,模擬煙花效果。
輔助函數(shù):
lerp(線性插值函數(shù)):用于平滑地在兩個值之間插值,常用于動畫效果中。
執(zhí)行流程:
頁面加載完成后,動畫循環(huán)開始運(yùn)行。
每隔一定幀數(shù),生成一個新的Rocket實(shí)例,模擬火箭發(fā)射。
當(dāng)火箭達(dá)到一定高度后,調(diào)用explode方法,生成多個Shard實(shí)例,模擬煙花爆炸。
碎片根據(jù)預(yù)設(shè)的目標(biāo)位置移動,最終形成“2024新年快樂!”的文字形狀。
<!DOCTYPE html>
<html lang="en">
<script>
var _hmt = _hmt || [];
(function () {
var hm = document.createElement("script");
hm.src = "https://#/hm.js?c923daf3182a4b0ce01878475080aadc";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<head>
<meta charset="UTF-8">
<title>2024,新年快樂!</title>
</head>
<style>
body {
margin: 0;
overflow: hidden;
background: black;
}
canvas {
position: absolute;
}
</style>
<body>
<canvas></canvas>
<canvas></canvas>
<canvas></canvas>
<script>
function GetRequest() {
var url = decodeURI(location.search); //獲取url中"?"符后的字串
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}
class Shard {
constructor(x, y, hue) {
this.x = x;
this.y = y;
this.hue = hue;
this.lightness = 50;
this.size = 15 + Math.random() * 10;
const angle = Math.random() * 2 * Math.PI;
const blastSpeed = 1 + Math.random() * 6;
this.xSpeed = Math.cos(angle) * blastSpeed;
this.ySpeed = Math.sin(angle) * blastSpeed;
this.target = getTarget();
this.ttl = 100;
this.timer = 0;
}
draw() {
ctx2.fillStyle = `hsl(${this.hue}, 100%, ${this.lightness}%)`;
ctx2.beginPath();
ctx2.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
ctx2.closePath();
ctx2.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 a = Math.atan2(dy, dx);
const tx = Math.cos(a) * 5;
const ty = Math.sin(a) * 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 + fidelity / 2, 0.05);
this.y = lerp(this.y, this.target.y + fidelity / 2, 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.xSpeed = lerp(this.xSpeed, 0, 0.1);
this.size = lerp(this.size, 1, 0.05);
if (this.y > c2.height) {
shards.forEach((shard, idx) => {
if (shard === this) {
shards.splice(idx, 1);
}
});
}
}
this.x = this.x + this.xSpeed;
this.y = this.y + this.ySpeed;
}
}
class Rocket {
constructor() {
const quarterW = c2.width / 4;
this.x = quarterW + Math.random() * (c2.width - quarterW);
this.y = c2.height - 15;
this.angle = Math.random() * Math.PI / 4 - Math.PI / 6;
this.blastSpeed = 6 + Math.random() * 7;
this.shardCount = 15 + Math.floor(Math.random() * 15);
this.xSpeed = Math.sin(this.angle) * this.blastSpeed;
this.ySpeed = -Math.cos(this.angle) * this.blastSpeed;
this.hue = Math.floor(Math.random() * 360);
this.trail = [];
}
draw() {
ctx2.save();
ctx2.translate(this.x, this.y);
ctx2.rotate(Math.atan2(this.ySpeed, this.xSpeed) + Math.PI / 2);
ctx2.fillStyle = `hsl(${this.hue}, 100%, 50%)`;
ctx2.fillRect(0, 0, 5, 15);
ctx2.restore();
}
update() {
this.x = this.x + this.xSpeed;
this.y = this.y + this.ySpeed;
this.ySpeed += 0.1;
}
explode() {
for (let i = 0; i < 70; i++) {
shards.push(new Shard(this.x, this.y, this.hue));
}
}
}
console.log(GetRequest('val').val)
// INITIALIZATION
const [c1, c2, c3] = document.querySelectorAll('canvas');
const [ctx1, ctx2, ctx3] = [c1, c2, c3].map(c => c.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;
ctx1.fillStyle = '#000';
const text = '2024新年快樂!'
let textWidth = 99999999;
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);
//ctx2.clearRect(0, 0, c2.width, c2.height);
counter += 1;
if (counter % 15 === 0) {
rockets.push(new Rocket());
}
rockets.forEach((r, i) => {
r.draw();
r.update();
if (r.ySpeed > 0) {
r.explode();
rockets.splice(i, 1);
}
});
shards.forEach((s, i) => {
s.draw();
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);
})();
// HELPER FUNCTIONS
const lerp = (a, b, t) => Math.abs(b - a) > 0.1 ? a + t * (b - a) : b;
function getTarget() {
if (targets.length > 0) {
const idx = Math.floor(Math.random() * targets.length);
let { x, y } = targets[idx];
targets.splice(idx, 1);
x += c2.width / 2 - textWidth / 2;
y += c2.height / 2 - fontSize / 2;
return { x, y };
}
}
</script>
</body>
</html>步驟 1: 頁面結(jié)構(gòu)和引入腳本
- HTML定義了三個
canvas元素用于繪制煙花動畫。 - 引入百度統(tǒng)計腳本,用于收集頁面訪問數(shù)據(jù)。
步驟 2: 樣式設(shè)置
- 頁面背景設(shè)置為黑色,
canvas元素被設(shè)置為絕對定位,覆蓋整個屏幕。
步驟 3: JavaScript 功能實(shí)現(xiàn)
3.1 獲取URL參數(shù)
GetRequest函數(shù)解析當(dāng)前URL的查詢字符串,將參數(shù)保存在一個對象中返回。
3.2 定義煙花碎片(Shard)類
- 每個
Shard實(shí)例代表煙花爆炸后的一個碎片。 - 包含位置、顏色、大小、速度等屬性。
draw方法用于繪制碎片。update方法用于更新碎片的狀態(tài)和位置,包括模擬重力影響和向目標(biāo)位置移動。
3.3 定義火箭(Rocket)類
- 每個
Rocket實(shí)例代表一個發(fā)射的煙花火箭。 - 包含位置、速度、顏色等屬性。
draw方法用于繪制火箭。update方法用于更新火箭的位置,模擬火箭上升。
3.4 初始化和動畫循環(huán)
- 初始化:根據(jù)屏幕大小調(diào)整字體大小,確保“2024新年快樂!”文字適應(yīng)屏幕寬度,并在
canvas上繪制該文字。通過讀取文字像素數(shù)據(jù)來確定煙花爆炸的目標(biāo)位置。 - 動畫循環(huán):使用
requestAnimationFrame循環(huán)不斷更新和繪制火箭和碎片,以及檢測碎片是否達(dá)到目標(biāo)位置或生命周期結(jié)束。
3.5 輔助函數(shù)
lerp函數(shù):用于在兩個數(shù)值之間進(jìn)行線性插值,幫助平滑動畫效果。
步驟 4: 執(zhí)行動畫
初始化畫布:調(diào)整畫布大小以適應(yīng)窗口,繪制“2024新年快樂!”文字,并基于此文字的像素數(shù)據(jù)確定煙花目標(biāo)位置。
啟動動畫循環(huán):
- 每隔一定時間間隔,創(chuàng)建一個新的
Rocket實(shí)例,模擬火箭發(fā)射。 - 更新每個火箭的位置,當(dāng)火箭達(dá)到一定高度時觸發(fā)爆炸,生成多個
Shard實(shí)例。 - 更新每個
Shard的位置,使其朝目標(biāo)位置移動。 - 當(dāng)
Shard到達(dá)目標(biāo)位置或生命周期結(jié)束時,從數(shù)組中移除。
- 每隔一定時間間隔,創(chuàng)建一個新的
渲染煙花效果:通過不斷更新
canvas上的火箭和碎片位置,以及繪制這些元素,形成動態(tài)的煙花效果。碎片最終會根據(jù)預(yù)設(shè)的目標(biāo)位置排列,形成“2024新年快樂!”的文字形狀。通過這些步驟,代碼實(shí)現(xiàn)了一個視覺吸引的新年煙花祝福動畫,既展示了編程技巧,也增添了節(jié)日氣氛。
explode方法在火箭達(dá)到頂點(diǎn)時被調(diào)用,生成多個Shard實(shí)例。
總結(jié)
到此這篇關(guān)于如何用前端html實(shí)現(xiàn)2024煙花效果的文章就介紹到這了,更多相關(guān)前端html實(shí)現(xiàn)2024煙花內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js點(diǎn)擊出現(xiàn)懸浮窗效果不使用JQuery插件
JQuery有很多這樣的插件,但本文的這個是跟著自己的想法寫的,也不知道他人是如何實(shí)現(xiàn)的,感興趣的朋友可以了解下2014-01-01
JavaScript結(jié)合AJAX_stream實(shí)現(xiàn)流式顯示
這篇文章主要介紹了JavaScript結(jié)合AJAX_stream實(shí)現(xiàn)流式顯示,需要的朋友可以參考下2015-01-01
Bootstrap學(xué)習(xí)系列之使用 Bootstrap Typeahead 組件實(shí)現(xiàn)百度下拉效果
這篇文章主要介紹了Bootstrap學(xué)習(xí)系列之使用 Bootstrap Typeahead 組件實(shí)現(xiàn)百度下拉效果的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07
JavaScript ES6中const、let與var的對比詳解
這篇文章主要給大家介紹了在JavaScript中const、let與var對比的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起看看吧。2017-06-06
avalon js實(shí)現(xiàn)仿google plus圖片多張拖動排序附源碼下載
這篇文章主要介紹了avalon js實(shí)現(xiàn)仿google plus圖片多張拖動排序附源碼下載的相關(guān)資料,需要的朋友可以參考下2015-09-09
js實(shí)現(xiàn)鼠標(biāo)拖拽多選功能示例
本篇文章主要介紹了js實(shí)現(xiàn)鼠標(biāo)拖拽多選功能示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08

