炫彩爱心粒子动画的实现(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>
运行效果如下:
热门推荐
慧眼卫星揭秘宇宙最亮伽马暴:发现最高能量伽马射线谱线
关于核磁共振(MRI)检查你需要了解的事
圣经中的数字12:意义和象征意义
装修门损坏怎么办?一文详解不同损坏程度的修补方法
甲状腺癌术后TSH控制范围最新指南解读
提升情商,让感情更和谐
什么是用户反馈循环?
春节去厦门,这些景点千万别错过!
2025厦门胡里山炮台春节游园会攻略:活动时间、门票及亮点全解析
春节去厦门,这些地方最适合拍大片!
鼓浪屿:世界遗产里的春节之旅
春节去厦门,鼓浪屿和厦大哪个更值得打卡?
青椒肉丝炒饭:《盗墓笔记》里的爱情密码
零失败青椒肉丝:从选材到出锅的全程攻略
《新神榜:哪吒重生》:当哪吒穿上皮衣骑上机车
哪吒:中国文化的超级英雄
长沙十大美食街全攻略:从太平街到扬帆小区,尽享地道长沙味
长沙十大美食街全攻略:从太平街到扬帆小区,尽享地道长沙味
中国“天关”卫星捕捉神秘伽马暴,揭秘宇宙新奥秘
便携式伽马射线成像仪:核能安全守护神
伽马刀:精准治疗脑疾的新希望
当贝投影助力《封神2》,视效总监揭秘科技奥秘
苏轼《定风波》的哲理意境与人生启示
定风波苏轼原文及翻译和赏析
深度解析【天官赐福】第二季:奇幻世界中的璀璨华章
保国寺:北山游步道上的千年古刹
秋日打卡:北山游步道灵峰段
色彩疗法:光的三原色如何影响你的情绪?
常德市科学技术馆:光的三原色合成大挑战!
孩子出现头晕症状应选择脑电图检查还是核磁共振检查