炫彩爱心粒子动画的实现(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>
运行效果如下:
热门推荐
春节假期,小生意如何带火县域经济?
春节摆摊卖年货,轻松月入三万!
双十一买枸杞?这份安全指南请收好!
黑枸杞 vs. 红枸杞:谁更适合你?
枸杞泡水真的能养生吗?科学家有话说!
孙中山弥留之际立下三份遗嘱,分别写给谁,都说了什么内容?
北京交通信息网:官方实时路况查询平台
佛家篇章:地藏菩萨
新年到!学会准确朗读郭伦伍的春联古诗词
科普必读:高铁火车报销凭证如何打印领取?
撞座了!两乘客买到"同日同座"高铁票?12306回应!
怎样预防犬瘟热
犬猫疫苗注射的必要性总结
犬瘟热的症状有哪些
忠县家庭节能环保的小妙招 ,你家还有哪些环保小窍门?
西安必打卡:贾三清真灌汤包&老米家泡馍
西安五天四晚深度游攻略:跟着霞霞玩转古都!
阔腿裤起静电吸腿怎么办 阔腿裤起球了怎么办
裤子静电吸腿怎么办?5个实用小妙招轻松应对
关晓彤最新时尚造型引热议:华伦天奴任务裙
关晓彤出任年度粉红丝带大使,为乳腺癌防治事业注入新活力
脊骨神经科医生如何在不用药的情况下缓解背部疼痛
三阴交一穴舒畅肝、肾、脾经 花莲慈院中医部养生教学“十总穴之妇科穴”
宫崎骏教你如何打造有深度的动漫角色
“静音车厢”到底有多静?网友:一“静”到底!
肺部结节不用开刀——肺结节消融“以针代刀”
不同类型的放射治疗:为不同肿瘤量身定制的治疗方案
肺癌放疗副作用真有那么大?肺泡真的会受损吗?一探究竟!
孟婆汤的寓意与来历
高原地区走一走,最怕什么?预防高原反应的3个技巧,收藏备用!