Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Animate favicon #38

wants to merge 3 commits into from

Conversation

kai-oswald
Copy link

Fixes #31

Changes

  • Animated favicon

Screenshot/GIF

loader

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

  • Passes linting
  • Consistent look across browsers

@jh3y
Copy link
Owner

jh3y commented Oct 7, 2019

Hey @kai-oswald 👋

This is great! Thanks for contributing 👍
That's fine. We can come up with a good solution first and then drop it into the react app using useEffect when the app loads 💪

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 basic. Then once there's a GIF, there are online tools that can split that into image frames. Then I was going to use requestAnimationFrame to update the favicon href attribute on the available animation frame with the next frame in the set.

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?

@kai-oswald
Copy link
Author

Browser support seems okay-ish.

image

Just fixed the dimensions btw, didn't even notice it was off 😅

@jh3y
Copy link
Owner

jh3y commented Oct 8, 2019

Fixed dimensions 👍

But we are no longer spinning 😭 I think this is due to how requestAnimationFrame has been used.

I'll leave a comment on the code 👍 And we will get this in today 💪

@kai-oswald
Copy link
Author

Did you clear your cache? 👀

Copy link
Owner

@jh3y jh3y left a 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 👍

image.addEventListener("load", function(e) {
var degrees = 0;
window.requestAnimationFrame(function() {
setInterval(function() {
Copy link
Owner

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 👍

context.drawImage(image, -image.width/2, -image.height/2, image.width, image.height);
context.restore();
favicon.href = canvas.toDataURL("image/png");
degrees+=5;
Copy link
Owner

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 👍

<img id="favicon-source" src="/favicon.ico" width="64" height="64"/>
</div>
<script>
var canvas = document.getElementById("favicon-canvas");
Copy link
Owner

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?

@jh3y
Copy link
Owner

jh3y commented Oct 8, 2019

Yep 👍 I see it working again now 😅

The changes for requestAnimationFrame would still be needed though as we don't get the benefit of requestAnimationFrame this way. I think reducing the degrees may make it look smoother 🤞

@kai-oswald
Copy link
Author

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 canvas.toDataURL("image/png") is an expensive operation or the browser limits the update rate of the favicon.

Here's another demo of this technique having the same issues

whirl

@jh3y
Copy link
Owner

jh3y commented Oct 8, 2019

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

@kai-oswald
Copy link
Author

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 😅

@jh3y jh3y mentioned this pull request Oct 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Animated favicon for page
2 participants