Emojicon is a small JS library that allows you to dynamically change a page's favicon to any emoji.
This project is part of #CreateWeekly, my attempt to create something new publicly every week in 2020.
With Emojicon you can:
- Dynamically change the favicon to any emoji
- Specify custom emoji color (for some older monochrome emojis)
- Render emoji to a data URL without changing page favicon
- Use a custom canvas for rendering (e.g. in Node, for SSR)
Emojicon is tiny and has no dependencies - weighing only 939 bytes (603 bytes g-zipped).
Emojicon has been designed to work as follows:
- Renders the given emoji to a 32x32 transparent canvas using the platform's emoji font
- Converts the canvas image to a data URL using
canvas.toDataURL('image/png')
- Changes the page's favicon by setting the
href
attribute on the favicon<link>
element in the document head
npm install emojicon --save
Before using Emojicon, ensure that your favicon <link>
element in the document head has an id of emojicon
, or pass linkId
with the actual id when calling emojicon.set()
.
<link rel="shortcut icon" id="emojicon" href="/favicon.ico" />
import emojicon from 'emojicon';
emojicon.set('π₯');
This only works for some older monochrome emojis.
import emojicon from 'emojicon';
emojicon.set('β·', { color: '#EA0' });
import emojicon from 'emojicon';
function getEmojiColor() {
// Check for dark mode support
if (window.matchMedia('(prefers-color-scheme)').media !== 'not all') {
return window.matchMedia('(prefers-color-scheme: dark)').matches
? '#fff'
: '#000';
}
// Default to black
return '#000';
}
emojicon.set('π₯', { color: getEmojiColor() });
import emojicon from 'emojicon';
const faviconUrl = emojicon.render('π₯');
// Use `faviconUrl` anywhere you would use an image URL
A custom canvas is useful when running in an environment that doesn't have <canvas>
available natively (e.g. Node).
const emojicon = require('emojicon');
const { createCanvas } = require('canvas'); // npm install canvas
const faviconUrl = emojicon.render('π₯', { canvas: createCanvas(32, 32) });
// Use `faviconUrl` to set the favicon <link> element's `href` attribute
Note that Node Canvas's emoji support is not that great. You are probably better off using a default favicon server-side and setting the emoji favicon client-side when the page loads.
Add Emojicon to your list of scripts:
<script src="https://unpkg.com/emojicon"></script>
Then use it anywhere in JS via the global emojicon
variable:
emojicon.set('π₯');
Set the page's favicon to the given emoji.
emoji
: String (required): the emoji (should contain one emoji only)options
: Object (optional) with any of the following properties:linkId
: String: id of the favicon<link>
element to set the emoji on, defaults toemojicon
color
: String: color of the emoji (any CSS color string)canvas
: Object: a custom canvas implementation, must be compatible with the standard canvas API
Render the given emoji to a data URL.
emoji
: String (required): the emoji (should contain one emoji only)options
: Object (optional) with any of the following properties:color
: String: color of the emoji (any valid CSS color string)canvas
: Object: a custom canvas implementation, must be compatible with the standard canvas API
Returns a data URL string that can be used anywhere you would use an image URL.
- Add TypeScript declarations (contributions welcome)