Created
May 20, 2016 16:45
-
-
Save evolutionxbox/135a02850a56ddce29ed9e44f673f5db to your computer and use it in GitHub Desktop.
Font Loaded fires an event when "FontAwesome" has been loaded.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Font Loaded | |
(function fontLoaded() { | |
var retries = 300; | |
function checkReady() { | |
var canvas, | |
context; | |
retries -= 1; | |
canvas = document.createElement('canvas'); | |
canvas.width = 20; | |
canvas.height = 20; | |
context = canvas.getContext('2d'); | |
context.fillStyle = 'rgba(0,0,0,1.0)'; | |
context.fillRect( 0, 0, 20, 20 ); | |
context.font = '16pt FontAwesome'; | |
context.textAlign = 'center'; | |
context.fillStyle = 'rgba(255,255,255,1.0)'; | |
context.fillText( '\uf0c8', 10, 18 ); | |
var data = context.getImageData( 2, 10, 1, 1 ).data; | |
if ( data[0] !== 255 && data[1] !== 255 && data[2] !== 255 ) { | |
// FontAwesome is not yet available, retrying ... | |
if ( retries > 0 ) { | |
setTimeout( checkReady, 100 ); | |
} else { | |
// failed | |
} | |
} else { | |
var customEvent; | |
if (window.CustomEvent) { | |
customEvent = new CustomEvent('fontLoaded', {detail: {}}); | |
} else { | |
customEvent = document.createEvent('CustomEvent'); | |
customEvent.initCustomEvent('fontLoaded', true, true, {}); | |
} | |
document.dispatchEvent(customEvent); | |
} | |
} | |
checkReady(); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment