Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!--
  Click and drag to attract
  Right click to repulse
  Mouse-wheel click to create a time dilation field
  Use the Controls to decrease or increase
  the particle count to tweak performance.
-->
<canvas id="swarm"></canvas>
              
            
!

CSS

              
                body {
  margin: 0px;
}

body canvas {
  display: block;
  cursor: crosshair;
}
              
            
!

JS

              
                window.addEventListener('load', function() {
  var rctx = new SmallPRNG(+new Date()), // random generator, see ref
      p = new Perlin(), // simplex noise generator
      canvas = document.getElementById("swarm"),
      context = canvas.getContext("2d"),
      monitor = new MouseMonitor(canvas),
      gui = new dat.GUI(),
      hue = 0, particles = [], resize,
      width, height, bounds = new Vector3D(0, 0, 0),
      settings = {
        particleNum: 5000,
        fadeOverlay: true,
        rotateColor: true,
        staticColor: {r: 0, g: 75, b: 50},
        staticColorString: 'rgba(0, 75, 50, 0.55)'
      };
  
  // dat.gui stuff, 2 folders with a few properties
  var f1 = gui.addFolder('Particles'),
      f2 = gui.addFolder('Colors');
  
  f1.add(settings, 'particleNum', 1000, 15000).step(10).name("Particles").onChange(function() {
    if(settings.particleNum < particles.length) {
      var toDelete = (particles.length - settings.particleNum);
      particles.splice(particles.length - toDelete, toDelete);
    } else {
      for(var i = particles.length; i < settings.particleNum; i += 1) {
        particles.push(new Particle(p, bounds, rctx, monitor));
      }
    }
  });
  
  f2.add(settings, 'fadeOverlay').name("Fade Clear").onChange(function() {
    if(settings.fadeOverlay) {
      resize();
    }
  });
  
  f2.add(settings, 'rotateColor').name("Rotate Color");
  f2.addColor(settings, 'staticColor').name("Static Color").onChange(function() {
    settings.staticColorString = 'rgba(' + 
      Math.floor(settings.staticColor.r) + ', ' + 
      Math.floor(settings.staticColor.g) + ', ' + 
      Math.floor(settings.staticColor.b) + ', ' + .55 + ')';
  });
  
  f1.open();
  f2.open();
  gui.close();
  
  // seed perlin with random bytes from SmallPRNG
  p.init(function() {
    // called for each permutation (256 times)
    return rctx.random(0, 255);
  });

  resize = function() {
    // resize the canvas
    canvas.width  = width  = bounds.x = window.innerWidth;
    canvas.height = height = bounds.y = window.innerHeight;
    
    // remove this and see weird gorgeous stuffs, the history of particles.
    context.fillStyle = '#ffffff';
    context.fillRect(0, 0, width, height);
  }; resize();

  window.addEventListener('resize', resize); 

  // generate a few particles
  for(var i = 0; i < settings.particleNum; i += 1) {
    particles.push(new Particle(p, bounds, rctx, monitor));
  }
  
  +(function render() {
    requestAnimFrame(render);

      context.beginPath();
        // render each particle and trail
        for(var i = 0; i < particles.length; i += 1) {
          particles[i].step(), particles[i].render(context);
        }

        context.globalCompositeOperation = 'source-over';
        if(settings.fadeOverlay) {
          context.fillStyle = 'rgba(0, 0, 0, .085)';
        } else {
          context.fillStyle = 'rgba(0, 0, 0, 1)';
        }
        context.fillRect(0, 0, width, height);

        context.globalCompositeOperation = 'lighter';
        if(settings.rotateColor) {
          context.strokeStyle = 'hsla(' + hue + ', 75%, 50%, .55)';
        } else {
          context.strokeStyle = settings.staticColorString;
        }
        context.stroke();
      context.closePath();    
    hue = ((hue + .5) % 360);
  }());
});
              
            
!
999px

Console