Code Monkey home page Code Monkey logo

use-spritesheet's Introduction


Version Twitter ETH Language License Bundle Size Build

use-spritesheet is a set of hooks to use pixel art, spritesheets and Aseprite with react-three-fiber in just a few lines of code.

👁  Live Demo (source in example)

Installation

npm i use-spritesheet
yarn add use-spritesheet

API

usePixelTexture

A small time-saver if you want crisp pixels on a texture, sets the texture filter to nearest-neighbour and (optionally) enables wrapping.

import frogSrc from './resources/frog.png';

const PixelTexture = () => {
  const tex = usePixelTexture(frogSrc);

  return (
    <sprite>
      <spriteMaterial transparent map={tex} />
    </sprite>
  );
};

useSpritesheet

Perfect for when you have a spritesheet and want to slice out a single frame to display statically (such as an icon from a icon set).

import smileySrc from './resources/smiley_idle.png';

const SpritesheetSingleFrame = () => {
  // 1 row
  // 8 columns
  // display frame index 2
  const tex = useSpritesheet(smileySrc, 1, 8, 2);

  return (
    <sprite>
      <spriteMaterial transparent map={tex} />
    </sprite>
  );
};

useSpritesheetAnimation

Play a series of frames that are baked into a single texture, ideal for particle effects.

import impSrc from './resources/impo.png';

const SpritesheetAnimation = ({ paused }: { paused: boolean }) => {
  // 100ms per frame
  // 2 rows
  // 4 columns
  const [tex] = useSpritesheetAnimation(impSrc, 100, 2, 4, paused);

  return (
    <sprite>
      <spriteMaterial transparent map={tex} />
    </sprite>
  );
};

useAsepriteAnimation

Import a texture + json file exported from Aseprite, select which animation to play and control playback speed.

import gremlin from "./resources/bomber.png";
import gremlinJson from "./resources/bomber.json";

export const AsepriteAnimation = ({
  animation = "idle",
  paused,
}: any) => {
  const [texture] = useAseprite(
    gremlin,
    gremlinJson as AsepriteJson,
    animation, // Changing this parameter automatically switches animations
    paused
  );

  return (
    <sprite>
      <spriteMaterial transparent map={texture} />
    </sprite>
  );
};

Running this repo

We make use of yarn workspaces to develop the example alongside the library itself.

Bootstrap

yarn

Running the examples

cd use-spritesheet
yarn build
cd ../example
yarn start

use-spritesheet's People

Contributors

bfollington avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

use-spritesheet's Issues

Refactor config API

Allow looping control, speed, callbacks etc. such that an animation controller could orchestrate this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.