🚨 I don't have time to maintain this package. I am open to PRs. 🚨
rn-sprite-sheet
A sprite sheet animation library for React Native
Install
npm install --save rn-sprite-sheet
Import
import SpriteSheet from 'rn-sprite-sheet';
Usage
- Create ref for SpriteSheet
- Set the source using
require()
for local asset images or using{ uri: <image's url>, width: <image's width>, height: <image's height> }
for network images - Specify the columns and rows (each frame must be the same size)
- Create animations object (each key is an animation name and their value should be an array of frame indexes)
- Play an animation by calling the play method on a SpriteSheet reference and pass it a config object with at least a type property
Example
<SpriteSheet ref= thismummy = ref source= columns=9 rows=6 // height={200} // set either, none, but not both // width={200} // frameHeight={50} // manually set size of your sprite // frameWidth={50} // overrides auto calculation of frame size based on height, width, columns, and rows. // offsetX={0} // offsetY={0} imageStyle= marginTop: -1 animations= walk: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 appear: Array die: Array />;// ... thismummy;
Methods
To be called on a SpriteSheet reference
// stop at current frame // stop and go to first frame
Props
static propTypes = source: sourcePropTypeisRequired // source must be required columns: PropTypesnumberisRequired rows: PropTypesnumberisRequired animations: PropTypesobjectisRequired // see example viewStyle: stylePropType // styles for the sprite sheet container imageStyle: stylePropType // styles for the sprite sheet height: PropTypesnumber // set either height, width, or none, width: PropTypesnumber // but not both height and width onLoad: PropTypesfunc frameWidth: PropTypesfunc // overrides sprite size calculation based on height,width,rows,columns props frameHeight: PropTypesfunc // both frameWidth and frameHeight must be set or neither offsetX: PropTypesfunc // used with frameWidth/frameHeight to adjust offset of first frame offsetY: PropTypesfunc; static defaultPropTypes = columns: 1 rows: 1 animations: {};
Sprite sheet image specifications
When generating a sprite sheet image to use with rn-sprite-sheet, ensure that the frames are all of the same dimensions, and that the overall image size is exactly the height of all rows and the width of all columns. Depending on the tool you use to generate your sprite sheet image, you may need to crop the source image.