From 00664f9e99f67a4646d1e3718dad6c3c39c9fb57 Mon Sep 17 00:00:00 2001 From: beeglebug Date: Mon, 4 Nov 2013 21:29:32 +0000 Subject: [PATCH] Phaser.Game parent can now be a HTMLElement --- src/system/Canvas.js | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/system/Canvas.js b/src/system/Canvas.js index a2b3b977..bdcf2cb8 100644 --- a/src/system/Canvas.js +++ b/src/system/Canvas.js @@ -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; },