cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              * {
  margin: 0;
  padding: 0;
}

html {
  height: 100%;
}

body {
  min-height: 100%;
}
            
          
!
            
              var config = {
  // BEGIN TEMPLATE CONFIG
  // - HTML id attribute of canvas element
  id_canvas: "canvas",
  size: {
    width: 500,
    height: 500
  },
  // If only one draw is needed
  animation: false,
  // - enable/disable FPS indicator
  show_fps: true,
  // - Delay between FPS indicator updates
  fps_update_delay: 500,
  // END TEMPLATE CONFIG
  // ...
};

// Initialize everything here
function Init() {
  // TODO ...
}

// Update everything
// dt: ms since last tick
function Tick(dt) {
  // TODO ...
}

// Draw everything
// ctx: CanvasRenderingContext2D object
function Draw(ctx) {
  // TODO ...
}

// #################################
// TEMPLATE LOGIC

var __canvas = null, __prevTick = null,
    __fps = null, __fps_d = 0, __fps_inc = 0;
function __Init() {
  __canvas = document.getElementById(config.id_canvas);
  __canvas.setAttribute("width", config.size.width);  __canvas.setAttribute("height", config.size.height);
  config.show_fps = config.show_fps && config.animation;
  // BEGIN FPS
  if (config.show_fps) {
    // INILE STYLE TO KEEP CSS EDITOR CLEAN
    __fps = document.createElement("div");
    __fps.style.position = "fixed";
    __fps.style.left = 0;
    __fps.style.top = 0;
    __fps.style.padding = "2px 5px";
    __fps.style.backgroundColor = "rgba(0,0,0,.5)";
    __fps.style.color = "#FFF";
    __fps.style.fontFamily = "monospace";
    __fps.innerHTML = "FPS"
    document.body.append(__fps);
  }
  // END FPS
  Init();
  window.requestAnimationFrame(__Tick);
}
// Only called when browser is about to redraw
// requestAnimationFrame automatically provide the current timestamp
function __Tick(timestamp) {
  if (!config.animation) {
    console.info("animation disabled (won't call Tick)");
    __Draw();
    return;
  }
  if (__prevTick === null) {
    __prevTick = timestamp;
  }
  var dt = timestamp - __prevTick;
  // BEGIN FPS
  if (config.show_fps) {
    __fps_inc += dt;
    __fps_d++;
    if (__fps_inc > config.fps_update_delay) {
      __fps.innerHTML = Math.round(__fps_d * (1000 / __fps_inc));
      __fps_inc = 0, __fps_d = 0;
    }
  }
  // END FPS
  Tick(dt);
  __Draw();
  __prevTick = timestamp;
  window.requestAnimationFrame(__Tick);
}
function __Draw() {
  Draw(__canvas.getContext("2d"));
}
window.addEventListener("load", __Init);
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console