npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

requestanimationframe-fix.js

v0.0.9

Published

Makes requestanimation frame only call if specified element is on screen

Downloads

8

Readme

requestanimationframe-fix.js

The issue this fixes: You've got a page with many canvases each independently using requestAnimationFrame (rAF) to handle animation. Most browsers as of March 2015 don't check if the canvas is off the screen. It seems a little short sited the default use case of rAF doesn't give the browser enough information to know if your canvas is off the screen since by default rAF doesn't require you to tell it which element you care about.

I thought the signature for rAF was

requestAnimationFrame(callback, optionalElement)

I at least I remember that being a consession to this issue I brought up when rAF was designed but checking the spec I see there is no second parameter :(

Why is this a problem? Imagine you're making a webpage about physics. Every 2 or 3 paragraphs you have an interactive animated diagram using rAF. In total you have 15 diagrams as you go down the page. Because of the API design every diagram is always running even though at any one time only 1 to 3 of them are visible. You find that your diagrams are running janky at 5-10fps instead of smooth at 60fps.

Well, this fix implements the second parameter so if passed it will check that element is on screen. It will also check iframes so if your various examples happen to be in iframes and those iframes are off the screen you're rAF callback will get delayed until are on screen.

##Examples:

Here's an example of 12 iframes, all running using standard rAF. It will likely run slow.

Here's an example using the fix. It should run much faster.

Deferred Loading

You can also use it to defer loading of a iframe. Just set a rAF on the iframe itself to set its src. It won't be called until it frame is on screen.

Here's an example

##Usage:

include it on every page that uses rAF

<script src="requestanimationframe-fix.js"></script>

It wraps the built in rAF automatically.

##Caveats:

Unfortunately this is not full solution. It requires every piece of code using rAF to use this fix. In other words, if you have a page with 10 animated diagrams and each diagram is in an iframe each of those diagrams have to include this fix. If you control all the content that might be fine. If on the other hand you're trying to host 3rd party content or ads you're S.O.L.

On top of that it has to do work for every rAF. If you're in the most common situation which is a single rAF on a full or close to full page app there's almost no overhead. If on the other hand you had 100 rAFs there are actually 100 rAF callbacks running each checking if their element is on the screen. If this was implemented at a browser level it would be much easier for it to do the minimal work and not have to check 100 elements every frame.