css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

            
              /**
    The 'playing field' or 'view target', a class which holds a list of
    renderables. Objects of this class have a reference to a canvas element. 
    All renderables will be drawn onto this canvas.
*/
;(function(root) {
    'use strict';
    
    // A few type-testing functions
    var is  = function(v,t) {return (typeof(v) === t);};
    var iss = function(v)   {return is(v, 'string');};
    var isf = function(v)   {return is(v, 'function')&&v instanceof Function;};
    
    /*
        A simple compatibility test, to see if the canvas element is supported
        in this browser. The variable canvasSupported will hold true if support
        for Canvas is available and false if this browser does not support Canvas.
    */
    var canvasSupported = (function () {
        var elem = document.createElement('canvas');
        return !!(elem.getContext && elem.getContext('2d'));
    }());
    
    // A custom exception which can be thrown when an error occurs.
    var CanvasException = function(message) {
        this.name = "CanvasException";
        this.message = message;
    };
    
    /**
        The Canvas constructor, which expects an HTML element referring to a canvas.
    */
    var Canvas = function(element) {
        if(!canvasSupported) {
            // Throw the CanvasException, no support for Canvas is available
            throw new CanvasException("The HTML5 Canvas element is not supported in this browser.");
        }
        
        // Test if the provided element actually is a canvas.
        var elementIsCanvas = !!(element.getContext && element.getContext('2d'));
        if(!elementIsCanvas) {
            // Throw the CanvasException, the element is not a canvas
            throw new CanvasException("The provided element is not a canvas element.");
        }
        
        // Store the reference to the DOM-element and the canvas 2d context.
        this.element = element;
        this.context = element.getContext('2d');
        
        // keep a list of renderable items!
        this.renderables = [];
        this.renderFPS   = true;
    };
    
    /**
        Add a renderable object to the render list. All of these items will automatically
        be stepped and rendered when the Canvas step() and render() methods are called.
    */
    Canvas.prototype.addRenderable = function(renderable) {
        if(typeof(renderable) !== "object" || !(renderable instanceof Renderable)) {
            throw new TypeError("Invalid renderable provided");
        }
        
        this.renderables.push(renderable);
    };
    
    /**
        Call function `f` with a reference to the Canvas instance as `this` 
        parameter and the 2d context as argument. The function can then 
        safely create variables without placing garbage in the calling scope.
    */
    Canvas.prototype.with2d = function(f) {
        if(!isf(f)) {
            throw new TypeError("Invalid function provided");
        }
        
        f.call(this, this.context);
    };
    
    /**
        Step all the renderables, allowing them to do their logic.
    */
    Canvas.prototype.step = function() {
        for(var i in this.renderables) {
            if(this.renderables.hasOwnProperty(i)) {
                this.renderables[i].step(this);
            }
        }
    };
    
    /**
        Calculate the frames per second each 50 milliseconds, only if
        renderFPS is set to true!
    */  
    Canvas.prototype.calculateFPS = function() {
        if(this.renderFPS) {
            if(!this.lct) {
                this.lct = Date.now();
                this.fps = 0;
                this.lut = null;
            }

            var now = Date.now();
            if(!this.lut || (now - this.lut) >= 50) {
                var delta = (new Date().getTime() - this.lct);
                this.fps = (1000 / delta);
                this.lut = now;
            }

            this.lct = now;

            if(!isFinite(this.fps)) {
                this.fps = 0;
            }
        }
    };
    
    /**
        Render all the renderables, like they want to be rendered!
    */
    Canvas.prototype.render = function() {
        this.calculateFPS();
        
        this.with2d(function(ctx) {
            this.clear();

            // all objects will have a shadow! :)
            ctx.shadowBlur = 20;
            ctx.shadowColor = "black";

            for(var i in this.renderables) {
                if(this.renderables.hasOwnProperty(i)) {
                    this.renderables[i].render(ctx);   
                }
            }
            
            if(this.renderFPS) {
                ctx.font = '10px arial';
                ctx.fillStyle = '#ffffff';
                ctx.fillText(Math.ceil(this.fps) + ' FPS', 10, 15);
            }
        });
    };
    
    /**
        Clear the canvas area
    */
    Canvas.prototype.clear = function() {
        this.context.clearRect(0, 0, this.element.width, this.element.height);
    };
    
    /**
        Resize the canvas drawing area and element size
    */
    Canvas.prototype.resize = function(width, height) {
        this.element.style.width = width + 'px';
        this.element.style.height = height + 'px';
        this.context.canvas.width = width;
        this.context.canvas.height = height;
    };
    
    /**
        Return the current size of the canvas drawing area
    */
    Canvas.prototype.getSize = function() {
        return {
            width: this.context.canvas.width,
            height: this.context.canvas.height
        };
    };
    
    // export the canvas support boolean to Canvas and the classes to `root`
    Canvas.support       = canvasSupported;
    root.CanvasException = CanvasException;
    root.Canvas          = Canvas;
}(window));
            
          
!
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.
Loading ..................

Console