如何在 JavaScript 项目中使用 Particle.js?

javascriptweb developmentfront end technology

顾名思义,Particle.js 库允许我们将粒子添加到特定的 HTML 元素。此外,我们可以更改 div 中粒子的形状和数量。

我们可以使用 Particle.js 库为粒子添加动画或运动。在这里,我们将通过不同的示例学习如何更改粒子的形状、颜色和运动。

语法

用户可以按照以下语法在 JavaScript 项目中使用 Particle.js 库。

tsParticles.load("id", {
    particles: {
    	// properties for the particles
    }
});

在上述语法中,id 表示我们需要添加粒子的 div 元素的 id。

示例

在下面的示例中,我们在 <head> 标记中使用了 CDN 的 Particles.JS 库。我们创建了 <div> 元素并在 HTML 主体中分配了 id。

在 JavaScript 中,我们添加了 tsparticles.load() 方法来加载粒子。作为 load() 方法的第一个参数,我们传递了 div 元素的 id。我们将对象作为包含粒子属性的第二个参数传递。

<html>
<head>
   <script src = "https://cdnjs.cloudflare.com/ajax/libs/tsparticles/1.18.11/tsparticles.min.js"> </script>
   <style>
      #tsparticles {
         width: 100%;
         height: 100%;
         background-color: grey;
      }
   </style>
</head>
<body>
   <div id = "tsparticles">
      <h2> Using the <i> particle.js library </i> in the JavaScript project. </h2>
   </div>
   <script>
      tsParticles.load("tsparticles", {
         particles: {
            number: {
               value: 500
            },
         }
      });
   </script>
</body>
</html> 

在输出中,用户可以观察 div 元素中的粒子。

示例

下面的示例将为粒子添加运动和颜色。用户可以使用 move 属性来移动粒子。'move' 属性将值作为包含具有布尔值的 enabled 属性的对象。

<html>
<head>
   <script src = "https://cdnjs.cloudflare.com/ajax/libs/tsparticles/1.18.11/tsparticles.min.js"> </script>
   <style>
      #particles {
         width: 100%;
         height: 100%;
         background-color: lightgreen;
      }
   </style>
</head>
<body>
   <div id = "particles"> 
      <h2> Using the <i> particle.js library </i> in the JavaScript project. </h2>
   </div>
   <script>
      tsParticles.load("particles", {
         particles: {
            number: {
               value: 1000
            },
            move: {
               enable: true
            },
            color: {
               value: "#272701"
            },
         }
      });
   </script>
</body>
</html>

用户可以使用 colour 属性来更改包含对象的粒子的颜色作为值。

用户可以使用 Particle.JS 库中以下形状的粒子。

  • "polygon"

  • "triangle"

  • "circle"

  • "edge" / "square"

  • "image" / "images"

  • "star"

  • "char" / "character"

示例

在下面的示例中,我们为粒子添加了多边形形状。此外,我们还使用 size 属性更改了粒子的大小。此外,我们还为粒子本身添加了动画,增加和减少用户可以在输出中观察到的粒子的大小。

<html>
<head>
   <script src = "https://cdnjs.cloudflare.com/ajax/libs/tsparticles/1.18.11/tsparticles.min.js"> </script>
   <style>
      #particles {
         width: 100%;
         height: 100%;
         background-color: lightgreen; 
      }
   </style>
</head>
<body>
   <div id = "particles">
      <h2>Using the <i> particle.js library </i> in the JavaScript project. </h2>
   </div>
   <script>
      tsParticles.load("particles", {
         particles: {
            number: {
               value: 1000
            },
            move: {
               enable: true
            },
            color: {
               value: "#ff0342"
            },

            // adding shape of particles
            shape: {
               type: "polygon",
            },

            // changing the size of elements
            size: {
               value: 8,
               random: true, 
               animation: {
                  enable: true,
                  speed: 40,
                  sync: true
               },
               move: {
                  enable: true,
               },
            }
         }
      });
   </script>
</body>
</html>

用户学习了如何在 JavaScript 项目中使用 Particles.js 库。但是,用户可以在本地计算机上安装 Particle.js 库并使用路径导入。每当用户想要将 Particle.js 库与 NodeJS 应用程序一起使用时,他们都应该使用 NPM 命令进行安装。


相关文章