Jump to content

Pass created canvas to Phaser.Game to reuse it


Recommended Posts



I want to take control of the canvas the game is rendered but when I specify the element ID to Phaser.Game parameter, it is creating a new canvas inside it.


If I have this:

    <div id="taskContainer"></div>

And I do this:

    new Phaser.Game(200, 400, Phaser.AUTO, "taskContainer");

Phaser is creating the canvas and adding to the div like this:

    <div id="taskContainer">        <canvas></canvas>    </div>
OK, but I want to specify the canvas, if I have this:
    <div>        <canvas id="taskContainer"></canvas>    </div>

With the same code, Phase is doing this crazy thing:

    <div>        <canvas id="taskContainer">            <canvas></canvas>        </canvas>    </div>

How can I say to Phaser I want to use the canvas I'm setting by itself and not as a parent container???



Link to comment
Share on other sites

Do you need to create the canvas yourself? If not, the canvas is available at Phaser.Game#canvas.


If you do need to create it yourself instead of modifying it after Phaser makes it, I bet you could instantiate the Game then re-assign the canvas and context properties.

Link to comment
Share on other sites

I don't know that this will work. Let's say you already have a reference to the canvas, maybe from document.getElementById or wherever.

var game = new Phaser.Game(640, 480, Phaser.CANVAS);game.canvas = canvas;game.context = canvas.getContext('2d');


Link to comment
Share on other sites

Maybe it should work, I will try, but that way Phaser is creating a canvas and then rewriting it, is bad for performance.

Is not possible to tell Phaser "ey dude, this is the canvas I have for you, don't create one!"? Is a pity!

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...