分享以下效果

复制下一代到你的项目即可

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Particle 粒子特效</title><style>* {margin: 0;padding: 0;overflow: hidden;}html, body {width: 100%;height: 100%;background-color: #292d35;}</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>window.onload = function() {ParticleEffect.run();// test();};// 实时获取窗口大小 和 从缓存中获取窗口大小 的性能对比function test() {var cache = {width: 1024, height: 780};function test(executeFunc, times) {var start, end, num = times;start = new Date();while(times--) {executeFunc();}end = new Date();console.log(executeFunc.name + ' executes ' + num + 'times and takes ' + (end.getTime() - start.getTime()) / 1000 + 's.');}function getWindowSizeRealTime() {return {width: window.innerWidth || document.documentElement.clientWidth,height: window.innerHeight || document.documentElement.clientHeight};}function getWindowSizeFromCache() {return cache;}[1000, 10000, 100000, 1000000].forEach(function(times) {test(getWindowSizeRealTime, times);test(getWindowSizeFromCache, times);});}// 粒子特效var ParticleEffect = {ctx: null,canvas: null,particles: [],mouseCoordinates: {x: 0, y: 0},config: {id: 'canvas',                   //count: 150,                     // 默认创建粒子数量radius: 1,                      // 默认粒子半径vxRange: [-1, 1],               // 默认粒子横向移动速度范围vyRange: [-1, 1],               // 默认粒子纵向移动速度范围scaleRange: [.5, 1],            // 默认粒子缩放比例范围lineLenThreshold: 125,          // 默认连线长度阈值color: 'rgba(255,255,255,.2)'   // 默认粒子、线条的颜色},init: function(newConfig) {// 更新config配置newConfig && Object.keys(newConfig).forEach(function(key) {_this.config[key] = newConfig[key];});var _this = this;this.canvas = document.getElementById(this.config.id);this.ctx = this.canvas.getContext('2d');// 只有在浏览器支持canvas的情况下才有效if(this.ctx) {Utils.updateWindowSize();var windowSize = Utils.getWindowSize();// 设置canvas宽高this.canvas.width = windowSize.width;this.canvas.height = windowSize.height;// 生成粒子var times = this.config.count;this.particles = [];while(times--) {this.particles.push(new Particle({x: Utils.rangeRandom(this.config.radius, windowSize.width - this.config.radius),y: Utils.rangeRandom(this.config.radius, windowSize.height - this.config.radius),vx: Utils.rangeRandom(this.config.vxRange[0], this.config.vxRange[1]),vy: Utils.rangeRandom(this.config.vyRange[0], this.config.vyRange[1]),color: this.config.color,scale: Utils.rangeRandom(this.config.scaleRange[0], this.config.scaleRange[1]),radius: this.config.radius}));}// 监听鼠标的mouseMove事件,记录下鼠标的x,y坐标window.addEventListener('mousemove', this.handleMouseMove.bind(this), false);// 监听窗口大小改变事件window.addEventListener('resize', this.handleWindowResize.bind(this), false);// 兼容requestAnimationFrameUtils.supportRequestAnimationFrame();window.requestAnimationFrame(this.draw.bind(this));}},move: function() {var windowSize = Utils.getWindowSize();this.particles.forEach(function(item) {// 更新粒子坐标item.x += item.vx;item.y += item.vy;// 如果粒子碰到了左墙壁或右墙壁,则改变粒子的横向运动方向if((item.x - item.radius < 0) || (item.x + item.radius > windowSize.width)) {item.vx *= -1;}// 如果粒子碰到了上墙壁或下墙壁,则改变粒子的纵向运动方向if((item.y - item.radius < 0) || (item.y + item.radius > windowSize.height)) {item.vy *= -1;}});},draw: function() {var _this = this;var lineLenThreshold = this.config.lineLenThreshold;var windowSize = Utils.getWindowSize();// 每次重新绘制之前,需要先清空画布,把上一次的内容清空this.ctx.clearRect(0, 0, windowSize.width, windowSize.height);// 绘制粒子this.particles.forEach(function(item) {item.draw(_this.ctx);});// 绘制粒子之间的连线for(var i = 0; i < this.particles.length; i++) {for(var j = i + 1; j < this.particles.length; j++) {var distance = Math.sqrt(Math.pow(this.particles[i].x - this.particles[j].x, 2) + Math.pow(this.particles[i].y - this.particles[j].y, 2));if(distance < lineLenThreshold) {// 这里我们让距离远的线透明度淡一点,距离近的线透明度深一点this.ctx.strokeStyle = this.translateColors(this.config.color, (1 - distance / lineLenThreshold));this.ctx.beginPath();this.ctx.moveTo(this.particles[i].x, this.particles[i].y);this.ctx.lineTo(this.particles[j].x, this.particles[j].y);this.ctx.closePath();this.ctx.stroke();}}}// 绘制粒子和鼠标之间的连线for(i = 0; i < this.particles.length; i++) {distance = Math.sqrt(Math.pow(this.particles[i].x - this.mouseCoordinates.x, 2) + Math.pow(this.particles[i].y - this.mouseCoordinates.y, 2));if(distance < lineLenThreshold) {this.ctx.strokeStyle = this.translateColors(this.config.color, (1 - distance / lineLenThreshold));this.ctx.beginPath();this.ctx.moveTo(this.particles[i].x, this.particles[i].y);this.ctx.lineTo(this.mouseCoordinates.x, this.mouseCoordinates.y);this.ctx.closePath();this.ctx.stroke();}}// 粒子移动,更新相应的x, y坐标this.move();// 循环调用draw方法window.requestAnimationFrame(this.draw.bind(this));},handleMouseMove: function(event) {var x, y;event = event || window.event;if(event.pageX || event.pageY) {x = event.pageX;y = event.pageY;} else {x = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;y = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;}this.mouseCoordinates = {x: x, y: y};},handleWindowResize: function() {Utils.updateWindowSize();var windowSize = Utils.getWindowSize();this.canvas.width = windowSize.width;this.canvas.height = windowSize.height;},translateColors: function(colorStr, ratio) {var r, g, b, a = 1, colorValues;if(colorStr[0] === '#') {                   // 传的是#RRGGBB形式r = parseInt(colorStr.slice(1, 3), 16);g = parseInt(colorStr.slice(3, 5), 16);b = parseInt(colorStr.slice(5, 7), 16);} else if(colorStr.startsWith('rgb(')) {     // 传的是rgb(r,g,b)形式colorStr = colorStr.slice(4, colorStr.length - 1);colorValues = colorStr.split(',');r = parseInt(colorValues[0].trim());g = parseInt(colorValues[1].trim());b = parseInt(colorValues[2].trim());} else if(colorStr.startsWith('rgba(')) {    // 传的是rgba(r,g,b,a)形式colorStr = colorStr.slice(5, colorStr.length - 1);colorValues = colorStr.split(',');r = parseInt(colorValues[0].trim());g = parseInt(colorValues[1].trim());b = parseInt(colorValues[2].trim());a = parseFloat(colorValues[3].trim());}return 'rgba(' + r + ',' + g + ',' + b + ',' + a * ratio + ')';},run: function(config) {this.init(config);}};/*** Particle 粒子类*/function Particle(attr) {// 粒子属性this.x = attr.x;            // 粒子在画布中的横坐标this.y = attr.y;            // 粒子在画布中的纵坐标this.vx = attr.vx;          // 粒子的横向运动速度this.vy = attr.vy;          // 粒子的纵向运动速度this.color = attr.color;    // 粒子的颜色this.scale = attr.scale;    // 粒子的缩放比例this.radius = attr.radius;  // 粒子的半径大小// 绘制方法if(typeof Particle.prototype.draw === 'undefined') {Particle.prototype.draw = function(ctx) {// canvas画圆方法ctx.beginPath();ctx.fillStyle = this.color;ctx.strokeStyle = this.color;ctx.arc(this.x, this.y, this.radius * this.scale, 0, 2 * Math.PI, false);ctx.closePath();ctx.fill();}}}// 工具var Utils = {_windowSize: {width: 0,height: 0},getWindowSize: function() {return this._windowSize;},updateWindowSize: function() {this._windowSize.width = this.getWindowWidth();this._windowSize.height = this.getWindowHeight();},getWindowWidth: function() {return window.innerWidth || document.documentElement.clientWidth;},getWindowHeight: function() {return window.innerHeight || document.documentElement.clientHeight;},rangeRandom: function(min, max) {const diff = max - min;return min + Math.random() * diff;},supportRequestAnimationFrame: function() {if(!window.requestAnimationFrame) {window.requestAnimationFrame = (window.webkitRequestAnimationFrame ||window.mozRequestAnimationFrame ||window.oRequestAnimationFrame ||window.msRequestAnimationFrame ||function (callback) {setInterval(callback, 1000 / 60)});}}};
</script>
</body>
</html>

canvas动画粒子效果分享,可以做背景,超级好看相关推荐

  1. canvas文字粒子效果 html+css+js 3点饮茶,7点放工,美滋滋~

    先言:  今天3点多在饮茶的时候,发现有好几天没水文章了,但是太难的玩意又不会啊,咋办,突然想起电脑里还有存着一个文字粒子特效,好家伙,这不就来了,先看效果如下: 因为gif图最大5m,但是这东西演示 ...

  2. canvas放射粒子效果

    这个也是别人的代码,就不多介绍了 写了些注释 body {overflow:hidden;margin:0;padding:0;background-color:#222222 } </styl ...

  3. html5粒子效果,8款惊艳的HTML5粒子动画特效

    [导读] HTML5确实强大,很多时候我们可以利用HTML5中的新技术实现非常炫酷的粒子动画效果,粒子动画在HTML5应用中也是比较消耗本地资源的,尤其是CPU,但是有些HTML5粒子效果确实能给用户 ...

  4. canvas 粒子效果 - 手残实践纪录

    canvas 实践 粒子效果 首先确定开发的步骤 准备基础的 html 跟 css 当背景 初始化 canvas 准备一个粒子类 Particle 编写粒子连线的函数 drawLine 编写动画函数 ...

  5. 字 掉落 炫酷 网站_16个富有创意的HTML5 Canvas动画特效集合

    本文作者html5tricks,转载请注明出处 1.HTML5 Canvas高空瀑布下落湖面动画 HTML5 Canvas是一个神奇的网页技术,我们在Canvas画布上可以做任何有趣的事情.今天要分享 ...

  6. Javascript Canvas 实现粒子动画效果分享

    今天和大家一起分享一下用 canvas 实现粒子动画效果的实现,就像下图的效果 要实现这样的效果,我们首先需要一段包含了 canvas 的基础 html 代码 <!DOCTYPE html> ...

  7. 基于canvas使用粒子拼出你想要的文字[2]——粒子的动画效果

    写在最前 本次分享一个用canvas粒子渲染文字的"完整"版实现,功能包括:随机粒子缓动动画,粒子汇聚.发散,并拼出你想要的文字.本文继续上面一节基于canvas使用粒子拼出你想要 ...

  8. html怎么把字做成动画效果,如何使用HTML5 css3实现粒子效果文字动画特效(附完整代码)...

    摘要 腾兴网为您分享:如何使用HTML5 css3实现粒子效果文字动画特效(附完整代码),学宝,小米社区,手机管家,神州专车等软件知识,以及小学英语点读机,便利宝,startos,工资宝,玩,大将军手 ...

  9. 怎么在html中加入特效文字,如何使用HTML5+css3实现粒子效果文字动画特效(附完整代码)...

    我们在浏览web网页的时候会发现现在的网页做的越来越美观,很多动画特效做的越来越炫酷,这离不开HTML5和css3的深入开发.今天我们要来分享一款基于HTML5和css3的文字特效--粒子效果文字动画 ...

最新文章

  1. Matlab中数据拟合函数lsqcurvefit的使用方法与常见问题
  2. iScroll 5 API 中文版
  3. 浅谈协同过滤推荐算法
  4. 背景提取算法——帧间差分法、背景差分法、ViBe算法、ViBe+算法
  5. AndroidStudio安卓原生开发_UI控件_TextView_Button_EditText---Android原生开发工作笔记97
  6. 禁用计算机服务,适当禁用系统服务 提升计算机运行速度
  7. 日本定了一个小目标,在2030年让五分之一的汽车实现自动驾驶
  8. HTML+CSS实现淘宝首页
  9. 小程序 自定义气泡框
  10. [渝粤教育] 徐州工业职业技术学院 药物分离技术 参考 资料
  11. 升降压斩波电路matlab,升降压斩波电路matlab仿真
  12. 阿里ET大脑如何帮助养猪产业提升PSY从20到32?
  13. 【无标题】微信开发者工具无法获取OpenId
  14. html5弹球游戏的实现,HTML5 Canvas 木板弹球小游戏/碰撞检测和反弹
  15. 微服务监控告警:Prometheus
  16. 网站渗透测试之常见漏洞排行
  17. VMware vSphere 超融合架构
  18. 使用TC工具针对端口的限速
  19. BI驾驶舱是什么?BI管理驾驶舱主要内容及特点
  20. 为什么5G用28GHZ载频?载频越高意味着什么呢?

热门文章

  1. 用h5video和h5stream实现监控视频的播放--rtsp流
  2. 网络模型: Capsule
  3. 大四狗:我的java历程(续)
  4. 报告称苹果应用商店逾千款应用存在漏洞
  5. 华三 h3c Rip、静态路由、silent-interface配置
  6. 转:管理大师曼弗雷德:不关注员工的动机需求,何谈高绩效组织?
  7. Win10优化2 REG文件
  8. Google Earth Engine python ——从谷歌地球引擎(GEE)上的点提取栅格值的实现
  9. 三网话费接口API 文档
  10. 怎么判断安卓解锁是否成功