Skip to content

Instantly share code, notes, and snippets.

@tmthrgd
Last active September 2, 2016 10:00
Show Gist options
  • Save tmthrgd/bc1d282f5c5b411006d43cb8b2940aaa to your computer and use it in GitHub Desktop.
Save tmthrgd/bc1d282f5c5b411006d43cb8b2940aaa to your computer and use it in GitHub Desktop.
<!doctype html>
<html lang=en>
<!--
(c) Tom Thorogood 2012
https://tomthorogood.co.uk
Copyright 2012 Tom Thorogood. All rights reserved.
Use of this source code is governed by a
Modified BSD License license that can be found in
the LICENSE file.
-->
<head>
<meta charset=utf-8>
<title>Animated Canvas Favicon</title>
<link rel="shortcut icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAZdEVYdFNvZnR3YXJlAFBhaW50Lk5FVCB2My41Ljg3O4BdAAABI0lEQVQ4T83Sv0tCURTAcdPEDIXawqFFEZXEoSWCpgQHN/0PdFIkEBwiGlraG2oQcZOWhsaghlCQ0LlBCAt0qKGppRrEvgfOiwd6DXLxwof3OPeeH+9xHY5FW555BtogOY/Ef4vskdjAPXYMRdzEl7FpalJmo2UrsqIHwzyTKOISZ8j+VeSBA4eooolvvKOHDuKwGkzUOibyhk+M1QfPK+T0E5ZME7jY2MezLbnN+wFWlSn3N36kI0v3AR6xa8sydpcz67jQ7vJDa7iF/JMUtuCHTDp1RYk+4QunkG4FdPGKIUrwTct2EtxGH3cIaQG5oXWMcIK0qbvEK3jR7mt6ULrd4ByxWcleNuWCXEMuj7VksiAys5KtvQAvEci1tS8pMrF+ANeXMr+BVB2tAAAAAElFTkSuQmCC" id=favicon>
</head>
<body>
<script>
function loaded() {
//var canvas = document.createElement('canvas'),
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d'),
favicon = document.getElementById('favicon'),
img = new Image(),
i = 0,
x = 0,
FPS = 20,
doWork = function() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, i, i, canvas.width, canvas.height);
document.head.removeChild(favicon);
favicon = document.createElement('link');
favicon.id = 'favicon';
favicon.rel = 'shortcut icon';
favicon.href = canvas.toDataURL();
document.head.appendChild(favicon);
if (i <= -canvas.width) {
i = x = canvas.width;
setTimeout(doWork, 4000 / FPS);
} else if (i-- !== 0 || x === 0)
setTimeout(doWork, 1000 / FPS);
else {
x = 0;
setTimeout(doWork, 16000 / FPS);
}
};
canvas.width = canvas.height = 16;
img.src = favicon.href;
img.onload = doWork();
}
if (window.addEventListener)
window.addEventListener('load', loaded, false);
else if (window.attachEvent)
window.attachEvent('onload', loaded);
else
window.onload = loaded;
</script>
<canvas id=canvas width=16 height=16 style="border:1px dotted #333;padding:1px"></canvas>
<div><p>&copy; <a href=https://tomthorogood.co.uk>Tom Thorogood</a> 2012</p></div>
</body>
</html>
Copyright (c) 2012, Tom Thorogood.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Tom Thorogood nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment