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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/stats.js/r11/Stats.min.js"></script>
<canvas id="snow-collision">
  Your browser doesnt support canvas-html5
</canvas>
<canvas id="flake-creator" class="hide">I can't create a snowflake :(</canvas>
<div id="debug"></div>
              
            
!

CSS

              
                body
{
  padding: 0px;
  margin: 0px;
  background-color: #0092B2;
}
#debug{ display: block; position: absolute; right: 0px; top: 0px; }
#snow-collision
{
  position: fixed;
  left: 0px;
  top: 0px;
  z-index: 0;
}
.hide{ display: none; }
              
            
!

JS

              
                $(document).ready(function(){
  window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       ||
          window.webkitRequestAnimationFrame ||
          window.mozRequestAnimationFrame    ||
          function( callback ){
            window.setTimeout(callback, 1000 / 60);
          };
  })();
  
  var canvas = $("#snow-collision")[0];
  var stats = new Stats();
  stats.setMode(0); // 0: fps, 1: ms
  var debug = $("#debug");
  // align top-left
  stats.domElement.style.position = 'absolute';
  stats.domElement.style.left = '0px';
  stats.domElement.style.top = '0px';

  document.body.appendChild( stats.domElement );
  
  var ctx    = canvas.getContext("2d");
  var wCanvas  = canvas.width = $(window).width(),
      hCanvas  = canvas.height = $(window).height();
  
  var snow_canvas = $("#flake-creator")[0],
      snow_ctx = snow_canvas.getContext("2d");
  
  $(window).resize(function(){
    wCanvas  = canvas.width = $(window).width();
    hCanvas  = canvas.height = $(window).height();
  });
  
  var isClicking = -1;
  $(document).on("mousedown mouseup touchstart touchend", function(e){
    var event = e.type;
    isClicking = (event == "mousedown" || event == "touchstart") ? 1 : -1;
  });
  
  var xMouse = 0,
      yMouse = 0;
  
  $(document).on("mousemove", function(e){
    xMouse = e.pageX;
    yMouse = e.pageY;
  }).on("touchstart", function(e){
    xMouse = changedTouches[0].pageX;
    yMouse = changedTouches[0].pageY;
  });
   
  var snowFlakes = [];
  
  for(var l = 0; l < 10000; l++)
 	{
  	  addParticle(getRandomRange(0, wCanvas), 10);
  }
  
  animate();
  var time = 0;
  function animate() {
    requestAnimFrame(animate);
    stats.begin();
    time++;
    var lSnow = snowFlakes.length;
    
    debug.text(lSnow + " snowflakes");
    
    var force = 2 * isClicking;
    
    var img = ctx.createImageData(wCanvas, hCanvas);
    var imgData = img.data;
    
    var lx = wCanvas / 2 + (Math.cos(time / 10) + Math.sin(time / 20)) * 100;
    var ly = hCanvas / 2 + (Math.sin(time / 10) + Math.cos(time / 20)) * 100;
    var r = Math.abs(Math.cos(time / 10)) * 40 + 10;
    for(var p = 0; p < lSnow; p++)
    {
    	var snow  = snowFlakes[p];
      snow.update(xMouse, yMouse, force, hCanvas);
      snow.draw(imgData, wCanvas, hCanvas, lx, ly, r);
    }
    
    ctx.putImageData(img, 0, 0);
    stats.end();
  }
  
  function Snowflake(x, y, size, imgData)
  {
    this.Image   			= imgData;
    this.setByteImage = function(i){ this.Image = i; };
    
    this.Size 				= size;
    this.setSize 			= function(s){ this.Size = s; };
    
    // Cords
    this.CordX    = x;
    this.CordY    = y;
    
    // Force
    this.VelocityX   	= 0;
    this.VelocityY   	= 0;
    
    this.update = function(x, y, force, h)
    { 
      // Velocity
      var xVelocity  = this.VelocityX;
      var yVelocity  = this.VelocityY;
			
      var xx = x - this.CordX;
      var yy = y - this.CordY;
      var distance = Math.sqrt(xx * xx + yy * yy);
      if(distance < 100)
      {
        xVelocity += xx / distance * force;
        yVelocity += yy / distance * force;
      }
      
      yVelocity += 0.5;
      yVelocity *= 0.9;
      xVelocity *= 0.9;
      
      this.VelocityX = xVelocity;
      this.VelocityY = yVelocity;
      this.CordX += ~~xVelocity;
      this.CordY += ~~yVelocity;
      if(this.CordY > h) this.recycle();
    };
    
    this.draw = function(data, sw, sh, lx, ly)
    {
      //Color By Light
      var x0 = this.CordX, y0 = this.CordY;
      var x1 = x0 + this.Size, y1 = y0 + this.Size;
      if(x0 < 0) 	x0 = 0;
      if(y0 < 0) 	y0 = 0;
      if(x1 > sw) x1 = sw;
      if(y1 > sh) y1 = sh;
			
      var dx = x0 - lx;
      var dy = y0 - ly;
      var c = 255 / (dx * dx + dy * dy) * 100;
      var rl = (c * 10) & 0xff,
          gl = (c * 20) & 0xff,
          bl = (c * 10) & 0xff;
      
      // Data
      var lmx 	= (sw - this.Size) << 2;
      var idCord 	= (x0 + y0 * sw) << 2;
      for(var j = y0; j < y1; j++)
      {
        for(var i = x0; i < x1; i++)
        { 
          data[idCord++] = rl;
          data[idCord++] = gl;
          data[idCord++] = bl;
          data[idCord++] = 255;
        }
        idCord += lmx;
      }
    };
    
    this.recycle = function()
    {
      this.VelocityX 	= this.VelocityY = 0;
      this.CordY  		= 20;
      this.CordX  		= getRandomRange(20, wCanvas - 20);
    };
  }
  
  function addParticle(x, y) 
  {
    var randomSize = Math.round(getRandomRange(5, 8)); 
    var newSnow    = new Snowflake(x, y, randomSize * 2, getRandomSnowflake(randomSize * 2));
    snowFlakes[snowFlakes.length] = newSnow;
  }
  function getRandomRange(min, max)
  { return Math.random() * (max - min) + min; }
  
  function getRandomSnowflake(size)
  {
    // https://codepen.io/zny/pen/QwEvMR?editors=001 Modified
    snow_canvas.width = snow_canvas.height = size;
    //snow_ctx.clearRect(0, 0, size, size);
    var spark = 50;
    // Realistic snowflakes, 
    // http://earthscience.stackexchange.com/questions/446/why-do-snowflakes-form-into-hexagonal-structures
    // thanks http://codereview.stackexchange.com/users/9357/200-success
    var randomArms = 6;
    var hs         = ~~(size / 2);
    var r          = size;
    var angle      = 2 * Math.PI / randomArms;
    var spShort    = Math.round(getRandomRange(0, 6)),
        spN        = Math.round(getRandomRange(0, 4));
    
    var spY        = Math.round(getRandomRange(0, r)), // y of first spark
        spYY       = Math.round(getRandomRange(0, r)), // y of last spark
        spH        = Math.round(getRandomRange(0, spark)), // height of sparks
        spDist     = spYY - spY;
    if (spDist > 0) 
    {
      spDist /= spN;
    }
    else 
    {
      spDist = (spY - spYY) /spN;
      spY = spYY;
      spYY = spY;
    }
    
    // Sparks
    var r2      = Math.random() * r, // shorter than main
        spY2    = Math.random() * r2, // y of spark
        spW2    = Math.random() * spark, // width of spark
        spH2    = Math.round(getRandomRange(-spark, spark * 2)), // height of spark
        spShort = Math.round(getRandomRange(0, 6)); // 1 in 6 chance of shorts not showing
    
    snow_ctx.translate(hs, hs);
    var grad = snow_ctx.createRadialGradient(0, 0, 0, 0, 0, r);
    grad.addColorStop("0","rgba(255,255,255,0.7)");
    grad.addColorStop("0.4","rgba(255,255,255,0.85)");
    grad.addColorStop("0.8","rgba(255,255,255,0.2)");
    grad.addColorStop("1","rgba(255,255,255,0)");
    
    // Large arms
    snow_ctx.beginPath();
    for (var i = 0; i < randomArms; i++) 
    {
      snow_ctx.moveTo(0, 0);
      snow_ctx.lineTo(0, r);
      snow_ctx.rotate(angle);
    }
    snow_ctx.lineWidth = 1;
    snow_ctx.strokeStyle = grad;
    snow_ctx.stroke();
    snow_ctx.closePath();
    
    // Short arms
    
    if (spShort > 1) 
    {
      snow_ctx.beginPath();
      snow_ctx.rotate(angle / 2);
      for (var i = 0; i < randomArms; i++) 
      {
        snow_ctx.moveTo(0, 0);
        snow_ctx.lineTo(0, r2);
        snow_ctx.rotate(angle);
      }
      snow_ctx.lineWidth = 1;
      snow_ctx.strokeStyle = grad;
      snow_ctx.stroke();
      snow_ctx.closePath();

      //short spark
      snow_ctx.beginPath();
      snow_ctx.moveTo(0, 0);
      for (var i = 0; i < randomArms; i++) 
      {
        snow_ctx.moveTo(-spW2, spY2 + spH2);
        snow_ctx.lineTo(0, spY2);
        snow_ctx.lineTo(spW2, spY2 + spH2);
        snow_ctx.rotate(angle);
      }
      snow_ctx.lineWidth = 1;
      snow_ctx.strokeStyle = grad;
      snow_ctx.stroke();
      snow_ctx.closePath();
    }
    return snow_ctx.getImageData(0, 0, size, size).data;
  }
});
              
            
!
999px

Console