项目作者: VitorLuizC

项目描述 :
:space_invader: Create and manage animation functions with AnimationFrame API.
高级语言: TypeScript
项目地址: git://github.com/VitorLuizC/animate.git
创建时间: 2019-01-01T20:28:38Z
项目社区:https://github.com/VitorLuizC/animate

开源协议:MIT License

下载


@bitty/animate

License
@bitty/animate"">Library minified size
@bitty/animate"">Library minified + gzipped size

Animate bubbles example GIF

Create and manage animation functions with AnimationFrame API.

  • :zap: Dependency free and smaller than 170B (ESM minified + gzipped);
  • :label: Type definitions to TS developers and IDE/Editors intellisense;
  • :package: CommonJS, ESM and UMD distributions (CDN uses UMD as default);

See bubbles example at Codepen

Installation

This library is published in the NPM registry and can be installed using any compatible package manager.

  1. npm install @vitorluizc/animate --save
  2. # For Yarn, use the command below.
  3. yarn add @vitorluizc/animate

Installation from CDN

This module has an UMD bundle available through JSDelivr and Unpkg CDNs.

  1. <script src="https://cdn.jsdelivr.net/npm/@bitty/animate"></script>
  2. <script>
  3. // module will be available through `animate` function.
  4. var animation = animate(function () {
  5. // ...
  6. });
  7. animation.start();
  8. </script>

Usage

Call animate, the default exported function, with your callback and use returned object to manage your animation.

  1. import animate from '@bitty/animate';
  2. const canvas = document.querySelector('canvas');
  3. const context = canvas.getContext('2d');
  4. const position = { x: 0, y: 0 };
  5. const animation = animate(() => {
  6. context.clearRect(0, 0, canvas.width, canvas.height);
  7. context.beginPath();
  8. context.arc(position.x, position.y, 100 / 2, 0, 2 * Math.PI);
  9. context.fillStyle = '#000000';
  10. context.fill();
  11. context.closePath();
  12. });
  13. window.addEventListener('mousemove', (event) => {
  14. position.x = event.clientX;
  15. position.y = event.clientY;
  16. });
  17. animation.start();

See this example on Codepen.

API

  • animate

    The default exported function, which receives callback as argument and returns an Animation.

    • callback is a synchronous function running into a AnimationFrame recursion.
    1. let count = 0;
    2. const animation = animate(() => {
    3. context.clearRect(0, 0, element.width, element.height);
    4. context.font = '4rem monospace';
    5. context.textAlign = 'center';
    6. context.fillText(count, element.width / 2, element.height / 2);
    7. count++;
    8. });
    9. animation.start();

    See this example on Codepen.


    TypeScript type definitions.




    ts export default function animate(callback: () => void): Animation;

  • Animation

    An object returned by animate function to manage your animations. It can start, stop and check if animation is running.

    • running: A getter property that indicates if animation is running.

    • start(): A method to start the animation.

    • stop(): A method to stop the animation.

    1. const animation = animate(() => { ... });
    2. animation.start();
    3. // Stops the animation after 10s
    4. setTimeout(() => animation.stop(), 10 * 1000);
    5. if (animation.running)
    6. console.log('The animation is running...');

    TypeScript type definitions.




    ts export interface Animation { readonly running: boolean; stop: () => void; start: () => void; }

License

Released under MIT License.