cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              <canvas id="world"></canvas>
            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Lobster);


html, body {
    margin: 0px;
    padding: 0px;
    overflow: hidden;
}
            
          
!
            
              // 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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console