Skip to content

Instantly share code, notes, and snippets.

@hongbo-miao
Forked from mgechev/canvas-as-favicon.html
Created December 15, 2020 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hongbo-miao/f8637bae6172a8f1d262b92153102437 to your computer and use it in GitHub Desktop.
Save hongbo-miao/f8637bae6172a8f1d262b92153102437 to your computer and use it in GitHub Desktop.
<link rel="shortcut icon" width=32px>
<canvas style="display: none" id="loader" width="16" height="16"></canvas>
<script>
class Loader {
constructor(link, canvas) {
this.link = link;
this.canvas = canvas;
this.context = canvas.getContext('2d');
this.context.lineWidth = 2;
this.context.strokeStyle = "white";
}
setProgress(progress) {
const startAngle = 1.5 * Math.PI;
this.context.clearRect(0, 0, 16, 16);
this.context.beginPath();
this.context.arc(8, 8, 5, startAngle, (progress * 2 * Math.PI) / 100 + startAngle);
this.context.stroke();
this.link.href = this.canvas.toDataURL("image/png"); // update favicon
}
}
const canvas = document.querySelector("#loader");
const link = document.querySelector('link[rel*="icon"]');
const loader = new Loader(link, canvas);
let progress = 0;
const loading = () => {
loader.setProgress(progress);
if (progress >= 100) {
return;
}
progress++;
requestAnimationFrame(loading);
}
loading();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment