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

              
                <canvas id="canvas"></canvas>

<aside style="display:none;">
  <img class="texture" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/neon-star6.png?v=1" crossorigin="anonymous" />
  <img class="texture" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/neon-star7.png?v=1" crossorigin="anonymous" />
  <img class="texture" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/neon-star8.png?v=1" crossorigin="anonymous" />
  <img class="texture" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/neon-star9.png?v=1" crossorigin="anonymous" />
  <img class="texture" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/neon-star10.png?v=1" crossorigin="anonymous" />
</aside>


              
            
!

CSS

              
                body {
  background: #000;
}

#canvas {
  position: fixed;
  background: #000;
}
              
            
!

JS

              
                console.clear();
var log = console.log.bind(console);

var toRad = Math.PI / 180;

var vw = window.innerWidth;
var vh = window.innerHeight;

var resolution = window.devicePixelRatio || 1;
var textures = document.querySelectorAll(".texture");

// Visible canvas
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d", { alpha: false });

// Offscreen canvas
var buffer = document.createElement("canvas");
var bufferContext = buffer.getContext("2d", { alpha: false });

var supportsFilters = (typeof context.filter !== "undefined");

var sprites = [];

window.addEventListener("load", init);

function init() {
  
  resize();
    
  for (var i = 0; i < 400; i++) {
    var texture = sample(textures);
    sprites.push(createSprite(texture));
  }
  
  requestAnimationFrame(function() {
    TweenLite.ticker.addEventListener("tick", render);
  });
}

function render() {
  
  context.fillStyle = "rgba(0,0,0,0.35)";
  context.fillRect(0, 0, vw, vh);  
  bufferContext.clearRect(0, 0, vw, vh);
    
  for (var i = 0; i < sprites.length; i++) {
    
    var sprite = sprites[i];
    
    // Skip rendering sprite if it has no alpha
    if (!sprite.alpha) {
      continue;
    }
    
    bufferContext.save();
  
    var offsetX = sprite.originX * sprite.width;
    var offsetY = sprite.originY * sprite.height;

    bufferContext.translate(sprite.x + offsetX, sprite.y + offsetY);
    bufferContext.rotate(sprite.rotation * toRad);
    bufferContext.scale(sprite.scale, sprite.scale);

    bufferContext.globalAlpha = sprite.alpha;
    bufferContext.drawImage(sprite.texture, -offsetX, -offsetY);

    bufferContext.restore();    
  }  
  
  if (supportsFilters) {
    
    // Draw main canvas on self with blur
    context.filter = "blur(25px) brightness(1.4)";
    context.drawImage(canvas, 0, 0);
        
    // Draw buffer on main canvas with blur
    context.filter = "blur(15px) brightness(1.4)";
    context.drawImage(buffer, 0, 0);
    
    // Draw buffer on main canvas with drop shadow
    context.filter = "drop-shadow(2px 2px 0px rgba(0,0,0,1)) contrast(150%)";
    context.drawImage(buffer, 0, 0);
    context.filter = "none";
    
  } else {
    context.drawImage(buffer, 0, 0);
  }  
}

function createSprite(texture) {
  
  var width  = (texture.naturalWidth  || texture.width  || 0) / resolution;
  var height = (texture.naturalHeight || texture.height || 0) / resolution;
  
  var isOutlier = chanceRoll(20);
    
  var tl = new TimelineMax({
    delay: random(4),
    repeat: -1,
    repeatDelay: isOutlier ? random(1, 4) : random(1)
  });
  
  var sprite = {
    animation: tl,
    texture: texture,
    width: width,
    height: height,
    alpha: 0,
    rotation: random(360),
    scale: random(0.5, 1.25),
    originX: 0.5,
    originY: 0.5,
    x: vw / 2 - width / 2,
    y: vh + height,
  };
  
  var angle = random(260, 280);
  var sign = angle < 270 ? -1: 1;
  var rotation = random(360, 1800) * sign;
  var velocity = isOutlier ? random(750, 900) : random(500, 750);
  var duration = isOutlier ? random(4, 5) : random(2, 4);
  
  tl.set(sprite, { alpha: 1, immediateRender: false })
    .to(sprite, duration, {
      alpha: 0,
      rotation: rotation,
      scale: random(0.25, 1),
      physics2D: {
        velocity: velocity,
        angle: angle,
        gravity: 500,
      }
    });
  
  return sprite;
}

function resize() {
  
  vw = window.innerWidth;
  vh = window.innerHeight;

  buffer.width  = canvas.width  = vw * resolution;
  buffer.height = canvas.height = vh * resolution;

  canvas.style.width  = vw + "px";
  canvas.style.height = vh + "px";

  context.scale(resolution, resolution);
  bufferContext.scale(resolution, resolution);
}

function sample(collection) {
  var index = Math.floor(Math.random() * collection.length);
  return collection[index];
}

function chanceRoll(chance) {
  if (chance === undefined) { chance = 50; }
  return chance > 0 && (Math.random() * 100 <= chance);
}

function random(min, max) {
  if (max == null) { max = min; min = 0; }
  if (min > max) { var tmp = min; min = max; max = tmp; }
  return min + (max - min) * Math.random();
}
              
            
!
999px

Console