Skip to content

Commit c741469

Browse files
committed
Added optional DOM Container parent and config values
1 parent 20f1b37 commit c741469

3 files changed

Lines changed: 85 additions & 0 deletions

File tree

src/boot/Config.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ var ValueToColor = require('../display/color/ValueToColor');
5050
* @property {integer} [timeout=0] - [description]
5151
*/
5252

53+
/**
54+
* @typedef {object} DOMContainerConfig
55+
*
56+
* @property {boolean} [createContainer=false] - Create a div element in which DOM Elements will be contained. You must also provide a parent.
57+
* @property {boolean} [behindCanvas=false] - Place the DOM Container behind the Phaser Canvas. The default is to place it over the Canvas.
58+
*/
59+
5360
/**
5461
* @typedef {object} GameConfig
5562
*
@@ -83,6 +90,7 @@ var ValueToColor = require('../display/color/ValueToColor');
8390
* @property {boolean} [banner.hidePhaser=false] - [description]
8491
* @property {string} [banner.text='#ffffff'] - [description]
8592
* @property {string[]} [banner.background] - [description]
93+
* @property {DOMContainerConfig} [dom] - The DOM Container configuration object.
8694
* @property {FPSConfig} [fps] - [description]
8795
* @property {boolean} [render.antialias=true] - [description]
8896
* @property {boolean} [render.pixelArt=false] - [description]
@@ -212,6 +220,18 @@ var Config = new Class({
212220
*/
213221
this.autoFocus = GetValue(config, 'autoFocus', true);
214222

223+
// DOM Element Container
224+
225+
/**
226+
* @const {?boolean} Phaser.Boot.Config#domCreateContainer - [description]
227+
*/
228+
this.domCreateContainer = GetValue(config, 'dom.createContainer', false);
229+
230+
/**
231+
* @const {?boolean} Phaser.Boot.Config#domBehindCanvas - [description]
232+
*/
233+
this.domBehindCanvas = GetValue(config, 'dom.behindCanvas', false);
234+
215235
// Input
216236

217237
/**

src/boot/CreateDOMContainer.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2018 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
var AddToDOM = require('../dom/AddToDOM');
8+
9+
var CreateDOMContainer = function (game)
10+
{
11+
var config = game.config;
12+
13+
if (!config.parent || !config.domCreateContainer)
14+
{
15+
return;
16+
}
17+
18+
var width = game.canvas.width;
19+
var height = game.canvas.height;
20+
21+
var z = (config.domBehindCanvas) ? 1 : 3;
22+
23+
// DOM Element Container
24+
var div = document.createElement('div');
25+
26+
div.style = 'width: ' + width + 'px; height: ' + height + 'px; padding: 0; margin: 0; position: absolute; overflow: hidden; pointer-events: none; z-index: ' + z;
27+
28+
// game.canvas.style.position = 'absolute';
29+
// game.canvas.style.zIndex = '2';
30+
// game.canvas.parentElement.style.position = 'relative';
31+
32+
game.domContainer = div;
33+
34+
AddToDOM(div, config.parent);
35+
};
36+
37+
module.exports = CreateDOMContainer;

src/boot/Game.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var CacheManager = require('../cache/CacheManager');
1010
var CanvasPool = require('../display/canvas/CanvasPool');
1111
var Class = require('../utils/Class');
1212
var Config = require('./Config');
13+
var CreateDOMContainer = require('./CreateDOMContainer');
1314
var CreateRenderer = require('./CreateRenderer');
1415
var DataManager = require('../data/DataManager');
1516
var DebugHeader = require('./DebugHeader');
@@ -68,6 +69,20 @@ var Game = new Class({
6869
*/
6970
this.renderer = null;
7071

72+
/**
73+
* A reference to an HTML Div Element used as a DOM Element Container.
74+
*
75+
* Only set if `createDOMContainer` is `true` in the game config (by default it is `false`) and
76+
* if you provide a parent element to insert the Phaser Game inside.
77+
*
78+
* See the DOM Element Game Object for more details.
79+
*
80+
* @name Phaser.Game#domContainer
81+
* @type {HTMLDivElement}
82+
* @since 3.12.0
83+
*/
84+
this.domContainer = null;
85+
7186
/**
7287
* A reference to the HTML Canvas Element that Phaser uses to render the game.
7388
* This is created automatically by Phaser unless you provide a `canvas` property
@@ -305,6 +320,8 @@ var Game = new Class({
305320

306321
CreateRenderer(this);
307322

323+
CreateDOMContainer(this);
324+
308325
DebugHeader(this);
309326

310327
AddToDOM(this.canvas, this.config.parent);
@@ -611,6 +628,12 @@ var Game = new Class({
611628
this.config.width = width;
612629
this.config.height = height;
613630

631+
if (this.domContainer)
632+
{
633+
this.domContainer.style.width = width + 'px';
634+
this.domContainer.style.height = height + 'px';
635+
}
636+
614637
this.renderer.resize(width, height);
615638

616639
this.input.resize();
@@ -666,6 +689,11 @@ var Game = new Class({
666689
}
667690
}
668691

692+
if (this.domContainer)
693+
{
694+
this.domContainer.parentNode.removeChild(this.domContainer);
695+
}
696+
669697
this.loop.destroy();
670698

671699
this.pendingDestroy = false;

0 commit comments

Comments
 (0)