Created
December 19, 2023 17:40
-
-
Save panphora/ee457f1043fc89533e428b33d10737d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function animateFavicon () { | |
const frameDelay = 10; | |
const angleChange = .5; | |
let angle = 0; | |
let lastFrameTime = 0; | |
const favicon = document.querySelector("[rel='icon']"); | |
const canvas = document.createElement('canvas'); | |
const ctx = canvas.getContext('2d'); | |
const img = new Image(); | |
img.src = './svgs/favicon.svg'; | |
img.onload = function() { | |
canvas.width = img.width; | |
canvas.height = img.height; | |
const rotateFavicon = (currentTime) => { | |
if (currentTime - lastFrameTime > frameDelay) { | |
ctx.clearRect(0, 0, canvas.width, canvas.height); | |
ctx.save(); | |
ctx.translate(canvas.width / 2, canvas.height / 2); | |
ctx.rotate((angle * Math.PI) / 180); | |
ctx.drawImage(img, -img.width / 2, -img.height / 2); | |
ctx.restore(); | |
favicon.setAttribute('href', canvas.toDataURL('image/png')); | |
angle = (angle + angleChange) % 360; | |
lastFrameTime = currentTime; | |
} | |
requestAnimationFrame(rotateFavicon); | |
}; | |
requestAnimationFrame(rotateFavicon); | |
}; | |
} | |
animateFavicon(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment