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>
<button>Fire</button>

              
            
!

CSS

              
                html,
body {
  min-height: 100%;
}
body {
  padding: 0;
  margin: 0;
}

button {
  position: fixed;
  bottom: 1em;
  margin-left: 50%;
  transform: translateX( -50% );
}
              
            
!

JS

              
                var canvas = document.getElementById('canvas');
var button = document.querySelector('button');

var ctx = canvas.getContext('2d');

var bullets = [];
var asteroids = [];
var start = null;
var deltaTime = null;
var asteroidSizeModifier = 5;

var step = function( timestamp ) {
  if (!start) start = timestamp;
  var progress = timestamp - start;
  
  // Stash Canvas Height/Width
  var h = canvas.height;
  var w = canvas.width;

  // Clear the canvas
  ctx.clearRect(0, 0, w, h);
  
  // Draw the background
  ctx.fillStyle = "#1a1a1a";
  ctx.fillRect(0, 0, w, h);
  ctx.save();                  
  
  // Randomize the Asteroids
  var rando = Math.floor(Math.random() * 150 );
  if(rando == 0) {
    var randomHP = Math.ceil(Math.random() * 10);
    asteroids.push( {
      x: Math.floor(Math.random()* w),
      y: 0,
      radius: randomHP *  asteroidSizeModifier,
      hp: randomHP
    });
  }

  // Move the Bullets
  for(var i = 0; i< bullets.length; i++ ) {
    var bullet = bullets[i];
    bullet.y -= 2;
    
    if( bullet.y < 0 ) {
      bullets.splice(i, 1);
    }
  }
  
  // Move the Asteroids
  for(var i = 0; i< asteroids.length; i++ ) {
    var asteroid = asteroids[i];
    asteroid.y += 1;
    
    if( asteroid.y > h ) {
      asteroids.splice(i, 1);
    }
  }
  
  // Draw the bullets
  for(var i = 0; i< bullets.length; i++ ) {
    var bullet = bullets[i];
    
    ctx.beginPath();
    ctx.fillStyle = '#0CF';
    ctx.arc(
      bullet.x, 
      bullet.y, 
      bullet.radius, 
      0, 
      2 * Math.PI);
    ctx.fill();
  }
  
  
  // Draw the Asteroids
  for(var i = 0; i< asteroids.length; i++ ) {
    var asteroid = asteroids[i];
    
    ctx.beginPath();
    ctx.strokeStyle = '#0CF';
    ctx.arc(
      asteroid.x, 
      asteroid.y, 
      asteroid.radius, 
      0, 
      2 * Math.PI);
    ctx.stroke();

  }

  // Re-draw the Scene
  requestAnimationFrame(step);
}

// Make the canvas responsive?
var setCanvasSize = function() {
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
}

setCanvasSize();
window.addEventListener('resize', setCanvasSize );

// Make the button create bullets
button.addEventListener('click', function(e) {
  e.preventDefault();
  
  // A bullet is really just an object in an array
  bullets.push( { 
    x: window.innerWidth/2, 
    y: window.innerHeight,
    radius: 3 
  } );
} );

// Start the animation
window.requestAnimationFrame(step);

              
            
!
999px

Console