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

              
                <canvas id="world"></canvas>
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Lobster);


html, body {
    margin: 0px;
    padding: 0px;
    overflow: hidden;
}
              
            
!

JS

              
                // RequestAnimationFrame
(function() {
    var lastTime = 0;
    var vendors = ['ms', 'moz', 'webkit', 'o'];
    for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
        window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
        window.cancelRequestAnimationFrame = window[vendors[x]+
          'CancelRequestAnimationFrame'];
    }

    if (!window.requestAnimationFrame)
        window.requestAnimationFrame = function(callback, element) {
            var currTime = new Date().getTime();
            var timeToCall = Math.max(0, 16 - (currTime - lastTime));
            var id = window.setTimeout(function() { callback(currTime + timeToCall); },
              timeToCall);
            lastTime = currTime + timeToCall;
            return id;
        };

    if (!window.cancelAnimationFrame)
        window.cancelAnimationFrame = function(id) {
            clearTimeout(id);
        };
}());

// Vector
var Vector = function(x, y) {

  return {

    x: x || 0,

    y: y || 0,

    sAdd: function(v1, v2) {
      return new Vector(v1.x + v2.x, v1.y + v2.y);
    },

    sSub: function(v1, v2) {
      return new Vector(v1.x - v2.x, v1.y - v2.y);
    },

    add: function(vector) {
      this.x += vector.x;
      this.y += vector.y;
    },

    sub: function(vector) {
      this.x -= vector.x;
      this.y -= vector.y;
    },

    mult: function(number) {
      this.x *= number;
      this.y *= number;
    },

    div: function(n) {
      this.x /= n;
      this.y /= n;
    },

    mag: function() {
      return Math.sqrt(this.x * this.x + this.y * this.y);
    },

    normalize: function() {
      m = this.mag();
      if (m !== 0) {
        this.div(m);
      }
    },

    limit: function(max) {

      if (this.mag() > max) {
        this.normalize();
        this.mult(max);
      }
    }

  };

};

// Particle
var Particle = function() {

  var _velocity = new Vector();
  var _force = new Vector();

  this.position = new Vector();
  this.radius = 3;

  this.addForce = function(vector) {
    _force.x += vector.x;
    _force.y += vector.y;
  };

  this.reset = function() {
    _force.x = 0;
    _force.y = 0;
  };

  this.addRandomness = function() {
    this.position.x += -1 + Math.random() * 2;
    this.position.y += -1 + Math.random() * 2;
  };

  this.update = function(ctx) {

    _velocity.add(_force);
    _velocity.limit(15);
    this.position.add(_velocity);

    this.addRandomness();

    this.reset();

    if (this.position.x + this.radius * 0.5 >= window.innerWidth) {
      _velocity.x *= -1;
    }

    if (this.position.y + this.radius * 0.5 >= window.innerHeight) {
      _velocity.y *= -1;
    }

    if (this.position.x + this.radius * 0.5 <= 0) {
      _velocity.x *= -1;
    }

    if (this.position.y + this.radius * 0.5 <= 0) {
      _velocity.y *= -1;
    }

    ctx.fillStyle = '#fff';
    ctx.beginPath();
    ctx.arc(
        this.position.x,
        this.position.y,
        this.radius,
        0,
        Math.PI * 2,
        true
    );
    ctx.closePath();
    ctx.stroke();
    ctx.fill();



  };

};

// Application
var Application = (function() {

  var TOTAL_PARTICLES = 200;
  var WINDOW_WIDTH = 0;
  var WINDOW_HEIGHT = 0;

  var _canvas;
  var _context;
  var sVector = new Vector();
  var _mouse = new Vector();

  var _particles = [];
  var _self;

  var _wind = new Vector();
  var _gravity = new Vector(0, 0.0001);
  var _acceleration = new Vector(-1, 1);

  return {

    init: function() {

      _self = this;

      //Get canvas
      _canvas = document.getElementById('world');
      _context = _canvas.getContext('2d');

      if (_context) {
        this._resize();
        
        _mouse.x = WINDOW_WIDTH * 0.5;
        _mouse.y = WINDOW_HEIGHT * 0.5;
        
        this._createForces();
        this._createParticles();
        this._addEventListeners();
        window.requestAnimationFrame(this._draw);

      }

    },

    _createForces: function() {

    },

    /**
    * Registers all the event listeners
    * @author Thodoris Tsiridis
    */
    _addEventListeners: function() {
      $(window).bind('resize', this._resize);
      $(window).bind('mousemove', this._onMouseMove);
    },

    _createParticles: function() {

      var i = TOTAL_PARTICLES;
      var particle;

      for (; i >= 0; i--) {
        particle = new Particle();
        particle.position.x = Math.floor(Math.random() * WINDOW_WIDTH);
        particle.position.y = Math.floor(Math.random() * WINDOW_HEIGHT);
        _particles.push(particle);
      }

    },

    _onMouseMove: function(event) {
      _mouse.x = event.clientX;
      _mouse.y = event.clientY;
    },

    _draw: function() {

      var i = TOTAL_PARTICLES;

      //_context.clearRect(0,0,window.innerWidth, window.innerHeight);
      _context.fillStyle = 'rgba(0,0,0,0.2)';
      _context.fillRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);

      for (; i >= 0; i--) {

        _acceleration = sVector.sSub(_mouse, _particles[i].position);
        _acceleration.normalize();
        //_acceleration.mult(0.5);
        _particles[i].addForce(_acceleration);
        _particles[i].update(_context);
      }

      window.requestAnimationFrame(_self._draw);
    },

    _resize: function() {
      _canvas.width = WINDOW_WIDTH = window.innerWidth;
      _canvas.height = WINDOW_HEIGHT = window.innerHeight;
    }
  };

});

var app = new Application();
app.init();

              
            
!
999px

Console