File: src/EmPx/empx.js
/**
Converts a given amount of pixels to the corresponding amount of Ems.
var a = glesea.toEm({px: 42});
// a ==> 2.625
@method toEm
@for glesea
@param {DOM node} context The context to measure px/em
@param {Number} px
@return {Number} Amount of Ems
**/
glesea.toEm = function(options) {
options = glesea.defaultize({
px: 10
}, options);
if (!options.hasOwnProperty('context')) {
options.context = document.querySelector('body');
}
var theDiv = document.createElement('div');
theDiv.style.position = 'absolute';
theDiv.style.left = '-99999px';
theDiv.style.top = '-99999px';
theDiv.style['font-size'] = '1em';
theDiv.style.margin = 0;
theDiv.style.padding = 0;
theDiv.style.height = 'auto';
theDiv.style['line-height'] = 1;
theDiv.style.border = 0;
theDiv.innerHTML = ' ';
options.context.appendChild(theDiv);
var value = theDiv.offsetHeight;
theDiv.parentNode.removeChild(theDiv);
return Number((options.px / value).toFixed(8));
};
/**
Converts a given amount of Ems to the corresponding amount of Pixels.
var a = glesea.toPx({em: 42});
// a ==> 672
@method toPx
@for glesea
@param {DOM node} context The context to measure px/em
@param {Number} em
@return {Number} Amount of pixels
**/
glesea.toPx = function(options) {
options = glesea.defaultize({
em: 10
}, options);
if (!options.hasOwnProperty('context')) {
options.context = document.querySelector('body');
}
var theDiv = document.createElement('div');
theDiv.style.position = 'absolute';
theDiv.style.left = '-99999px';
theDiv.style.top = '-99999px';
theDiv.style['font-size'] = '1em';
theDiv.style.margin = 0;
theDiv.style.padding = 0;
theDiv.style.height = 'auto';
theDiv.style['line-height'] = 1;
theDiv.style.border = 0;
theDiv.innerHTML = ' ';
options.context.appendChild(theDiv);
var value = theDiv.offsetHeight;
theDiv.parentNode.removeChild(theDiv);
return Number((options.em * value).toFixed(8));
};