[Unity学习教程] web实现酷炫的canvas粒子动画配景

[复制链接]
查看1110 | 回复0 | 2023-8-23 12:08:30 | 显示全部楼层 |阅读模式 来自 中国北京
前言

粒子动画,顾名思义,就是页面上存在大量的粒子构建而成的动画。
传统的粒子动画重要由 Canvas、WebGL 实现,我们经常用来用作网站的动画配景。
今天介绍两个个可以轻松创建高度可定制的粒子动画库。
一、particle-bg

1. git地点:

https://github.com/PengJiyuan/particle-bg
不带连线结果:

带连线的结果:

2. 安装

NPM
  1. npm i particle-bg
复制代码
CDN
  1. https://unpkg.com/particle-bg/lib/particle-bg.umd.min.js
复制代码
3. 使用

ES Module
  1. import particleBg from 'particle-bg';
  2. particleBg('body');
复制代码
Browser
  1. <script src="https://unpkg.com/particle-bg/lib/particle-bg.umd.min.js
  2. "></script><script>  particleBg('body');</script>
复制代码
API
  1. particleBg(element, config)
  2. element
  3. 要插入粒子背景的DOM。
  4. config [Object]
  5. 粒子背景的一些配置。
  6. config.color
  7. default: '#fff'
  8. 粒子的颜色。
  9. config.count
  10. default: 100
  11. 粒子的数量
  12. config.radius
  13. default: 2
  14. 粒子的半径
  15. config.distance
  16. default: width / 10
  17. 粒子间距小于多少会连线
  18. config.rate
  19. default: width / 10000
  20. 粒子运动的速率
  21. config.zIndex
  22. default: 1
  23. canvas的z-index.
  24. config.resize
  25. default: true
  26. 是否监听window.resize,自动缩放粒子背景。
  27. config.line
  28. default: true
  29. 粒子之间是否连线。
  30. config.bounce
  31. default: false
  32. 是否触碰边界进行反弹。
复制代码
4. 完备demo

  1. <div style="width: 100%;height: 200px;background-color: rgb(109, 108, 106);" id="Bg"></div><script src="https://unpkg.com/particle-bg/lib/particle-bg.umd.min.js
  2. "></script><script>   particleBg('#Bg', {        color: '#fff',        count: 100,        radius: 2,        distance: 70,        rate:1,        zIndex: 1,        resize: true,        line: true,        bounce: true,    });</script>
复制代码
结果

二、tsParticles

TypeScript Particles 是在 particles.js 底子上重写的一个库,目标是更轻易地创建更多的配景动画,并提供更多的实用步调和支持功能。
这个库最大的亮点在于它可以用于许多差异的框架,比方 React.js, Vue.js (2.x and 3.x), Angular, Svelte, jQuery等等JS框架、Web组件。
1. 源码地点:

GitHub地点:https://github.com/matteobruni/tsparticles
官网地点:https://particles.js.org/
更多demo地点:https://codepen.io/collection/DPOage
2. 安装

npm
  1. npm install tsparticles-engine
复制代码
yarn
  1. yarn add tsparticles-engine
复制代码
pnpm
  1. pnpm install tsparticles-engine
复制代码
3. 引入

从版本1.12.11开始使用import和require导入tsParticles了。
  1. const tsParticles = require("tsparticles-engine");
  2. // or
  3. import { tsParticles } from "tsparticles-engine";
复制代码
4. 使用

index.html
  1. <div id="tsparticles"></div>
  2. <script src="tsparticles.engine.min.js"></script>
复制代码
app.js
  1. tsParticles
  2.     .loadJSON("tsparticles", "presets/default.json")
  3.     .then(container => {
  4.         console.log("callback - tsparticles config loaded");
  5.     })
  6.     .catch(error => {
  7.         console.error(error);
  8.     });
复制代码
5. 几个例子

