项目作者: codegewerk

项目描述 :
A lightweight, dependency-free and responsive javascript plugin for particle backgrounds.
高级语言: TypeScript
项目地址: git://github.com/codegewerk/particle-cloud.git
创建时间: 2020-09-02T13:10:16Z
项目社区:https://github.com/codegewerk/particle-cloud

开源协议:MIT License

下载


particle-cloud

GitHub license

A lightweight, dependency-free and responsive javascript plugin for particle backgrounds.
This is a fork of particles.js.
Compared to the archived original project, this library is written in typescript, fixes several bugs and includes some significant performance improvements.
React server-side rendering, and therefore also usage within Gatsby, is supported out of the box.

Install the library via npm:

  1. npm install --save @codegewerk/particle-cloud

Alternatively, the minified package can be used directly via a CDN.

  1. <script src="https://unpkg.com/@codegewerk/particle-cloud@1.x/dist/particles.min.js"></script>

Minimal Example

Screenshot

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <style>
  6. html,
  7. body {
  8. margin: 0;
  9. padding: 0;
  10. height: 100%;
  11. }
  12. .background {
  13. position: absolute;
  14. display: block;
  15. top: 0;
  16. left: 0;
  17. z-index: 0;
  18. height: 100%;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <canvas class="background"></canvas>
  24. <script src="https://unpkg.com/@codegewerk/particle-cloud@1.x/dist/particles.min.js"></script>
  25. <script>
  26. const instance = new ParticleCloud({
  27. connectParticles: true,
  28. selector: ".background",
  29. });
  30. instance.start();
  31. </script>
  32. </body>
  33. </html>

Usage with npm

You need to import the ParticleCloud class before using the library.

  1. import ParticleCloud from "@codegewerk/particle-cloud";
  2. const instance = new ParticleCloud({
  3. connectParticles: true,
  4. selector: ".background",
  5. });
  6. instance.start();

Usage with React

  1. import React, { useEffect } from "react";
  2. import ParticleCloud from "@codegewerk/particle-cloud";
  3. export default function ParticleCloudCanvas() {
  4. useEffect(() => {
  5. const instance = new ParticleCloud({
  6. speed: 0.2,
  7. maxParticles: 100,
  8. selector: ".particles",
  9. color: ["#f58220", "#d28645", "#dddddd"],
  10. connectParticles: true,
  11. });
  12. instance.start();
  13. return () => instance.destroy();
  14. }, []);
  15. return <canvas className="particles"></canvas>;
  16. }

Options

Key Type Default Description
selector string - Required: The CSS selector of your canvas element
maxParticles number 100 Optional: Amount of particles
sizeVariations number 3 Optional: Maximum size of each particle
speed number 0.5 Optional: Movement speed of each particle
color string or string[] #000000 Optional: Color(s) of the particles and connecting lines
minDistance number 120 Optional: Distance in px for connecting lines
connectParticles boolean false Optional: true/false if connecting lines should be drawn or not
responsive BreakpointEntry[] [] Optional: Array of objects containing breakpoints and options

BreakpointEntry

When any breakpoint options are specified, the library tries to find the smallest breakpoint with is still larger than the
current screen size.
If there is not such breakpoint, the library will fall back to the general settings.
If there is a suitable breakpoint, the relevant options from the general settings are overwritten with the breakpoint specific
options.

Key Type Default Description
breakpoint number - Required: The breakpoint value
options BreakpointOptions - Required: The breakpoint options

Each BreakpointOptions has the following fields:

Key Type Default Description
maxParticles number undefined Optional: Amount of particles
sizeVariations number undefined Optional: Maximum size of each particle
speed number undefined Optional: Movement speed of each particle
color string or string[] undefined Optional: Color(s) of the particles and connecting lines
minDistance number undefined Optional: Distance in px for connecting lines
connectParticles boolean undefined Optional: true/false if connecting lines should be drawn or not

Example Configuration

  1. import ParticleCloud from "@codegewerk/particle-cloud";
  2. const instance = new ParticleCloud({
  3. selector: ".background",
  4. maxParticles: 1000,
  5. connectParticles: true,
  6. responsive: [
  7. {
  8. breakpoint: 300,
  9. options: {
  10. color: "#ff0000",
  11. maxParticles: 200,
  12. },
  13. },
  14. {
  15. breakpoint: 600,
  16. options: {
  17. color: "#00ff00",
  18. maxParticles: 600,
  19. },
  20. },
  21. ],
  22. });