(function () {
  var canvas = document.getElementById("particles");
  if (!canvas) return;
  var ctx = canvas.getContext("2d");
  var width,
    height,
    particles = [];

  function mainRgb() {
    var style = getComputedStyle(document.body);
    return style.getPropertyValue("--accent-main-rgb").trim() || "0, 255, 102";
  }

  function resize() {
    width = canvas.width = window.innerWidth;
    height = canvas.height = window.innerHeight;
  }

  function Particle() {
    this.x = Math.random() * width;
    this.y = Math.random() * height;
    this.vx = (Math.random() - 0.5) * 0.5;
    this.vy = (Math.random() - 0.5) * 0.5;
    this.size = Math.random() * 1.5;
  }
  Particle.prototype.update = function () {
    this.x += this.vx;
    this.y += this.vy;
    if (this.x < 0 || this.x > width) this.vx *= -1;
    if (this.y < 0 || this.y > height) this.vy *= -1;
  };
  Particle.prototype.draw = function () {
    ctx.fillStyle = "rgba(" + mainRgb() + ", 0.35)";
    ctx.beginPath();
    ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
    ctx.fill();
  };

  function animate() {
    ctx.clearRect(0, 0, width, height);
    particles.forEach(function (p) {
      p.update();
      p.draw();
    });
    requestAnimationFrame(animate);
  }

  resize();
  window.addEventListener("resize", resize);
  for (var i = 0; i < 100; i++) particles.push(new Particle());
  animate();
})();
