I want to add emoji to my site, but hide them if they're not supported on the platform, rather than showing little squares.
I have a feeling that this isn't possible, but does anybody disagree? How can this be done?
Paint a glyph (in the Emoji range that is the one most popular by vendors, in the range of Emojis by the Unicode Consortium like Happy face, Kiss, Sad face etc) to canvas
and read a pixel using getImageData
. If the pixel's Alpha channel data[3]
you're interested-in is not transparent (like for example in the center of ) , else, it might be an Emoji 😗
function supportsEmoji () {
const ctx = document.createElement("canvas").getContext("2d");
ctx.canvas.width = ctx.canvas.height = 1;
ctx.fillText("😗", -4, 4);
return ctx.getImageData(0, 0, 1, 1).data[3] > 0; // Not a transparent pixel
}
console.log( supportsEmoji() );
or something like that...
Tested the above in Chrome, Firefox, IE11, Edge, Safari
Safari returns false
and IE11 although has Emoji but without colors returned true
.
Edit:
(As pointed in comments) Modernizr has a similar detection for Emoji - so you might give it also a go