炫彩爱心粒子动画的实现(HTML5 + Canvas)
创作时间:
作者:
@小白创作中心
炫彩爱心粒子动画的实现(HTML5 + Canvas)
引用
CSDN
1.
https://blog.csdn.net/qq_67010376/article/details/146202886
本文将分享一个使用HTML5 Canvas和JavaScript制作的炫彩爱心粒子特效。最终效果是一个不断闪烁、流动的五彩爱心,非常适合作为网页背景或情人节特效。
在这个项目中,我们将创建一个炫彩爱心粒子动画特效。这个特效使用HTML5 Canvas和JavaScript实现,效果是一个不断闪烁、流动的五彩爱心。以下是完整的代码实现:
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>Hacker6 </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<style>
html, body {
height: 100%;
padding: 0;
margin: 0;
background: #000;
}
#canvasWrapper {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
canvas {
width: 80%; /* Set canvas width to 80% of the original size */
height: 80%; /* Set canvas height to 80% of the original size */
}
</style>
</HEAD>
<BODY>
<div id="canvasWrapper">
<canvas id="pinkboard"></canvas>
</div>
<script>
/*
* Settings
*/
var settings = {
particles: {
length: 500, // maximum amount of particles
duration: 2, // particle duration in sec
velocity: 100, // particle velocity in pixels/sec
effect: -0.75, // play with this for a nice effect
size: 30, // particle size in pixels
},
};
/*
* RequestAnimationFrame polyfill by Erik Möller
*/
(function(){var b=0;var c=["ms","moz","webkit","o"];for(var a=0;a<c.length&&!window.requestAnimationFrame;++a){window.requestAnimationFrame=window[c[a]+"RequestAnimationFrame"];window.cancelAnimationFrame=window[c[a]+"CancelAnimationFrame"]||window[c[a]+"CancelRequestAnimationFrame"]}if(!window.requestAnimationFrame){window.requestAnimationFrame=function(h,e){var d=new Date().getTime();var f=Math.max(0,16-(d-b));var g=window.setTimeout(function(){h(d+f)},f);b=d+f;return g}}if(!window.cancelAnimationFrame){window.cancelAnimationFrame=function(d){clearTimeout(d)}}}());
/*
* Point class
*/
var Point = (function() {
function Point(x, y) {
this.x = (typeof x !== 'undefined') ? x : 0;
this.y = (typeof y !== 'undefined') ? y : 0;
}
Point.prototype.clone = function() {
return new Point(this.x, this.y);
};
Point.prototype.length = function(length) {
if (typeof length == 'undefined')
return Math.sqrt(this.x * this.x + this.y * this.y);
this.normalize();
this.x *= length;
this.y *= length;
return this;
};
Point.prototype.normalize = function() {
var length = this.length();
this.x /= length;
this.y /= length;
return this;
};
return Point;
})();
/*
* Particle class
*/
var Particle = (function() {
function Particle() {
this.position = new Point();
this.velocity = new Point();
this.acceleration = new Point();
this.age = 0;
}
Particle.prototype.initialize = function(x, y, dx, dy) {
this.position.x = x;
this.position.y = y;
this.velocity.x = dx;
this.velocity.y = dy;
this.acceleration.x = dx * settings.particles.effect;
this.acceleration.y = dy * settings.particles.effect;
this.age = 0;
};
Particle.prototype.update = function(deltaTime) {
this.position.x += this.velocity.x * deltaTime;
this.position.y += this.velocity.y * deltaTime;
this.velocity.x += this.acceleration.x * deltaTime;
this.velocity.y += this.acceleration.y * deltaTime;
this.age += deltaTime;
};
Particle.prototype.draw = function(context, gradient) {
function ease(t) {
return (--t) * t * t + 1;
}
var size = settings.particles.size * ease(this.age / settings.particles.duration);
context.globalAlpha = 1 - this.age / settings.particles.duration;
context.beginPath();
context.arc(this.position.x, this.position.y, size / 2, 0, Math.PI * 2);
context.fillStyle = gradient;
context.fill();
};
return Particle;
})();
/*
* ParticlePool class
*/
var ParticlePool = (function() {
var particles,
firstActive = 0,
firstFree = 0,
duration = settings.particles.duration;
function ParticlePool(length) {
// create and populate particle pool
particles = new Array(length);
for (var i = 0; i < particles.length; i++)
particles[i] = new Particle();
}
ParticlePool.prototype.add = function(x, y, dx, dy) {
particles[firstFree].initialize(x, y, dx, dy);
// handle circular queue
firstFree++;
if (firstFree == particles.length) firstFree = 0;
if (firstActive == firstFree ) firstActive++;
if (firstActive == particles.length) firstActive = 0;
};
ParticlePool.prototype.update = function(deltaTime) {
var i;
// update active particles
if (firstActive < firstFree) {
for (i = firstActive; i < firstFree; i++)
particles[i].update(deltaTime);
}
if (firstFree < firstActive) {
for (i = firstActive; i < particles.length; i++)
particles[i].update(deltaTime);
for (i = 0; i < firstFree; i++)
particles[i].update(deltaTime);
}
// remove inactive particles
while (particles[firstActive].age >= duration && firstActive != firstFree) {
firstActive++;
if (firstActive == particles.length) firstActive = 0;
}
};
ParticlePool.prototype.draw = function(context, gradient) {
// draw active particles
if (firstActive < firstFree) {
for (i = firstActive; i < firstFree; i++)
particles[i].draw(context, gradient);
}
if (firstFree < firstActive) {
for (i = firstActive; i < particles.length; i++)
particles[i].draw(context, gradient);
for (i = 0; i < firstFree; i++)
particles[i].draw(context, gradient);
}
};
return ParticlePool;
})();
/*
* Putting it all together
*/
(function(canvas) {
var context = canvas.getContext('2d'),
particles = new ParticlePool(settings.particles.length),
particleRate = settings.particles.length / settings.particles.duration, // 生成粒子速率
time;
function pointOnHeart(t) {
return new Point(
96 * Math.pow(Math.sin(t), 3),
78 * Math.cos(t) - 30 * Math.cos(2 * t) - 12 * Math.cos(3 * t) - 6 * Math.cos(4 * t) + 15
);
}
// 修改 Particle 类,添加随机颜色
Particle.prototype.initialize = function(x, y, dx, dy) {
this.position.x = x;
this.position.y = y;
this.velocity.x = dx;
this.velocity.y = dy;
this.acceleration.x = dx * settings.particles.effect;
this.acceleration.y = dy * settings.particles.effect;
this.age = 0;
// 生成一个随机颜色(HSL 颜色模式)
this.color = `hsl(${Math.random() * 360}, 100%, 60%)`;
};
// 修改 draw 方法,使用粒子的随机颜色
Particle.prototype.draw = function(context) {
function ease(t) {
return (--t) * t * t + 1;
}
var size = settings.particles.size * ease(this.age / settings.particles.duration);
context.globalAlpha = 1 - this.age / settings.particles.duration;
context.beginPath();
context.arc(this.position.x, this.position.y, size / 2, 0, Math.PI * 2);
context.fillStyle = this.color; // 使用粒子的随机颜色
context.fill();
};
function render() {
requestAnimationFrame(render);
var newTime = new Date().getTime() / 1000,
deltaTime = newTime - (time || newTime);
time = newTime;
context.clearRect(0, 0, canvas.width, canvas.height);
var amount = particleRate * deltaTime;
for (var i = 0; i < amount; i++) {
var pos = pointOnHeart(Math.PI - 2 * Math.PI * Math.random());
var dir = pos.clone().length(settings.particles.velocity);
particles.add(canvas.width / 2 + pos.x, canvas.height / 2 - pos.y, dir.x, -dir.y);
}
particles.update(deltaTime);
particles.draw(context);
}
function onResize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener('resize', onResize);
onResize();
render();
})(document.getElementById('pinkboard'));
</script>
</BODY>
</HTML>
运行效果如下:
热门推荐
维他命C的五大功效,该怎么做?副作用,何时吃,推荐补充的族群
上海九院医疗特色:口腔科、整复外科、骨科、眼科全国领先
中国十大特色香肠:从南到北,各具风味的美味传奇
新增建设用地审批有哪些程序?
陕西五大著名国家名胜风景区
消毒项目管理员指南:从职责到创新的全方位管理
消毒产品国家标准和行业标准有哪些不同?
会计与业务部门报销规则起冲突,如何高效解决矛盾
职场电话礼仪的注意点
美国主要节日有哪些?
租房用虚假地址签合同的法律风险与应对策略
用虚假地址注册营业执照的处罚标准是什么
代表说996不理想,应该888,重塑工作与生活的平衡
中国造船行业发展趋势研究与未来前景分析报告(2025-2032年)
化工厂手套如何管理好
玻璃纤维在生活中十分常见?究竟藏着哪些健康隐患
景德镇文化出海:让陶瓷文化成为“破壁者”
深入解析BMS:电池管理系统的基础功能与应用
读懂《道德经》,整个人都通透了
数据库为什么id这么重要
心理学上有一个词叫:蝴蝶效应
设备管理项目需求怎么写
港股市场波动:中金、银河澄清合并传闻引发股价异动
为什么挂号尽量别挂特需门诊?解析特需门诊的优缺点
4060笔记本和4060台式差多少
四君子汤:传统中药方剂的现代应用
白芸豆可以减肥吗?科学解读白芸豆的减肥效果
倚天最后出场的三大高手,张无忌三战三败,张三丰也望尘莫及
文旅新探|广西涠洲岛:浮天无岸寻鲸处
让企业在种业振兴中“唱主角”,如何定义种业企业?