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

gif-tsx

v0.1.5

Published

A TypeScript-first React GIF player library.

Downloads

46

Readme

gif-tsx

A simple TypeScript-first React GIF player library. Ships with best practices like:

  • ESM module specification for tree-shaking

  • Strict type-checking

gif-tsx offers total flexibility when it comes to writing a GIF player component. Because controls are exposed via a reusable hook, the consumer is given complete creative freedom when it comes to defining what the GIF player should do and how it should look.

Install

Install with your choice of package manager.

NPM

npm install gif-tsx --save

Yarn

yarn add gif-tsx

Getting started

The useGifController hook is the core of this library, and returns a GIF controller bound to a canvas element which plays a GIF.

Let's start by writing a basic GifPlayer component. We will first need to import our dependencies:

import React, { useRef } from "react";
import { useGifController } from "gif-tsx";

And then, we need to provide two things.

  1. A URL to load our GIF from.

  2. A ref to a canvas element that will be rendered after we load our GIF.

To load a custom GIF (i.e. not one served remotely), you must add the GIF to your public/ directory which is configured by your bundler (e.g. Webpack, Snowpack, or Vite). If you are using Vite, it is as simple as moving your GIF to the public/ directory at the project root.

Then, we handle the error, loading, and resolved states accordingly, and we're all good to go!

function GifPlayer() {
  const canvasRef = useRef<HTMLCanvasElement>(null);
  const controller = useGifController("/earth.gif", canvasRef);

  // handle loading state
  if (controller.state === "loading") {
    // `controller` has type `GifControllerLoading`
    return null;
  }

  // handle error state
  if (controller.state === "error") {
    // `controller` has type `GifControllerError`
    console.error(controller.errorMessage);
    return null;
  }

  // `controller` has type `GifControllerResolved`
  const { canvasProps, play, pause } = controller;

  return (
    <div>
      <canvas {...canvasProps} ref={canvasRef} />
      <button alt="play" onClick={play} />
      <button alt="pause" onClick={pause}>
    </div>
  );
}

API

useGifController

The useGifController hook accepts 3 parameters:

  • url: string -- The URL of the GIF to play. This can be a local or remote URL. For example, it can be /earth.gif if earth.gif is served by the local server, or https://foo.bar/earth.gif if earth.gif is served by foo.bar.

  • canvasRef: React.RefObject<HTMLCanvasElement> -- A ref bound to the canvas element returned in the resolved state.

  • autoplay: boolean = false (optional, defaults to false) -- Whether to begin playing the GIF as soon as the GIF resolves.

useGifController has 3 possible states, exposed by the GifController.state property.

  • loading: The GIF is currently being fetched and processed.

  • error: There was an error fetching or processing the GIF. The GIF controller exposes an error message on GifController.errorMessage.

  • resolved: The GIF was fetched and processed successfully. The GIF player controls are only exposed in this state.

The component calling useGifController should handle the states separately. Assuming you've handled the loading and error states accordingly, the following controls are exposed by the GIF controller.

  • canvasProps: HTMLCanvasElementProps -- Props to pass to the canvas element. Includes the ideal width and height.

  • playing: boolean -- Whether the GIF is currently playing.

  • play: () => void -- Callback which begins playing the GIF.

  • pause: () => void -- Callback which pauses the GIF.

  • restart: () => void -- Callback which resets the GIF back to the first frame. Does not affect playback state.

  • frameIndex: number -- The current frame, 0-indexed.

  • renderFrame: (frameIndex: number) => void -- Callback which accepts an argument frameIndex: number that renders the frame at frame index frameIndex.

  • renderNextFrame: () => void -- Callback which renders the next frame of the GIF.

  • renderPreviousFrame: () => void -- Callback which renders the previous frame of the GIF.

  • width: number -- Width of the loaded GIF.

  • height: number -- Height of the loaded GIF.

License

MIT. See LICENSE file in project root.