5.1 ts粒子五彩纸屑烟花

  1. <script src="https://cdn.jsdelivr.net/npm/tsparticles-confetti@2.11.0/tsparticles.confetti.bundle.min.js"></script>
  2. <script id="rendered-js">
  3.     const duration = 60 * 60 * 1000,
  4.         animationEnd = Date.now() + duration,
  5.         defaults = { startVelocity: 30, spread: 360, ticks: 20, zIndex: 0 };
  6.     function randomInRange(min, max) {
  7.         return Math.random() * (max - min) + min;
  8.     }
  9.     const interval = setInterval(function () {
  10.         const timeLeft = animationEnd - Date.now();
  11.         if (timeLeft <= 0) {
  12.             return clearInterval(interval);
  13.         }
  14.         const particleCount = 20 * (timeLeft / duration);
  15.         // since particles fall down, start a bit higher than random
  16.         confetti(
  17.             Object.assign({}, defaults, {
  18.                 particleCount,
  19.                 origin: { x: randomInRange(0.1, 0.3), y: Math.random() - 0.2 }
  20.             }));
  21.         confetti(
  22.             Object.assign({}, defaults, {
  23.                 particleCount,
  24.                 origin: { x: randomInRange(0.7, 0.9), y: Math.random() - 0.2 }
  25.             }));
  26.     }, 250);
  27. </script>
复制代码
结果

5.2 多粒子产卵器-用tsParticles制作

  1. <div style="width: 100%;height: 200px;background-color: rgb(109, 108, 106);" id="tsparticles"></div>
  2. <script src="https://cdn.jsdelivr.net/npm/tsparticles@1.19.0-alpha.3/dist/tsparticles.min.js"></script>
  3. <script id="rendered-js">
  4.     tsParticles.load("tsparticles", {
  5.         fpsLimit: 60,
  6.         particles: {
  7.             number: {
  8.                 value: 0,
  9.                 density: {
  10.                     enable: true,
  11.                     value_area: 800
  12.                 }
  13.             },
  14.             color: {
  15.                 value: "#ffff00"
  16.             },
  17.             shape: {
  18.                 type: "circle"
  19.             },
  20.             opacity: {
  21.                 value: 1,
  22.                 random: false,
  23.                 animation: {
  24.                     enable: true,
  25.                     speed: 0.5,
  26.                     minimumValue: 0,
  27.                     sync: false
  28.                 }
  29.             },
  30.             size: {
  31.                 value: 8,
  32.                 random: { enable: true, minimumValue: 4 },
  33.                 animation: {
  34.                     enable: false,
  35.                     speed: 20,
  36.                     minimumValue: 4,
  37.                     sync: false
  38.                 }
  39.             },
  40.             move: {
  41.                 enable: true,
  42.                 gravity: {
  43.                     enable: true,
  44.                     acceleration: -0.5
  45.                 },
  46.                 speed: 5,
  47.                 direction: "top",
  48.                 random: false,
  49.                 straight: false,
  50.                 outModes: {
  51.                     default: "destroy",
  52.                     bottom: "none"
  53.                 },
  54.                 attract: {
  55.                     enable: true,
  56.                     distance: 300,
  57.                     rotate: {
  58.                         x: 600,
  59.                         y: 1200
  60.                     }
  61.                 }
  62.             }
  63.         },
  64.         interactivity: {
  65.             detectsOn: "canvas",
  66.             events: {
  67.                 resize: true
  68.             }
  69.         },
  70.         detectRetina: true,
  71.         background: {
  72.             color: "#000000"
  73.         },
  74.         emitters: [
  75.             {
  76.                 direction: "top",
  77.                 particles: {
  78.                     color: "#f00"
  79.                 },
  80.                 rate: {
  81.                     quantity: 1,
  82.                     delay: 0.1
  83.                 },
  84.                 size: {
  85.                     width: 100,
  86.                     height: 10
  87.                 },
  88.                 position: {
  89.                     x: 50,
  90.                     y: 100
  91.                 }
  92.             },
  93.             {
  94.                 direction: "top",
  95.                 particles: {
  96.                     color: "#0f0"
  97.                 },
  98.                 rate: {
  99.                     quantity: 1,
  100.                     delay: 0.1
  101.                 },
  102.                 size: {
  103.                     width: 100,
  104.                     height: 10
  105.                 },
  106.                 position: {
  107.                     x: 50,
  108.                     y: 100
  109.                 }
  110.             }]
  111.     });
  112.     //# sourceURL=pen.js
  113. </script>
