GithubHelp home page GithubHelp logo

gerhobbelt / animation-frame Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kof/animation-frame

0.0 2.0 0.0 73 KB

An even better requestAnimationFrame

License: MIT License

JavaScript 100.00%

animation-frame's Introduction

An even better requestAnimationFrame

  • fixed ios6 issues upgrading to the native implementation if it works (no agent sniffing)
  • like the native implementation it will group callbacks for better performance
  • like the native implementation it will degrade the frame rate depending on device performance
  • you can define your own frame rate specifically for every animation
  • highly optimized for performance
  • can be used as a shim, but has an own namespace per default
  • no performance degradation if using mutilple RAF calls in parallel (see examples/compare.html)

Usage

Get the api

If you are inside of a commonjs/amd module:

var AnimationFrame = require('animation-frame');

Otherwise its defined on window:

var AnimationFrame = window.AnimationFrame;

Activate the shim AnimationFrame.shim(options)

It will replace native implementation if it does exist but still will use it if possible. So you can use window.requestAnimationFrame and window.cancelAnimationFrame after this safely. Optionally you can pass the frame rate.

AnimationFrame.shim(options);

Set custom default frame rate

There are devices with different refresh rate than 60 out of there. You can define a custom value, for the shim implementation. Native implementation should do it for you. Do it before requesting frames, because after that the frame length is cached.

AnimationFrame.FRAME_RATE = 30;

Create instance new AnimationFrame(options)

Options can be an object or a number, number is the custom frame rate.

Options:

  • useNative use the native animation frame if possible, defaults to true
  • frameRate pass a custom frame rate
    // Using default frame rate
    var animationFrame = new AnimationFrame();

    // Using custom frame rate.
    var animationFrame = new AnimationFrame(20);

    // Avoid using native RAF:
    var animationFrame = new AnimationFrame({useNative: false});

Request a frame animationFrame.request(fn)

var frameId = animationFrame.request(function(time) {
    // Your animation here.
});

Cancel frame animationFrame.cancel(frameId)

var animationFrame = new AnimationFrame();
animationFrame.cancel(frameId);

Known problems

  • ios6-7 safari native animation frame animation can conflict with css animations, see #2

Credits

http://paulirish.com/2011/requestanimationframe-for-smart-animating

http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating

https://gist.github.com/paulirish/1579671

https://gist.github.com/jonasfj/4438815

https://gist.github.com/KrofDrakula/5318048

License

MIT

animation-frame's People

Contributors

connesc avatar gerhobbelt avatar greenkeeper[bot] avatar iduuck avatar kof avatar olivierrr avatar ponychicken avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.