炫彩爱心粒子动画的实现(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>
运行效果如下:
热门推荐
揭秘游戏设计原理:解密游戏的逻辑与策略
GPA计算方法及其重要性详解
哪些因素会影响学习能力评估的结果?
《黑神话:悟空》三处河北取景地——惊艳了!河北的高级审美
手串的盘玩过程是一种独特的体验
惬意生活指南:如何打造舒适的生活环境
如何在长途飞行中获得充足的睡眠?
抑郁症患者如何有效管理自己的情绪?如何改变消极的思维模式?
应对生活压抑感的有效方法与心理健康维护建议
冻鸡烹饪全攻略:从解冻到餐桌的完美呈现
在家也能做出美味冻鸡!两种实用做法详解
冷鲜鸡 vs 冷冻鸡:营养价值大比拼
甘草的六大功效与作用,三类人群需谨慎使用
甘草的5大健康功效:消化、免疫、護膚大公開!
《权奕天下》短剧奇境:穿越时空的情感微缩
《爱意成碑》短剧奇境:穿越时空的情感微缩
夏天要到了,想避暑就去贵州这三座城
码住!浙里全面“开板”,十大滑雪场速速唤醒你的“雪脉”
2025春晚前瞻:杨幂唐嫣同台,沈腾马丽小品引期待
柳岩揭秘2025春晚小品:热梗融入与创新突破
赵本山宋丹丹:那些年我们一起笑过的春晚
赵丽颖的演技,距离真正的影后有多远?
从梦境探险到现实:探索未知的精神之旅
《层层梦境》:现实与梦境交织的奇幻冒险
工龄满30年退休能拿多少钱?附公式及计算案例!
社保缴费基数与退休金对照表:一文详解影响因素
槟城5天:文化与美食之旅
赵丽颖斩获百花奖,影视领域双丰收,彻底打破“85花”格局
蒋勤勤&陈建斌:灵魂伴侣的相互成就
赵本山全球巡演即将启幕:68岁“小品王”重返舞台,带领“赵家班”再创辉煌