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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <canvas id="game"></canvas>
            
          
!
            
              * { margin: 0; padding: 0;}

body, html {width:100%; height:100%;}

canvas {display:block;}

            
          
!
            
              var canvas;
var stage;
var width = 650;
var height = 400;
var particles = [];
var max = 60;
var mouseX=0;
var mouseY=0;

var speed=3;
var size=20;

//The class we will use to store particles. It includes x and y
//coordinates, horizontal and vertical speed, and how long it's
//been "alive" for.
function Particle(x, y, xs, ys) {
  this.x=x;
  this.y=y;
  this.xs=xs;
  this.ys=ys;
  this.life=0;
}

function resizeCanvas() {
  setTimeout(function() {
    width = window.innerWidth;
    height = window.innerHeight;
    canvas.width = width;
    canvas.height = height;
    canvas.style.width = width + "px";
    canvas.style.height = height + "px";
    mouseX=canvas.width/2;
    mouseY=canvas.height*0.8;
   stage.globalCompositeOperation="lighter"
  }, 0);
}

function init() {
  
  //Reference to the HTML element
  canvas=document.getElementById("game");
  
  resizeCanvas();
  
  //See if the browser supports canvas
  if (canvas.getContext) {
    
    //Get the canvas context to draw onto
    stage = canvas.getContext("2d");
    
    //Makes the colors add onto each other, producing
    //that nice white in the middle of the fire
    stage.globalCompositeOperation="xor";
    
    //Update the mouse position
    canvas.addEventListener("mousemove", getMousePos);
    
    window.addEventListener("resize", function() {
      resizeCanvas();
      stage.globalCompositeOperation="lighter";
      mouseX=canvas.width/2;
      mouseY=canvas.height*0.8;
    });
    
    //Update the particles every frame
    var timer=setInterval(update,40);
    
  } else {
    alert("Canvas not supported.");
  }
}

function getMousePos (evt) {
  var rect = canvas.getBoundingClientRect();
  var root = document.documentElement;
  
  // return mouse position relative to the canvas
  mouseX = evt.clientX - rect.left - root.scrollLeft;
  mouseY = evt.clientY - rect.top - root.scrollTop;
}

function update() {

  //Adds ten new particles every frame
  for (var i=0; i<10; i++) {
    
    //Adds a particle at the mouse position, with random horizontal and vertical speeds
    var p = new Particle(mouseX, mouseY, (Math.random()*2*speed-speed)/2, 0-Math.random()*2*speed);
    particles.push(p);
  }
  
  //Clear the stage so we can draw the new frame
  stage.clearRect(0, 0, width, height);
  
  //Cycle through all the particles to draw them
  for (i=0; i<particles.length; i++) {
    
    //Set the file colour to an RGBA value where it starts off red-orange, but progressively gets more grey and transparent the longer the particle has been alive for
    stage.fillStyle = "rgba("+(260-(particles[i].life*2))+","+((particles[i].life*2)+50)+","+(particles[i].life*2)+","+(((max-particles[i].life)/max)*0.4)+")";
    
    stage.beginPath();
    //Draw the particle as a circle, which gets slightly smaller the longer it's been alive for
    stage.arc(particles[i].x,particles[i].y,(max-particles[i].life)/max*(size/2)+(size/2),0,2*Math.PI);
    stage.fill();
    
    //Move the particle based on its horizontal and vertical speeds
    particles[i].x+=particles[i].xs;
    particles[i].y+=particles[i].ys;
    
    particles[i].life++;
    //If the particle has lived longer than we are allowing, remove it from the array.
    if (particles[i].life >= max) {
      particles.splice(i, 1);
      i--;
    }
  }
}

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.

Console