0.0.18 • Published 10 months ago

@ga1az/react-pixel-motion v0.0.18

Weekly downloads
-
License
-
Repository
github
Last release
10 months ago

React Pixel Motion

A lightweight React component for creating smooth, pixelated sprite animations. Perfect for games, retro-style interfaces, and pixel art animations. Totally inspired by react-sprite-animation.

Features

  • 🎮 Simple API for sprite sheet animations
  • 🖼️ Support horizontal, vertical and grid sprite sheets
  • 🔄 Control over animation speed, scale, and direction
  • 📱 Responsive and lightweight

Sprite Sheet example supports

Installation

# Using npm
npm install @ga1az/react-pixel-motion

# Using yarn
yarn add @ga1az/react-pixel-motion

# Using pnpm
pnpm add @ga1az/react-pixel-motion

# Using bun
bun add @ga1az/react-pixel-motion

Usage

import { PixelMotion } from "@ga1az/react-pixel-motion";
import characterSprite from './assets/character.svg'; // or any other image format
import warrior from './assets/warrior.png';

function App() {
  return (
    <PixelMotion
      sprite={characterSprite}
      width={24} // Width of each frame in pixels (required)
      height={31} // Height of each frame in pixels (required)
      frameCount={3} // Total number of frames in the sprite sheet (optional)
      fps={10} // Frames per second for the animation (optional)
      scale={5} // Scale factor for the sprite (optional)
      startFrame={0} // Initial frame to start the animation (optional)
      loop={true} // Whether the animation should loop (optional)
      shouldAnimate={true} // Whether the animation should play (optional)
      direction="horizontal" // Direction of the sprite sheet (optional)
      imageRendering={false} // Whether the image should be rendered in pixelated mode (optional - default is true)
      onAnimationEnd={() => console.log('Animation ended')} // Callback when animation ends (optional)
      onAnimationStart={() => console.log('Animation started')} // Callback when animation starts (optional)
    />
    // Grid 4x4 example
    <PixelMotion
      sprite={warrior}
      width={30}
      height={30}
      scale={5}
      fps={5}
      shouldAnimate={true}
      direction="grid" // Direction of the sprite sheet (optional)
      gridOptions={{
        columns: 4, // Total columns in the sprite sheet (required)
        rows: 4, // Total rows in the sprite sheet (required)
        rowIndex: 0, // Index of the row to animate (optional)
      }}
      onFrameChange={(frameIndex) => console.log(`Current frame: ${frameIndex}`)} // Callback for each frame change (optional)
    />

    // Example with specific frame callback
    <PixelMotion
      sprite={characterSprite}
      width={24}
      height={31}
      frameCount={3}
      fps={10}
      scale={5}
      shouldAnimate={true}
      onSpecificFrame={{
        frame: [1, 2], // Trigger on frames 1 and 2
        callback: (frameIndex) => console.log(`Special action on frame ${frameIndex}`) // Callback for specific frames (optional)
      }}
    />
  );
}

API

Props

PropTypeDefaultDescription
spritestring | StaticImageDataRequiredURL or import of the sprite sheet image
widthnumberRequiredWidth of each frame in pixels
heightnumberRequiredHeight of each frame in pixels
frameCountnumber1Total number of frames in the sprite sheet
fpsnumber60Frames per second for the animation
direction'horizontal' | 'vertical' | 'grid''horizontal'Direction of the sprite sheet
shouldAnimatebooleanfalseWhether the animation should play
scalenumber1Scale factor for the sprite
startFramenumber0Initial frame to start the animation
loopbooleantrueWhether the animation should loop
imageRenderingbooleantrueWhether the image should be rendered in pixelated mode
gridOptionsobjectundefinedOptions for grid sprite sheets (see Grid Options table)
onAnimationEnd() => voidundefinedCallback function when animation ends (only if loop=false)
onAnimationStart() => voidundefinedCallback function when animation starts
onFrameChange(frameIndex: number) => voidundefinedCallback function for each frame change
onSpecificFrame{ frame: number \| number[], callback: (frameIndex: number) => void }undefinedCallback for specific frames

Grid Options

OptionTypeDefaultDescription
columnsnumberRequiredNumber of columns in the grid
rowsnumberRequiredNumber of rows in the grid
rowIndexnumberundefinedIndex of the row to animate (animate horizontally)
columnIndexnumberundefinedIndex of the column to animate (animate vertically)
gapnumber0Gap between frames in pixels

Development

bun install
bun run dev # to run the demo
bun run build # to build the library
0.0.18

10 months ago

0.0.17

10 months ago

0.0.16

11 months ago

0.0.15

11 months ago

0.0.14

11 months ago

0.0.13

11 months ago

0.0.12

11 months ago

0.0.11

11 months ago

0.0.10

11 months ago

0.0.9

11 months ago

0.0.8

11 months ago

0.0.7

11 months ago

0.0.6

11 months ago

0.0.5

11 months ago

0.0.4

11 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago