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

@craf-te/filmer

v1.0.3

Published

JavaScript/TypeScript requestAnimationFrame manager.

Downloads

7

Readme

filmer 🎥

npm version size

日本語

filmer is a lightweight & simple request-animation-frame manager written in TypeScript. No dependencies 🚀

This tool helps creators to start animation development smoothly 🔨

install

using a package manager:

npm i @craf-te/filmer
import filmer from '@craf-te/filmer';

how to use

basic usage


filmer
  .add('animation1',          // unique id,
    ({ time, deltaTime }) => {
      /* do something */
      console.log(time);      // elapsed time
      console.log(deltaTime); // time difference from the previous frame
    }
  );

filmer.start();

// if you need to stop/reset,
filmer.stop();
// or
filmer.reset();

and also, it can be written this way.

import filmer from '@craf-te/filmer';
import type { AnimationFunction } from '@craf-te/filmer';

const animate: AnimationFunction = ({ time, deltaTime }) => {
  console.log('something to animate',time, deltaTime);
}

filmer.add('animation1', animate);
filmer.start();

remove animation

const removeFunc = filmer.add('animation1', () => {});

removeFunc();
// or
filmer.remove('animation1');
// or if you want to remove all,
filmer.removeAll();

order of execution (optional)

If the third argument specifies the order of execution, it will be executed in ascending order.

filmer.add('animation1', () => {}, 2);
filmer.add('animation2', () => {}, Infinity);
filmer.add('animation3', () => {}, 1);
filmer.start();

// executed in the following order
// 1, animation3
// 2, animation1
// 3, animation2

advanced

coefficient not affected by fps

For implementations that use coefficients, such as animations using linear interpolation, coefficients that are not affected by fps can be used. This is useful, for example, when using a 120 fps monitor and the fps is not stable.

The default target is 60fps. To change it, enter a value in the argument.

filmer.add(
  'animation1',
  ({ time, deltaTime }) => {
    const coeff = 0.8;

    const adjustedCoeff = filmer.getLerpCoeff(coeff);

    // if you want to set target to 120fps,
    const adjustedCoeff120 = filmer.getLerpCoeff(coeff, 120);
  }
)

using with framework

It can also be used in a front-end framework. For example, if you use React, you can write the following.

// useRAF.ts

// reusing Logic with Custom Hooks
import { useEffect, useId } from "react";

import filmer from "@craf-te/filmer";
import type { AnimationFunction } from "@craf-te/filmer";

export const useRAF = (callback: AnimationFunction, order?: number) => {
  const id = useId();
  useEffect(()=> {
    const remove = filmer.add(id, callback, order);
    if(!filmer.isAnimating) filmer.start();
    return () => remove();
  }, [callback, id, order]);
}
// component.tsx

import type { AnimationFunction } from '@craf-te/filmer';
import { useCallback } from 'react';

import { useRAF } from '<useRAF-file-path>';

export default function Component() {
  const animation: AnimationFunction = useCallback(({ time, deltaTime }) => {
    console.log(time, deltaTime);
  }, []);

  useRAF(animation);

  return <div>{/* something */}</div>;
}

re-arrangement of execution order

The execution order argument accepts numbers other than integers. If you want to rearrange the numbers again, execute reorder.

filmer.add('anim1', () => {}, 4.2);
filmer.add('anim2', () => {}, 2);
filmer.add('anim3', () => {}, -Infinity);
filmer.add('anim4', () => {}, 0.5);

filmer.reorder();
console.log(filmer.animationList)
// result log
[
  {
    id: 'anim3',
    order: -Infinity
    update: () => {}
  },{
    id: 'anim4',
    order: 0
    update: () = {}
  },{
    id: 'anim2',
    order: 1
    update: () => {}
  },{
    id: 'anim1',
    order: 2
    update: () => {}
  }
]

License

The MIT License.