Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>2DCanvas Images</title>
    <script src="https://mrdoob.github.io/stats.js/build/stats.min.js"></script>
</head>
<body>
<canvas id="canvas" width="1920" height="1080"></canvas>
</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                /* 
This is a modified version of code from webgl2fundamentals.org to test 2D Canvas performance.
Read more here: https://demyanov.dev/past-and-future-html-canvas-brief-overview-2d-webgl-and-webgpu
*/
let then = 0;
let app;

const stats = new Stats();
stats.showPanel(1);
document.body.appendChild(stats.dom);

class App {
    gl = null;
    units = [];
    imagesCount = 300;

    constructor() {
        const canvas = document.querySelector("#canvas");
        const ctx = canvas.getContext("2d");
        this.canvas = canvas;
        this.ctx = ctx;

        const textureInfos = [
            this.loadImageAndCreateTextureInfo('https://demyanov.dev/images/articles/tmnt_01.png'),
            this.loadImageAndCreateTextureInfo('https://demyanov.dev/images/articles/tmnt_02.png'),
            this.loadImageAndCreateTextureInfo('https://demyanov.dev/images/articles/tmnt_03.png'),
            this.loadImageAndCreateTextureInfo('https://demyanov.dev/images/articles/tmnt_04.png'),
            this.loadImageAndCreateTextureInfo('https://demyanov.dev/images/articles/tmnt_05.png'),
            this.loadImageAndCreateTextureInfo('https://demyanov.dev/images/articles/tmnt_06.png'),
        ];

        for (let i = 0; i < this.imagesCount; i++) {
            const speedMin = 30;
            const speedMax = 200;
            const speed = Math.random() * (speedMax - speedMin) + speedMin;

            const drawInfo = {
                x: Math.random() * canvas.width,
                y: Math.random() * canvas.height,
                dx: Math.random() > 0.5 ? -1 : 1,
                dy: Math.random() > 0.5 ? -1 : 1,
                image: textureInfos[Math.random() * textureInfos.length | 0],
            };

            this.units.push(
                new Unit(speed, drawInfo)
            );
        }
    }

    loadImageAndCreateTextureInfo(url) {
        const img = new Image();

        const textureInfo = {
            width: 0,
            height: 0,
            image: img,
        };
        img.src = url;

        img.onload = () => {
            // resolve();
            textureInfo.width = img.width;
            textureInfo.height = img.height;
            textureInfo.image = img;
        };

        return textureInfo;
    }

    loop(time) {
        const now = time * 0.001;
        const deltaTime = Math.min(0.1, now - then);
        then = now;

        stats.begin();

        app.clearCanvas(app.ctx);
        app.units.forEach(function (unit) {
            unit.update(deltaTime, app.ctx.canvas.width, app.ctx.canvas.height);
            unit.draw(app.ctx);
        });

        stats.end();

        requestAnimationFrame(app.loop);
    }

    init() {
        requestAnimationFrame(this.loop);
    }

    clearCanvas(ctx) {
        ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);

    }
}

window.onload = function () {
    app = new App();
    app.init();
}

class Unit {
    speed = 0;
    drawInfo = null;

    constructor(speed, drawInfo) {
        this.speed = speed;
        this.drawInfo = drawInfo;
    }

    draw(ctx) {
        ctx.drawImage(
            this.drawInfo.image.image,
            this.drawInfo.x,
            this.drawInfo.y,
            this.drawInfo.image.width,
            this.drawInfo.image.height,
        );
    }

    update(deltaTime, width, height) {
        this.drawInfo.x += this.drawInfo.dx * this.speed * deltaTime;
        this.drawInfo.y += this.drawInfo.dy * this.speed * deltaTime;
        if (this.drawInfo.x < 0) {
            this.drawInfo.dx = 1;
        }
        if (this.drawInfo.x >= width) {
            this.drawInfo.dx = -1;
        }
        if (this.drawInfo.y < 0) {
            this.drawInfo.dy = 1;
        }
        if (this.drawInfo.y >= height) {
            this.drawInfo.dy = -1;
        }
    }
}
              
            
!
999px

Console