js canvas实现随机粒子特效
作者:莫兮是我 发布时间:2024-06-05 09:10:50
标签:js,粒子
本文实例为大家分享了js canvas随机粒子特效的具体代码,供大家参考,具体内容如下
前言
canvas实现前端的特效美术
结果展示
代码
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="./main.js"></script>
</body>
</html>
main.js
/*
*粒子化类构造
*类功能:
*1.初始化。创建画布,规定粒子属性等;
*2.创建图像并且进行绘制
*3.区域颜色定义
*4.粒子移动和偏射角度
*/
// 生成粒子
let Particle = function(context, options){
let random = Math.random();
this.context = context;
// 在画布里的x坐标
this.x = options.x;
// 在画布里的y坐标
this.y = options.y;
// 取随机数的1/2,对角度进行随机放大
this.s = 0.5 + Math.random();
// this.s = 1 + Math.random();
// 粒子运动的变化角度
this.a = 0;
// 宽度
this.w = window.innerWidth;
// 高度
this.h = window.innerHeight;
// 半径
this.radius = options.radius || 0.5 + Math.random() * 10;
// 颜色
this.color = options.color || "#E5483F";
// console.log(this.color);
// 指定3秒后调用;
// 如果粒子的半径大于0.5,加入新的粒子。
setTimeout(function(){
if(this.radius > 0.5){
particles.push(
new Particle(context, {
x: this.x,
y: this.y,
color: this.radius / 2 > 1 ? "#d6433b" : "#FFFFFF",
radius: this.radius / 2.2 })
);
}
}.call(Particle), 3000);
};
// 渲染图像
Particle.prototype.render = function() {
//从(0,0)开始新的路径;
this.context.beginPath();
// 创建曲线弧
this.context.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
// 绘图的线条宽度
this.context.lineWidth = 2;
//颜色填充
this.context.fillStyle = this.color;
// 填充当前图像的路径
this.context.fill();
// 返回初始点,并且绘制线条到初始位置
this.context.closePath();
};
Particle.prototype.swapColor = function() {
// 排除白色
if (this.color != "#FFFFFF") {
// 判断左右界面,并且赋颜色的值
if (this.x < this.w / 2) {
// 左半边
this.color = "#36fcfa";
} else {
// 右半边
this.color = "#E5483F";
}
}
};
Particle.prototype.move = function() {
// 颜色定义
this.swapColor();
// 横坐标按照cos角度进行变换,并且对其进行随机数放大;
// 偏射角度以便两个半界分开
this.x += Math.cos(this.a) * this.s;
this.y += Math.sin(this.a) * this.s;
// this.y += Math.cos(this.a) * this.s;
// this.x += Math.sin(this.a) * this.s;
// 在变化后,对随机角度进行再重取;
this.a += Math.random() * 0.8 - 0.4;
// 判断全为0,粒子横坐标无移动;
if (this.x < 0 || this.x > this.w - this.radius) {
return false;
}
// 粒子纵坐标无移动;
if (this.y < 0 || this.y > this.h - this.radius) {
return false;
}
// 重新绘制图像
this.render();
return true;
};
let canvas = document.createElement('canvas');
canvas.width = window.innerWidth - 20;
canvas.height = window.innerHeight - 30;
document.body.insertBefore(canvas, null);
let context = canvas.getContext('2d');
const conf = {
frequency: 50,
x: canvas.width,
y: canvas.height
};
let particles = [],
frequency = conf.frequency;
setInterval(function(){
popolate();
}.bind(null), frequency);
function popolate(){
particles.push(
new Particle(context, {
x: conf.x / 2,
y: conf.y / 2
})
);
return particles.length;
}
function clear() {
context.globalAlpha = 0.04;
context.fillStyle = '#000042';
context.fillRect(0,0,canvas.width, canvas.height);
context.globalAlpha = 1;
}
function update(){
particles = particles.filter(p => p.move());
clear();
requestAnimationFrame(arguments.callee);
}
update();
来源:https://blog.csdn.net/u013362192/article/details/115222568


猜你喜欢
- 1.基本函数介绍(1)标准类型函数[type()、str()和 cmp()] &n
- list/tuple转置:以二维grid[][]为例:grid = [[row[i] for row in grid] for i in r
- 简介 Closure 所谓“闭包”,指的是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分
- 前言栈、队列和优先级队列都是非常基础的数据结构。Python作为一种“编码高效”的语言,对这些基础的数据结构都有比较好的实现。在业务需求开发
- 本文的asp xmlhttp类,使用asp的MSXML2.ServerXMLHTTP组件来获取远程音乐文件。类定义 Cls_AspHttp.
- 本文为大家分享了mysql 5.7.18 Archive压缩版安装的具体方法,供大家参考,具体内容如下文章参考:5.7.17 winx64安
- 在Sql Server 2012之前,实现分页主要是使用ROW_NUMBER(),在SQL Server2012,可以使用Offset ..
- 第一步:要使用vant组件安装好vant,npm i vant -S第二步:在你要用到的地方js中引入或者在src/main.js里面引入i
- 挺久没写博客了,因为博主开始了今年另一段美好的实习经历,学习加做项目,时间已排满;很感谢今年这两段经历,让我接触了golang和python
- 说明本文根据https://github.com/liuchengxu/blockchain-tutorial的内容,用python实现的,
- 1.Python代码import cx_Oracletns=cx_Oracle.makedsn('127.0.0.1',
- 公司里很多部门,每个部门可以发多条信息,但每条信息只对应一个部门部门类:class Dep(models.Model): nam
- TeX 排版中文字体嵌入问题,兼谈不拘小节的中文字体设计原文:http://yulewang.spaces.live.com/blog/cn
- js一共有9种数据类型,分别是:未定义(undefined)、空(null)、布尔型(boolean)、字符串(string)、数值(num
- 多表连接查询表与表之间的连接分为内连接和外连接内连接:仅选出两张表互相匹配的记录外连接:既包括两张表匹配的记录,也包括不匹配的记录,同时外连
- 这篇文章主要介绍了通过实例了解Python str()和repr()的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
- Python 使用tf-idf算法计算文档关键字权重,并生成词云1. 根据tf-idf计算一个文档的关键词或者短语:代码如下:注意需要安装p
- 1、将mysql数据导出到SQL文件中(数据库存在的情况)主要需要修改数据库的相关信息,端口号、用户名、密码等其中数据库得存在,不然会报错
- 上个项目中用到了ActiveMQ,只是简单应用,安装完成后直接是用就可以了。由于新项目中一些硬件的限制,需要把消息队列换成RabbitMQ。
- 在制作一个 Python 分发包时经常需要把一些文件添加到包中。最常见的例子是你希望通过 pip install 命令安装 Pyt