Skip to content

Instantly share code, notes, and snippets.

@jackrugile
Last active December 22, 2015 06:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackrugile/6429002 to your computer and use it in GitHub Desktop.
Save jackrugile/6429002 to your computer and use it in GitHub Desktop.
Favicon generated from array using canvas and toDataURL()
function generateFavicon() {
var favicon = document.getElementById( 'favicon' ), // referencing shortcut icon in <head>
favc = document.createElement( 'canvas' ),
favctx = favc.getContext( '2d' ),
faviconGrid = [
[ 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 ],
[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0 ],
[ 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0 ],
[ 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1 ],
[ 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1 ],
[ 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1 ]
];
favc.width = favc.height = 16;
favctx.beginPath();
for( var y = 0; y < 16; y++ ) {
for( var x = 0; x < 16; x++ ) {
if( faviconGrid[ y ][ x ] === 1 ) {
favctx.rect( x, y, 1, 1 );
}
}
}
favctx.fill();
favicon.href = favc.toDataURL();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment