Skip to content

Instantly share code, notes, and snippets.

@videlais
Created March 16, 2014 20:19
Show Gist options
  • Save videlais/9589285 to your computer and use it in GitHub Desktop.
Save videlais/9589285 to your computer and use it in GitHub Desktop.
determineFontHeightInPixels -- proposed fix for Pixi.js
/*
* http://stackoverflow.com/posts/13730758/revisions
*
* @method determineFontHeightInPixels
* @param fontStyle {String}
* @private
*/
PIXI.Text.prototype.determineFontHeightInPixels = function(fontStyle)
{
var result = PIXI.Text.heightCache[fontStyle];
if (!result)
{
var fontDraw = document.createElement("canvas");
var ctx = fontDraw.getContext('2d');
ctx.fillRect(0, 0, fontDraw.width, fontDraw.height);
ctx.textBaseline = 'top';
ctx.fillStyle = 'white';
ctx.font = fontStyle;
ctx.fillText('gM', 0, 0);
var pixels = ctx.getImageData(0, 0, fontDraw.width, fontDraw.height).data;
var start = -1;
var end = -1;
for (var row = 0; row < fontDraw.height; row++)
{
for (var column = 0; column < fontDraw.width; column++)
{
var index = (row * fontDraw.width + column) * 4;
if (pixels[index] === 0) {
if (column === fontDraw.width - 1 && start !== -1) {
end = row;
row = fontDraw.height;
break;
}
continue;
}
else
{
if (start === -1)
{
start = row;
}
break;
}
}
}
result = end - start;
PIXI.Text.heightCache[fontStyle] = result;
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment