123

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.

            
              <div id="container"><canvas id="canvas"></canvas></div>
<a href="https://s.codepen.io/jscottsmith/debug/dwZjWK" rel="noopener" target="_blank" class="fullscreen">
    <svg version="1.1" x="0px" y="0px" width="30px" height="30px" viewBox="0 0 30 30">
        <polygon fill="#FFF" opacity="0.75" points="16.5,15 23.1,8.3 26,11.2 26,4 18.8,4 21.7,6.9 15,13.5 8.3,6.9 11.2,4 4,4 4,11.2 6.9,8.3 13.5,15  6.9,21.7 4,18.8 4,26 11.2,26 8.3,23.1 15,16.5 21.7,23.1 18.8,26 26,26 26,18.8 23.1,21.7 	"/>
    </svg>
</a>
            
          
!
            
              html,
body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    touch-action: none;
    cursor: none;
}

#container {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}

.fullscreen {
    position: fixed;
    bottom: 1rem;
    right: 1rem;
}

            
          
!
            
              // Inspired by:
// http://fabiensanglard.net/doom_fire_psx/
const DPR = window.devicePixelRatio || 1;
const CELL_WIDTH = 8 * DPR;
const CELL_HEIGHT = 8 * DPR;
const FLAME_COLOR_DEPTH = 24;
const FLAME_COLOR_TABLE = [
    [0, 'lavender'],
    [0.1, 'yellow'],
    [0.3, 'gold'],
    [0.5, 'hotpink'],
    [0.6, 'tomato'],
    [0.8, 'darkslateblue'],
    [1, '#222'],
];
// direction the flame will spread from
const SPREAD_FROM = [
    'bottom',
    'bottom',
    'bottom',
    'bottom',
    'bottom',
    'bottom',
    'bottom',
    'bottom',
    'bottom',
    'bottom',
    'left',
    'left',
    'right',
    'right',
    'top',
];

function createGradientArray(size, colorStops) {
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');
    canvas.width = size;
    canvas.height = 1;

    const gradient = ctx.createLinearGradient(0, 0, size, 0);
    colorStops.forEach(args => gradient.addColorStop(...args));

    ctx.fillStyle = gradient;
    ctx.fillRect(0, 0, size, 1);

    return Array(size)
        .fill(null)
        .map((_, x) => {
            const data = ctx.getImageData(x, 0, 1, 1).data;
            return `rgb(${data[0]}, ${data[1]}, ${data[2]})`;
        });
}

class Pixel {
    constructor(x, y, arr, idx) {
        this.x = x;
        this.y = y;
        this.idx = idx;
        this.arr = arr;
    }

    setSides({ top, left, bottom, right }) {
        this.top = top;
        this.left = left;
        this.bottom = bottom;
        this.right = right;
    }

    draw = ({ ctx }) => {
        const fill = this.arr[this.idx];
        ctx.fillStyle = fill;
        ctx.fillRect(this.x, this.y, CELL_WIDTH, CELL_HEIGHT);
    };

    update = () => {
        const side = SPREAD_FROM[getRandomInt(0, SPREAD_FROM.length - 1)];
        const dest = this[side];

        // check if it can dest to designated side
        if (dest && dest.idx < this.idx) {
            const rand = getRandomInt(-1, 4);
            this.idx = dest.idx + rand;
        } else {
            this.idx += 1;
        }

        // resets if overflow
        if (this.idx > this.arr.length - 1) {
            this.idx = this.arr.length - 1;
        } else if (this.idx < 0) {
            this.idx = 0;
        }
    };
}

class Matrix {
    createMatrix({ bounds }) {
        const colors = createGradientArray(FLAME_COLOR_DEPTH, FLAME_COLOR_TABLE);

        this.rows = Math.ceil(bounds.h / CELL_HEIGHT);
        this.columns = Math.ceil(bounds.w / CELL_WIDTH);
        const table = Array(this.rows).fill(Array(this.columns).fill(null));

        this.pixels = table.map((row, y) =>
            row.map(
                (col, x) =>
                    new Pixel(
                        x * CELL_WIDTH,
                        y * CELL_HEIGHT,
                        colors,
                        y >= this.rows - 2 ? 0 : colors.length - 1
                    )
            )
        );

        for (let row = 0; row < this.rows; row++) {
            for (let col = 0; col < this.columns; col++) {
                const bounds = {
                    top: this.pixels[row - 1] && this.pixels[row - 1][col],
                    left: this.pixels[row][col - 1],
                    bottom: this.pixels[row + 1] && this.pixels[row + 1][col],
                    right: this.pixels[row][col + 1],
                };

                this.pixels[row][col].setSides(bounds);
            }
        }
    }

    setup = (context) => this.createMatrix(context);

    resize = (context) => this.createMatrix(context);

    draw = ({ ctx, pointer, tick, bounds }) => {
        let { x, y } = pointer.position;
        let pointCol, pointRow;
        if (x !== null && y !== null) {
            // pointer
            pointCol = Math.floor(x / CELL_WIDTH);
            pointRow = Math.floor(y / CELL_HEIGHT);
        } else {
            // demo circle
            const z = tick / 10;
            const s = bounds.h / 4;
            const { center: c } = bounds;
            pointCol = Math.floor((c.x + Math.sin(z) * s) / CELL_WIDTH);
            pointRow = Math.floor((c.y + Math.cos(z) * s) / CELL_HEIGHT);
        }

        for (let row = 0; row < this.rows; row++) {
            for (let col = 0; col < this.columns; col++) {
                const pixel = this.pixels[row][col];
                if (pointRow === row && pointCol === col) {
                    pixel.idx = 0;
                }

                pixel.draw({ ctx });
                pixel.update();
            }
        }
    };
}

const canvas = new Canvas({
    canvas: document.getElementById('canvas'),
    container: document.getElementById('container'),
    hasPointer: true,
    pauseInBackground: true,
    entities: [new Matrix()],
});


            
          
!
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