Get started with particle-api-js CDN

Apache-2.0 licensed

Particle-API-JS is a library for interacting with the Particle Cloud Platform.

Tags:
  • particle
  • library
  • spark
  • api

Stable version

Copied!

How to start using particle-api-js CDN


<!DOCTYPE html>
<html>
<head>
  <title>Get started with particle-api-js CDN - cdnhub.io</title>
  <script src="https://cdn.cdnhub.io/particle-api-js/10.4.2/particle.min.js"></script>
</head>
<body>
  <script>
    // Initialize the Particle.js library with your Particle Access Token
    const particle = new p5(p => {
      p.setup = () => {
        p.createCanvas(400, 400);
        p.particles = [];

        for (let i = 0; i < 50; i++) {
          p.particles.push(new p5.Particle());
        }

        p.colorMode(p.HSL, 360, 100, 100, 100);
      };

      p.draw = () => {
        p.background(p.mapValue(p.mouseX, 0, p.width, 0, 360));

        for (let i = 0; i < p.particles.length; i++) {
          p.particles[i].follow(p.mousePosition, 1);
          p.particles[i].update();
          p.particles[i].show();
        }
      };

      p.Particle = class {
        constructor() {
          this.pos = p.createVector(p.random(p.width), p.random(p.height));
          this.vel = p.createVector();
          this.acc = p.createVector();
          this.size = 4;
          this.lifespan = 100;
        }

        follow(target, maxSpeed) {
          this.acc.x = p.lerp(this.acc.x, p.sub(target, this.pos).x, 0.1);
          this.acc.y = p.lerp(this.acc.y, p.sub(target, this.pos).y, 0.1);
          this.vel.add(this.acc);
          this.vel.limit(maxSpeed);
          this.update();
        }

        update() {
          this.pos.add(this.vel);
          this.lifespan -= 1;
        }

        show() {
          const hue = p.mapValue(this.lifespan, 0, 100, 0, 360);
          p.stroke(hue, 100, 100, 100);
          p.noFill();
          p.ellipse(this.pos.x, this.pos.y, this.size, this.size);
        }
      };
    });
  </script>
</body>
</html>
Copied!
Copied!

All versions