phaser/src/dom/AddToDOM.js

57 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
2017-10-12 14:09:52 +00:00
/**
2018-01-26 03:40:49 +00:00
* Adds the given element to the DOM. If a parent is provided the element is added as a child of the parent, providing it was able to access it.
* If no parent was given or falls back to using `document.body`.
2017-10-12 14:09:52 +00:00
*
* @function Phaser.Dom.AddToDOM
* @since 3.0.0
*
2018-01-26 03:40:49 +00:00
* @param {object} element - The element to be added to the DOM. Usually a Canvas object.
* @param {string|object} [parent] - The parent in which to add the element. Can be a string which is passed to `getElementById` or an actual DOM object.
2017-10-12 14:09:52 +00:00
* @param {boolean} [overflowHidden=true] - [description]
*
2018-01-26 03:40:49 +00:00
* @return {object} The element that was added to the DOM.
2017-10-12 14:09:52 +00:00
*/
var AddToDOM = function (element, parent, overflowHidden)
{
if (overflowHidden === undefined) { overflowHidden = true; }
var target;
if (parent)
{
if (typeof parent === 'string')
{
// Hopefully an element ID
target = document.getElementById(parent);
}
else if (typeof parent === 'object' && parent.nodeType === 1)
{
// Quick test for a HTMLelement
target = parent;
}
}
// Fallback, covers an invalid ID and a non HTMLelement object
if (!target)
{
target = document.body;
}
if (overflowHidden && target.style)
{
target.style.overflow = 'hidden';
}
target.appendChild(element);
return element;
};
module.exports = AddToDOM;