Code Monkey home page Code Monkey logo

react-spritesheet's Introduction

react-spritesheet

Spritesheets for React

Installation

npm install react-spritesheet

Usage

This module exports three components:

Sprite

This component is for displaying a part of a bigger image - a rectangular cutout as you will.

<Sprite filename="hello.png" x={40} y={40} width={40} height={40} />

Props

filename
Type: String

The filename of the image to be displayed

x
Type: Number

The x offset of the image

y
Type: Number

The y offset of the image

width
Type: Number

The width of the image that will be displayed

height
Type: Number

The height of the image that will be displayed

SpriteSheet

Providing an image and a spritesheet object, this will display an image from the pre-defined object.

const spriteSheet = {
  image1: {
    x: 0,
    y: 0,
    width: 100,
    height: 100,
  },
  image2: {
    x: 100,
    y: 0,
    width: 100,
    height: 100,
  },
};

<SpriteSheet filename="myimage.png" data={spriteSheet} sprite="image1" />

For generating a spritesheet object, take a look at TexturePacker or spritesmith.

Props

filename
Type: String

The filename of the image to be displayed

data
Type: Object

The spritesheet object, to be in the form of:

{
  "<name>": {
    "x": 0,
    "y": 0,
    "width": 0,
    "height": 0
  }
}
sprite
Type: String

The name of the sprite that should be displayed

AnimatedSpriteSheet

Similar to Sprite, but displays one sprite after another at a particular speed.

<AnimatedSpriteSheet
  filename="myimage.png"
  initialFrame={0}
  frame={{ width: 120, height: 120 }}
  bounds={{ x: 0, y: 0, width: 480, height: 120 }}
  isPlaying
  loop
  speed={300}
/>

Props

filename
Type: String

The filename of the image to be displayed

initialFrame
Type: Number
Default: 0

The initial frame of the image

frame
Type: Object

The dimensions of a single frame

bounds
Type: Object

The bounds of the image that will be used for displaying one frame after another.

isPlaying
Type: Boolean
Default: true

Determines if the spritesheet is actually playing.

loop
Type: Boolean
Default: true

Loops through the animation

speed
Type: Number
Default: 300

The speed (in milliseconds) of displaying one frame after another

Example

See example/index.html.

License

MIT

react-spritesheet's People

Contributors

frostney avatar npmcdn-to-unpkg-bot avatar raphaelkottakal avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

react-spritesheet's Issues

Height and Width only increase image container

When increasing the size of a Sprite through the props, it only increases the height and width of a container, but doesn't scale up the size of the background image, meaning a 20px icon on a spritesheet when set to height or width 30, will just showing additional parts of surrounding sprites.

Similarly, changing the sprite image size down to 10px will just show a portion of the sprite, not a small version of the sprite

Please add an ability to scale sprites.

I would like to add a spritesheet and be able to scale it as an HTML <img>. The current version creates a <div> with the image data as a CSS background-image property. The 'height' and 'width' parameters for sprites will change what part of the spritesheet it will display. Scaling using CSS transform: scale(0.5) introduces strange behavior where the images will have thick padding. This is also what #6 seems to want too.

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.