复制代码
结果

5.3 ts粒子鼠标吸引力

  1. <div style="width: 100%;height: 200px;background-color: rgb(109, 108, 106);" id="tsparticles1"></div>
  2. <script src="https://cdn.jsdelivr.net/npm/tsparticles@1.37.4/tsparticles.min.js"></script>
  3. <script id="rendered-js">
  4.     tsParticles.load("tsparticles1", {
  5.         fps_limit: 60,
  6.         interactivity: {
  7.             detect_on: "canvas",
  8.             events: {
  9.                 onclick: { enable: true, mode: "push" },
  10.                 onhover: {
  11.                     enable: true,
  12.                     mode: "attract",
  13.                     parallax: { enable: false, force: 60, smooth: 10 }
  14.                 },
  15.                 resize: true
  16.             },
  17.             modes: {
  18.                 push: { quantity: 4 },
  19.                 attract: { distance: 200, duration: 0.4, factor: 5 }
  20.             }
  21.         },
  22.         particles: {
  23.             color: { value: "#ffffff" },
  24.             line_linked: {
  25.                 color: "#ffffff",
  26.                 distance: 150,
  27.                 enable: true,
  28.                 opacity: 0.4,
  29.                 width: 1
  30.             },
  31.             move: {
  32.                 attract: { enable: false, rotateX: 600, rotateY: 1200 },
  33.                 bounce: false,
  34.                 direction: "none",
  35.                 enable: true,
  36.                 out_mode: "out",
  37.                 random: false,
  38.                 speed: 2,
  39.                 straight: false
  40.             },
  41.             number: { density: { enable: true, value_area: 800 }, value: 80 },
  42.             opacity: {
  43.                 anim: { enable: false, opacity_min: 0.1, speed: 1, sync: false },
  44.                 random: false,
  45.                 value: 0.5
  46.             },
  47.             shape: {
  48.                 character: {
  49.                     fill: false,
  50.                     font: "Verdana",
  51.                     style: "",
  52.                     value: "*",
  53.                     weight: "400"
  54.                 },
  55.                 image: {
  56.                     height: 100,
  57.                     replace_color: true,
  58.                     src: "images/github.svg",
  59.                     width: 100
  60.                 },
  61.                 polygon: { nb_sides: 5 },
  62.                 stroke: { color: "#000000", width: 0 },
  63.                 type: "circle"
  64.             },
  65.             size: {
  66.                 anim: { enable: false, size_min: 0.1, speed: 40, sync: false },
  67.                 random: true,
  68.                 value: 5
  69.             }
  70.         },
  71.         polygon: {
  72.             draw: { enable: false, lineColor: "#ffffff", lineWidth: 0.5 },
  73.             move: { radius: 10 },
  74.             scale: 1,
  75.             type: "none",
  76.             url: ""
  77.         },
  78.         retina_detect: true
  79.     });
  80. </script>
复制代码
结果

5.4 粒子烟花

  1. <script src="https://cdn.jsdelivr.net/npm/tsparticles-fireworks@2.11.0/tsparticles.fireworks.bundle.min.js"></script>
  2. <script id="rendered-js">
  3.    fireworks();
  4. </script>
复制代码
结果

源码地点

https://gitcode.net/my12/particle
完结

赠人玫瑰,手有余香!如果文章内容对你有所帮助,请不要吝啬你的点赞评论和关注,以便我第一时间收到反馈,你的每一次支持都是我不停创作的最大动力。固然如果你发现了文章中存在错误大概有更好的解决方法,也欢迎评论私信告诉我哦!
好了,我是向宇,https://xiangyu.blog.csdn.net
一位在小公司冷静奋斗的开发者,出于兴趣爱好,于是迩来才开始自习unity。如果你碰到任何问题,也欢迎你评论私信找我, 虽然有些问题我大概也不肯定会,但是我会查阅各方资料,争取给出最好的发起,盼望可以帮助更多想学编程的人,共勉~


来源:https://blog.csdn.net/qq_36303853/article/details/132318746
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则