Skip to content

Instantly share code, notes, and snippets.

@videlais
Created March 16, 2014 20:01
Show Gist options
  • Save videlais/9588989 to your computer and use it in GitHub Desktop.
Save videlais/9588989 to your computer and use it in GitHub Desktop.
determineFontHeight from Pixi.js
/* @license
* pixi.js - v1.5.1
* Copyright (c) 2012-2014, Mat Groves
* http://goodboydigital.com/
*
* pixi.js is licensed under the MIT License.
* http://www.opensource.org/licenses/mit-license.php
*
* http://stackoverflow.com/users/34441/ellisbben
* great solution to the problem!
* returns the height of the given font
*
* @method determineFontHeight
* @param fontStyle {Object}
* @private
*/
PIXI.Text.prototype.determineFontHeight = function(fontStyle)
{
var result = PIXI.Text.heightCache[fontStyle];
if (!result)
{
var body = document.getElementsByTagName('body')[0];
var dummy = document.createElement('div');
var dummyText = document.createTextNode('M');
dummy.appendChild(dummyText);
dummy.setAttribute('style', fontStyle + ';position:absolute;top:0;left:0');
body.appendChild(dummy);
result = dummy.offsetHeight;
PIXI.Text.heightCache[fontStyle] = result;
body.removeChild(dummy);
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment