HTML粒子碰撞烟花,html5 canvas漂亮的粒子烟花背景动画特效
特效描述:html5 canvas 漂亮的粒子烟花 背景动画特效。html5 canvas漂亮的粒子烟花背景动画特效
代码结构
1. HTML代码
class Vector2 {
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
add(v) {
this.x += v.x;
this.y += v.y;
return this;
}
multiplyScalar(s) {
this.x *= s;
this.y *= s;
return this;
}
clone() {
return new Vector2(this.x, this.y);
}
}
class Time {
constructor() {
const now = Time.now();
this.delta = 0;
this.elapsed = 0;
this.start = now;
this.previous = now;
}
update() {
const now = Time.now();
this.delta = now - this.previous;
this.elapsed = now - this.start;
this.previous = now;
}
static now() {
return Date.now() / 1000;
}
}
class Particle {
constructor(position, velocity = new Vector2, color = 'white', radius = 1, lifetime = 1, mass = 1) {
this.position = position;
this.velocity = velocity;
this.color = color;
this.radius = radius;
this.lifetime = lifetime;
this.mass = mass;
this.isInCanvas = true;
this.createdOn = Time.now();
}
update(time) {
if (!this.getRemainingLifetime()) {
return;
}
this.velocity.add(Particle.GRAVITATION.clone().multiplyScalar(this.mass));
this.position.add(this.velocity.clone().multiplyScalar(time.delta));
}
render(canvas, context) {
const remainingLifetime = this.getRemainingLifetime();
if (!remainingLifetime) return;
const radius = this.radius * remainingLifetime;
context.globalAlpha = remainingLifetime;
context.globalCompositeOperation = 'lighter';
context.fillStyle = this.color;
context.beginPath();
context.arc(this.position.x, this.position.y, radius, 0, Math.PI * 2);
context.fill();
}
getRemainingLifetime() {
const elapsedLifetime = Time.now() - this.createdOn;
return Math.max(0, this.lifetime - elapsedLifetime) / this.lifetime;
}
}
Particle.GRAVITATION = new Vector2(0, 9.81);
class Trail extends Particle {
constructor(childFactory, position, velocity = new Vector2, lifetime = 1, mass = 1) {
super(position, velocity);
this.childFactory = childFactory;
this.children = [];
this.lifetime = lifetime;
this.mass = mass;
this.isAlive = true;
}
update(time) {
super.update(time);
// Add a new child on every frame
if (this.isAlive && this.getRemainingLifetime()) {
this.children.push(this.childFactory(this));
}
// Remove particles that are dead
this.children = this.children.filter(function(child) {
if (child instanceof Trail) {
return child.isAlive;
}
return child.getRemainingLifetime();
});
// Kill trail if all particles fade away
if (!this.children.length) {
this.isAlive = false;
}
// Update particles
this.children.forEach(function(child) {
child.update(time);
});
}
render(canvas, context) {
// Render all children
this.children.forEach(function(child) {
child.render(canvas, context);
});
}
}
class Rocket extends Trail {
constructor(childFactory, explosionFactory, position, velocity = new Vector2) {
super(childFactory, position, velocity);
this.explosionFactory = explosionFactory;
this.lifetime = 10;
}
update(time) {
if (this.getRemainingLifetime() && this.velocity.y > 0) {
this.explosionFactory(this);
this.lifetime = 0;
}
super.update(time);
}
}
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
const time = new Time;
let rockets = [];
const getTrustParticleFactory = function(baseHue) {
function getColor() {
const hue = Math.floor(Math.random() * 15 + 30);
return `hsl(${hue}, 100%, 75%`;
}
return function(parent) {
const position = this.position.clone();
const velocity = this.velocity.clone().multiplyScalar(-.1);
velocity.x += (Math.random() - .5) * 8;
const color = getColor();
const radius = 1 + Math.random();
const lifetime = .5 + Math.random() * .5;
const mass = .01;
return new Particle(position, velocity, color, radius, lifetime, mass);
};
};
const getExplosionFactory = function(baseHue) {
function getColor() {
const hue = Math.floor(baseHue + Math.random() * 15) % 360;
const lightness = Math.floor(Math.pow(Math.random(), 2) * 50 + 50);
return `hsl(${hue}, 100%, ${lightness}%`;
}
function getChildFactory() {
return function(parent) {
const direction = Math.random() * Math.PI * 2;
const force = 8;
const velocity = new Vector2(Math.cos(direction) * force, Math.sin(direction) * force);
const color = getColor();
const radius = 1 + Math.random();
const lifetime = 1;
const mass = .1;
return new Particle(parent.position.clone(), velocity, color, radius, lifetime, mass);
};
}
function getTrail(position) {
const direction = Math.random() * Math.PI * 2;
const force = Math.random() * 128;
const velocity = new Vector2(Math.cos(direction) * force, Math.sin(direction) * force);
const lifetime = .5 + Math.random();
const mass = .075;
return new Trail(getChildFactory(), position, velocity, lifetime, mass);
}
return function(parent) {
let trails = 32;
while (trails--) {
parent.children.push(getTrail(parent.position.clone()));
}
};
};
const addRocket = function() {
const trustParticleFactory = getTrustParticleFactory();
const explosionFactory = getExplosionFactory(Math.random() * 360);
const position = new Vector2(Math.random() * canvas.width, canvas.height);
const thrust = window.innerHeight * .75;
const angle = Math.PI / -2 + (Math.random() - .5) * Math.PI / 8;
const velocity = new Vector2(Math.cos(angle) * thrust, Math.sin(angle) * thrust);
const lifetime = 3;
rockets.push(new Rocket(trustParticleFactory, explosionFactory, position, velocity, lifetime));
rockets = rockets.filter(function(rocket) {
return rocket.isAlive;
});
};
const render = function() {
requestAnimationFrame(render);
time.update();
context.clearRect(0, 0, canvas.width, canvas.height);
rockets.forEach(function(rocket) {
rocket.update(time);
rocket.render(canvas, context);
});
};
const resize = function() {
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
};
canvas.onclick = addRocket;
document.body.appendChild(canvas);
window.onresize = resize;
resize();
setInterval(addRocket, 2000);
render();
HTML粒子碰撞烟花,html5 canvas漂亮的粒子烟花背景动画特效相关推荐
- html5基于canvas制作酷炫,应用HTML5 Canvas制作酷炫科技背景动画特效
更多特效代码请添加HTML5前端交流群111645711 看这性感的线条,激情的律动! 废话不多说,上代码! 源代码 需要文档版本源码,可以加我的HTML5前端交流群111645711 * { mar ...
- HTML5+Canvas漂亮的3D烟花动画生日特效,节日特效,烟花
fireworks HTML5+Canvas漂亮的3D烟花动画生日特效,节日特效,烟花 https://github.com/louislivi/fireworks
- 粒子背景php,html5+canvas圆形粒子移动背景动画特效
html5+canvas圆形粒子移动背景动画特效 this.update = function () { var lastPoint = { x: _this.x, y: _this.y }; // ...
- html5 特效 背景 腾讯,html5腾讯QQ登录界面背景动画特效
特效描述:html5 腾讯QQ 登录界面 背景动画特效.腾讯QQ登陆界面动态背景,直接从腾讯网站获取,js代码有加密,做了个简单地示例 代码结构 1. 引入JS 2. HTML代码 *{margin: ...
- html5动态效果随鼠标动,html5跟随鼠标移动银河星系背景动画特效
特效描述:html5 跟随鼠标移动 银河星系 背景动画特效.html5银河星系背景动画,鼠标的移动.点击会发生相应的变化! 代码结构 1. HTML代码 流星雨 body {margin:0;padd ...
- php插入html动态背景,HTML5超酷响应式视频背景动画特效
简要教程 这是一款HTML5超酷响应式视频背景动画特效.该视频背景可以将视频自适应屏幕的大小,制作出炫酷的动态视频背景效果. 使用方法 在页面中引入bideo.js文件. HTML结构 该视频背景动画 ...
- html5 canvas 烟花,html5 canvas酷炫的烟花爆炸动画特效
特效描述:html5canvas 酷炫的 烟花爆炸动画特效.html5点击页面烟花爆炸动画,3D烟花动画,酷炫的烟花特效. 代码结构 1. 引入JS 2. HTML代码 点击页面 (function( ...
- html 穿越星空效果,html5 canvas绚丽3d星空飞行穿梭动画特效
简要教程 这是一款使用 html5 canvas和 jQuery 制作的绚丽3d星空飞行穿梭动画特效.该3d星空飞行特效模拟了飞船在宇宙星空中快速穿梭的动画场景,效果非常逼真. HTML结构 HTML ...
- html做星空网页游戏,html5 canvas绚丽3d星空飞行穿梭动画特效
这是一款使用 html5 canvas和 jQuery 制作的绚丽3d星空飞行穿梭动画特效.该3d星空飞行特效模拟了飞船在宇宙星空中快速穿梭的动画场景,效果非常逼真. HTML结构 HTML结构就使用 ...
最新文章
- 4.IT-解决方案-4-Cluster-Win2K3
- vue Iframe
- vuetify electron (开发环境及打包)
- [文档].艾米电子 - 在综合中使用函数,Verilog
- TortoiseGit上传项目到GitHub
- 戴尔看好Ubuntu超越Windows的十个优势
- C语言 fopen 函数 - C语言零基础入门教程
- 中国联通董事李福申辞任
- python零基础入门五小时教学_五小时轻松入门Python
- Shell 中 exit 和 return 的区别
- Golang 1.16 新特性-embed 包及其使用
- 全球及中国汽车节能减排行业投资可行性及十四五发展趋势研究报告2021-2027年
- 618运动好物清单、必买运动装备推荐
- 爱快iKuai 安装成功后运行提示程序运行中解决办法
- react实现上传文件进度条功能_js上传文件(可自定义进度条)
- 华为摄像头采集自动聚焦崩溃
- 2061 【例1.2】梯形面积
- 怎么讲d 盘里的软件弄到桌面_桌面如何存文件到D - 卡饭网
- 组合数学之递推关系(二)常系数线性齐次递推关系及其通项求解
- 计算机仿真之盐水浓度动态变化