Skip to content

Commit 00664f9

Browse files
committed
Phaser.Game parent can now be a HTMLElement
1 parent 3e9777e commit 00664f9

1 file changed

Lines changed: 20 additions & 14 deletions

File tree

src/system/Canvas.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -137,37 +137,43 @@ Phaser.Canvas = {
137137
*
138138
* @method Phaser.Canvas.addToDOM
139139
* @param {HTMLCanvasElement} canvas - The canvas to set the touch action on.
140-
* @param {string} parent - The DOM element to add the canvas to. Defaults to ''.
140+
* @param {string|HTMLElement} parent - The DOM element to add the canvas to. Defaults to ''.
141141
* @param {boolean} overflowHidden - If set to true it will add the overflow='hidden' style to the parent DOM element.
142142
* @return {HTMLCanvasElement} Returns the source canvas.
143143
*/
144144
addToDOM: function (canvas, parent, overflowHidden) {
145145

146-
parent = parent || '';
146+
var target;
147147

148148
if (typeof overflowHidden === 'undefined') { overflowHidden = true; }
149149

150-
if (parent !== '')
150+
if (parent)
151151
{
152-
if (document.getElementById(parent))
152+
// hopefully an element ID
153+
if (typeof parent === 'string')
153154
{
154-
document.getElementById(parent).appendChild(canvas);
155-
156-
if (overflowHidden)
157-
{
158-
document.getElementById(parent).style.overflow = 'hidden';
159-
}
155+
target = document.getElementById(parent);
156+
}
157+
// quick test for a HTMLelement
158+
else if (typeof parent === 'object' && parent.nodeType === 1)
159+
{
160+
target = parent;
160161
}
161-
else
162+
163+
if (overflowHidden)
162164
{
163-
document.body.appendChild(canvas);
165+
target.style.overflow = 'hidden';
164166
}
165167
}
166-
else
168+
169+
// fallback, covers an invalid ID and a none HTMLelement object
170+
if(!target)
167171
{
168-
document.body.appendChild(canvas);
172+
target = document.body;
169173
}
170174

175+
target.appendChild(canvas);
176+
171177
return canvas;
172178

173179
},

0 commit comments

Comments
 (0)