Merge pull request #170 from beeglebug/selectors

Phaser.Game parent can now be a HTMLElement
This commit is contained in:
Richard Davey
2013-11-12 16:26:42 -08:00
+20 -14
View File
@@ -137,37 +137,43 @@ Phaser.Canvas = {
*
* @method Phaser.Canvas.addToDOM
* @param {HTMLCanvasElement} canvas - The canvas to set the touch action on.
* @param {string} parent - The DOM element to add the canvas to. Defaults to ''.
* @param {string|HTMLElement} parent - The DOM element to add the canvas to. Defaults to ''.
* @param {boolean} overflowHidden - If set to true it will add the overflow='hidden' style to the parent DOM element.
* @return {HTMLCanvasElement} Returns the source canvas.
*/
addToDOM: function (canvas, parent, overflowHidden) {
parent = parent || '';
var target;
if (typeof overflowHidden === 'undefined') { overflowHidden = true; }
if (parent !== '')
if (parent)
{
if (document.getElementById(parent))
// hopefully an element ID
if (typeof parent === 'string')
{
document.getElementById(parent).appendChild(canvas);
target = document.getElementById(parent);
}
// quick test for a HTMLelement
else if (typeof parent === 'object' && parent.nodeType === 1)
{
target = parent;
}
if (overflowHidden)
{
document.getElementById(parent).style.overflow = 'hidden';
}
}
else
if (overflowHidden)
{
document.body.appendChild(canvas);
target.style.overflow = 'hidden';
}
}
else
// fallback, covers an invalid ID and a none HTMLelement object
if(!target)
{
document.body.appendChild(canvas);
target = document.body;
}
target.appendChild(canvas);
return canvas;
},