1.1.2 • Published 2 years ago

react-texture-atlas v1.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

React Texture Atlas

Logo

Display specific portions of an image, and animate between sprites.

Desktop Atlas and Animation Editor Editor Preview Image

Contents

Features

  • Supports custom frame patterns/orders for animations
  • 3 animation modes ("loop", "keep-on-last", "destroy-after-last")
  • Animation events (onStart, onEnd, onDestroy, onChange)
  • No dependencies

Installation

npm i react-texture-atlas

This library functions on major browsers such as Chrome, Edge, Firefox and Opera.

Usage

import TextureAtlas from "react-texture-atlas";

This component contains an <img> element. Do not style this element (size etc.) as it may stop the component from functioning.

Example

import TextureAtlas from "react-texture-atlas";

export function Demo({ src }) {
  return (
    <TextureAtlas
      src={src}
      alt="Demo animation"
      rendering="pixelated"
      loading="lazy"
      draggable="false"
      width={200}
      height={200}
      uv={{
        width: 12,
        height: 16,
        // Place UV 10px from the left and 5px from the top of the source image.
        originX: 10,
        originY: 5
      }}
      base={{
        width: 120,
        height: 16
      }}
      animation={{
        frames: 10,
        // Move UV across 12px for each frame.
        frameStep: 12,
        // 'frameDuration' may be used instead of 'fps'.
        fps: 30,
        mode: "loop",
        orientation: "horizontal",
        // Display the frame on every frame change
        onChange: f => console.log(`Yay! Frame ${f} has arrived.`)
      }}
    />
  );
}

Props

PropTypeDefault ValueDescription
srcstring \| string[]URL source of the image - can be an array of sources, creating multiple atlases with the same settings.
altstring""Alternate text for image.
renderingImageRendering"auto"CSS image-rendering.
loading"eager" \| "lazy""eager"Image Loading.
draggable"true" \| "false""true"Image Draggability.
widthnumberPixel width of the atlas' output (output size ratio should be the same as UV size ratio).
heightnumberPixel height of the atlas' output (output size ratio should be the same as UV size ratio).
uvobjectPortion of the source image visible. @param width: number - Pixel width of the UV. @param height: number - Pixel height of the UV. @param originX: number - Initial X position of the UV (from the left). @param originY: number - Initial Y position of the UV (from the top).
baseobjectDimensions of the source image. @param width: number - Pixel width of the source. @param height: number - Pixel height of the source.
animationTextureAtlasAnimation \| falsefalseSettings for the atlas' animation - set to false for no animation.

TextureAtlasAnimation

An object containing animation settings.

ParamTypeDefault ValueDescription
framesnumber \| number[]Number of frames for the animation or array of frames as a pattern. A value of the number 3 would have the same result as an array of [0, 1, 2].
frameStepnumberNumber of pixels the UV moves across the source image.
frameDurationnumberSeconds per frame - cannot be active at the same times as fps.
fpsnumberFrames per second - cannot be active at the same times as frameDuration.
mode"loop" \| "keep-on-last" \| "destroy-after-last""loop"How the animation behaves. "loop" - repeats frames infinitely. "keep-on-last" - once the last frame is reached, it will stay on that frame. "destroy-after-last" - removes element when animation is complete.
orientation"horizontal" \| "vertical"X/Y direction the UV moves in for each frame.
onStart() => voidFunction to run on first frame.
onEnd() => voidFunction to run on last frame.
onDestroy() => voidFunction to run when atlas is destroyed by the "destroy-after-last" mode.
onChange(frame: number) => voidFunction to run every frame change. @param frame: number - Current frame of the atlas.
onFrameChanged(frame: number) => voidFunction to run every frame change. @deprecated - Use onChange instead. @param frame: number - Current frame of the atlas.
onFrameobjectFunction to run on a specific frame. @deprecated - Functionality replaced by onChange. @param frame: number - Frame function runs on. @param action: () => void - Function to run.
1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago