-
Notifications
You must be signed in to change notification settings - Fork 254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Animate favicon #38
base: master
Are you sure you want to change the base?
Animate favicon #38
Conversation
Hey @kai-oswald 👋 This is great! Thanks for contributing 👍 I hadn't even considered canvas so this is another option worth exploring 👍 My initial idea was that we can create a GIF of the current whirl used for the favicon. I believe it's I see the current canvas solution is doing something kinda similar. However, the dimensions are slightly off and the favicon appears smaller than it currently does. My last question 😅 How's the browser support? Is it working in the main browsers? |
Fixed dimensions 👍 But we are no longer spinning 😭 I think this is due to how I'll leave a comment on the code 👍 And we will get this in today 💪 |
Did you clear your cache? 👀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To fix up the requestAnimationFrame
usage, we need to first remove the setInterval
call as it won't be necessary 👍
Then we create a function that does the drawing/setting the favicon, let's say setFavicon
.
We call it first with requestAnimationFrame(setFavicon)
and then inside setFavicon
after all the logic is handled, we call requestAnimationFrame(setFavicon)
again 👍
public/index.html
Outdated
image.addEventListener("load", function(e) { | ||
var degrees = 0; | ||
window.requestAnimationFrame(function() { | ||
setInterval(function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need setInterval
if we are going to try and use requestAnimationFrame
👍
public/index.html
Outdated
context.drawImage(image, -image.width/2, -image.height/2, image.width, image.height); | ||
context.restore(); | ||
favicon.href = canvas.toDataURL("image/png"); | ||
degrees+=5; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be able to reduce this to 1
potentially and it will look smooth 👍
public/index.html
Outdated
<img id="favicon-source" src="/favicon.ico" width="64" height="64"/> | ||
</div> | ||
<script> | ||
var canvas = document.getElementById("favicon-canvas"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this still work if we create the canvas off-render? As in not in the DOM?
Could we instead create a canvas with document.createElement('canvas')
and never append it to the DOM?
Yep 👍 I see it working again now 😅 The changes for |
Hmm.. I applied your requested changes, but the favicon still doesn't look smooth compared to the visible canvas. I think the issue is that either Here's another demo of this technique having the same issues |
Hey @kai-oswald 👋 Yeah, I see the stutter too 😭 Could be an expensive operation. However, I have seen another approach that also had a kinda stuttery result. This article details a different method of animating the favicon. I'm wondering if the processing overhead is worth having in place if the favicon animation isn't smooth. It can't be seen in Safari also 😅 What do you think? That being said, the animation seems relatively smooth for Defender of the favicon |
Favicon doesn't seem to be working on the public Edge too (Edge dev-channel works since it's Chromium). We'd have to manually handle these cases so we can preserve the un-animated favicon. I also tested it in Firefox and the animation runs a lot smoother there than in Chrome, therefore I think it's a browser related issue and not an expensive operation. This feature would've been nice if it'd run smoothly, but now we know that browsers have issues with it 😅 |
Fixes #31
Changes
Screenshot/GIF
It looks a little bit laggy, although the canvas is running smoothly when visible.
I also don't know a lot about react, so I just added it to the
index.html
. Let me know if you want it changed.Checks