Custom Canvas

Demonstrates how to provide an existing HTML5 Canvas element to the Phaser game configuration instead of allowing Phaser to create its own.

Custom Canvas, running
800 × 600
GitHub
class Example extends Phaser.Scene
{
    preload ()
    {
        this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
        this.load.image('pic', 'assets/pics/baal-loader.png');
    }

    create ()
    {
        this.add.image(400, 300, 'pic');
    }
}


const myCustomCanvas = document.createElement('canvas');

myCustomCanvas.id = 'myCustomCanvas';
myCustomCanvas.style = 'border: 8px solid red';

document.body.appendChild(myCustomCanvas);

// const myCustomContext = myCustomCanvas.getContext('2d');

const config = {
    scale: { mode: Phaser.Scale.FIT },
    type: Phaser.CANVAS,
    parent: 'game-container',
    width: 800,
    height: 600,
    canvas: document.getElementById('myCustomCanvas'),
    scene: Example
};

const game = new Phaser.Game(config);

More in Game Config19 examples