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="canvas"></canvas>

<div class="controls">
  <button id="toggle-button">Toggle</button>
</div>

<aside style="display:none;">
  <img class="texture" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/sparkle.png?v=123" crossorigin="anonymous">
</aside>
              
            
!

CSS

              
                body {
  background: #000;
}

button {
  padding: 8px 12px;
  cursor: pointer;
}

#canvas {
  position: fixed;
  background: #0084cc;
  z-index: 0;
}

.controls {
  position: fixed;
  top: 0;
  left: 0;
  padding: 12px;
  z-index: 1;
}
              
            
!

JS

              
                
class Emitter {
  
  constructor() {
    
    this.x = 0;
    this.y = 0;
    this.isActive = false;
    this.sprites = [];  
  }
  
  init(textures, numParticles) {
    
    this.sprites = [];  
    
    var last = textures.length - 1;
  
    for (var i = 0; i < numParticles; i++) {
      var texture = textures[randomInt(last)];
      this.sprites.push(createSprite(texture, this));
    }
  }
  
  start() {
    
    this.isActive = true;
    
    for (var i = 0; i < this.sprites.length; i++) {      
      var sprite = this.sprites[i];
      sprite.timer.restart(true);
    }
  }
  
  stop() {
    
    this.isActive = false;
    
    for (var i = 0; i < this.sprites.length; i++) {      
      var sprite = this.sprites[i];
      sprite.timer.pause();
    }
  }
  
  draw(context) {
    
    for (var i = 0; i < this.sprites.length; i++) {
    
      var sprite = this.sprites[i];

      // Skip rendering sprite if it has no alpha
      if (!sprite.alpha) {
        continue;
      }

      context.save();

      var offsetX = sprite.originX * sprite.width;
      var offsetY = sprite.originY * sprite.height;

      // context.translate(sprite.x + offsetX, sprite.y + offsetY);
      context.translate(sprite.emitX, sprite.emitY);
      
      context.rotate(sprite.rotation * toRad);
      context.scale(sprite.scale, sprite.scale);

      context.globalAlpha = sprite.alpha;
      
      // context.drawImage(sprite.texture, -offsetX, -offsetY);
      context.drawImage(sprite.texture, -offsetX + sprite.x, -offsetY + sprite.y);

      context.restore();    
    }    
  }
}

var vw = window.innerWidth;
var vh = window.innerHeight;
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var resolution = window.devicePixelRatio || 1;
var sprites = [];
window.addEventListener("load", init);
var textures = document.querySelectorAll(".texture");
var toRad = Math.PI / 180;

var toggleButton = document.querySelector("#toggle-button");

var emitter = new Emitter();

function init() {
  
  resize();
  
  emitter.x = vw / 2;
  emitter.y = vh / 2;
  emitter.init(textures, 100);
  emitter.start();
  
//   var last = textures.length - 1;
  
//   for (var i = 0; i < 20; i++) {
//     var texture = textures[randomInt(last)];
//     sprites.push(createSprite(texture));
//   }
  
  toggleButton.addEventListener("click", toggleEmitter);
  window.addEventListener("mousemove", onMouseMove);
  
  TweenLite.ticker.addEventListener("tick", render);
}


function createSprite(texture, emitter) {
    
  var width  = (texture.naturalWidth  || texture.width  || 0) / resolution;
  var height = (texture.naturalHeight || texture.height || 0) / resolution;
      
  var tl = new TimelineMax({
    // delay: random(4),
    repeat: -1,
    repeatDelay: random(1),
    paused: true,
  });
  
  var timer = TweenLite.delayedCall(random(4), function() {
    tl.resume();
  }).pause();
  
  var sprite = {
    animation: tl,
    texture: texture,
    width: width,
    height: height,
    alpha: 0,
    scale: random(0.5, 1),
    originX: 0.5,
    originY: 0.50,
    x: 0,
    y: 0,
    
    emitX: emitter.x,
    emitY: emitter.y,
    updatePosition: updatePosition,
    tl: tl,
    timer: timer,
  };
    
  tl.add(updatePosition)
    .set(sprite, { alpha: 1, immediateRender: false })
    .to(sprite, random(2, 4), {
      rotation: 0,
      scale: random(1),
      alpha: 0,
      physics2D: {
        velocity: random(100, 150),
        angle: random(250, 290),
        gravity: 200,
      }
    })
    .add(function() {
    
      if (!emitter.isActive) {
        tl.pause();
      }
    })
  
  function updatePosition() {
    
    if (sprite.emitX !== emitter.x || sprite.emitY !== emitter.y) {
      sprite.emitX = emitter.x;
      sprite.emitY = emitter.y;
    }
  }
  
  return sprite;
}

function onMouseMove(e) {
  emitter.x = e.clientX;
  emitter.y = e.clientY;
}

function toggleEmitter() {
  
  if (emitter.isActive) {
    emitter.stop();
  } else {
    emitter.start();
  }
}

function render() {
  
  context.clearRect(0, 0, vw, vh);
  
  emitter.draw(context);
}

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

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

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

  context.scale(resolution, resolution);
}

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();
}

function randomInt(min, max) {
  if (max == null) { max = min; min = 0; }
  if (min > max) { var tmp = min; min = max; max = tmp; }
  return Math.floor(min + (max - min + 1) * Math.random());
}





              
            
!
999px

Console