2.3.9 • Published 6 years ago

react-responsive-spritesheet v2.3.9

Weekly downloads
1,992
License
MIT
Repository
github
Last release
6 years ago

React Responsive Spritesheet

Build Status npm npm node version Known Vulnerabilities

Hello, world!

React Responsive Spritesheet is a React component which helps you to easily apply responsive spritesheet animations on your project. See our basic example.


New version 2 available!

  • Now you can use horizontal, vertical or multi-row spritesheet image;
  • Orientation parameter is no longer required;
  • Spritesheet image is preloaded before initialization;
  • The animation can be rewinded with setDirection() method and/or with direction parameter;
  • Direction information is provided by getInfo() method;

For previous versions, see our release history


Installation

$ npm install react-responsive-spritesheet --save

Usage

Basic usage with required parameters

import Spritesheet from 'react-responsive-spritesheet';

class App extends Component {

  render() {

    return (
      <Spritesheet
        image={`http://www.example.com/assets/image.png`}
        widthFrame={800}
        heightFrame={648}
        steps={46}
        fps={12}
      />
    );

  }

}

Complete usage with all parameters

import Spritesheet from 'react-responsive-spritesheet';

class App extends Component {

  render() {

    return (
      <Spritesheet
        className={`my-element__class--style`}
        style={{ backgroundColor: 'red' }}
        image={`http://www.example.com/assets/image.png`}
        widthFrame={800}
        heightFrame={648}
        steps={46}
        fps={12}
        direction={`forward`}
        timeout={1800}
        autoplay={false}
        loop={true}
        startAt={10}
        endAt={30}
        background={`http://www.example.com/assets/image.png`}
        backgroundSize={`cover`}
        backgroundRepeat={`no-repeat`}
        backgroundPosition={`center center`}
        getInstance={spritesheet => {
          this.spriteInstance = spritesheet;
        }}
        onClick={spritesheet => {
          spritesheet.play();
        }}
        onDoubleClick={spritesheet => {
          console.log( spritesheet.getInfo('isPlaying') );
        }}
        onMouseMove={spritesheet => {
          console.log( 'onMouseMove', spritesheet.getInfo('frame') );
        }}
        onMouseEnter={spritesheet => {
          console.log('onMouseEnter');
        }}
        onMouseLeave={spritesheet => {
          console.log('onMouseLeave');
        }}
        onMouseOver={spritesheet => {
          console.log('onMouseOver');
        }}
        onMouseOut={spritesheet => {
          console.log('onMouseOut');
        }}
        onMouseDown={spritesheet => {
          console.log('onMouseDown');
        }}
        onMouseUp={spritesheet => {
          console.log('onMouseUp');
        }}
        onInit={spritesheet => {
          console.log('onInit');
        }}
        onResize={spritesheet => {
          console.log( 'onResize', spritesheet.getInfo('frame') );
        }}
        onPlay={spritesheet => {
          console.log('onPlay');
        }}
        onPause={spritesheet => {
          console.log('onPause');
        }}
        onLoopComplete={spritesheet => {
          console.log('onLoopComplete');
        }}
        onEachFrame={spritesheet => {
          console.log('onEachFrame');
        }}
        onEnterFrame={[
          {
            frame: 2,
            callback: (() => {
              console.log('passed by frame 2')
            })
          },
          {
            frame: 7,
            callback: (() => {
              console.log('passed by frame 7')
            })
          }
        ]}
      />
    );

  }

}

Options

RequiredParameterTypeExampleDescription
classNamestring'my-element__class--style'Applies a classname for spritehseet container
styleobject{ backgroundColor: 'red', display: 'flex' }Applies inline style for spritehseet container
imagestring'http://yyy.com/image.png'URL or path of image to animate
widthFramenumber800Original width of each frame, in pixels
heightFramenumber800Original height of each frame, in pixels
stepsnumber47Total frames / steps present on animation image
fpsnumber12Velocity / Animation frames per second
directionstring'rewind''forward' or 'rewind' direction to display frames. It allows rewind the animation. Default: 'forward'
timeoutnumber1200*Delay for start animating. The 'autoplay' option must be true*
autoplaybooleanfalseDetermines if animation starts automatically
loopbooleanfalseDetermines if animation replay on end
isResponsivebooleantrueDetermines if animation resizes according window resize
startAtnumberfalseDetermines the first frame which will be displayed on start to animate
endAtnumberfalseDetermines the last frame which will be displayed on stop animation
backgroundstring'/assets/background.png'URL or path of background image placed behind animation
backgroundSizestring'cover'Style for background image
backgroundRepeatstring'no-repeat'Style for background image
backgroundPositionstring'center center'Style for background image
getInstancecallbackReturns callback instance for spritesheet controls
onClickfunctionProvides onClick callback function for spritesheet container
onDoubleClickfunctionProvides onDoubleClick callback function for spritesheet container
onMouseMovefunctionProvides onMouseMove callback function for spritesheet container
onMouseEnterfunctionProvides onMouseEnter callback function for spritesheet container
onMouseLeavefunctionProvides onMouseLeave callback function for spritesheet container
onMouseOverfunctionProvides onMouseOver callback function for spritesheet container
onMouseOutfunctionProvides onMouseOut callback function for spritesheet container
onMouseDownfunctionProvides onMouseDown callback function for spritesheet container
onMouseUpfunctionProvides onMouseUp callback function for spritesheet container
onInitfunctionProvides callback function when the spritesheet initializes
onResizefunctionProvides callback function when the spritesheet resizes
onPlayfunctionProvides callback function when the spritesheet plays. spritesheet.goToAndPlay(x) method also fires this callback
onPausefunctionProvides callback function when the spritesheet pauses. spritesheet.goToAndPause(x) method also fires this callback
onLoopCompletefunctionProvides callback function when the animation completes a loop cicle
onEachFramefunctionProvides callback function when each animation frame is changed
onEnterFramearrayAccepts an array of callback functions when the specific animation frame is displayed

Call methods

Using the instance provided on callback functions you can call some methods

Example

onMouseEnter={spritesheet => {
  console.log( spritesheet.setStartAt(6) );
}}
MethodCallDescription
playspritesheet.play()Plays the animation from current frame
pausespritesheet.pause()Pauses the animation on current frame
goToAndPlayspritesheet.goToAndPlay(frameNumber)Plays the animation from specified frame as argument
goToAndPausespritesheet.goToAndPause(frameNumber)Pauses the animation on specified frame
setStartAtspritesheet.setStartAt(frameNumber)Sets the first frame to be displayed on animation starts. It will be considered on loop cycles.
setEndAtspritesheet.setEndAt(frameNumber)Sets the last frame to be displayed on animation ends. It will be considered on loop cycles.
setFpsspritesheet.setFps(fpsNumber)Sets the fps (speed) of animation, even while is playing
setDirectionspritesheet.setDirection('rewind')Sets the direction of animation forward or rewind
getInfospritesheet.getInfo('stringInfoToRetrieve')Returns some real-time information about spritesheet. See below on Requesting infos section

Requesting infos

Using the instance.getInfo(x) method provided on callback functions you can request a real-time information about your spritesheet animation

Example

onMouseEnter={spritesheet => {
  console.log( spritesheet.getInfo('frame') );
}}
ParameterTypeReturns
framenumbercurrent frame of animation
fpsnumbercurrent frames per second (speed)
stepsnumbertotal number of animation steps
widthnumberscaled animation width, in pixels
heightnumberscaled animation height, in pixels
scalenumberscale of spritesheet, based on default sizes, note that scale=1 is relative to original size
directionstringdirection 'forward' or 'rewind' playing
isPlayingbooleanif animation is currently playing, returns true
isPausedbooleanif animation is currently paused or stopped, returns true
completeLoopCiclesnumbertotal number of cycles (loops) the animation has completed

Examples

Example #1 A basic usage with minimal parameters for spritesheet animation loop starting automatically.

live demo / edit source code

<Spritesheet
  className={`my-element__class--style`}
  image={`https://raw.githubusercontent.com/danilosetra/react-responsive-spritesheet/master/assets/images/examples/sprite-image-horizontal.png`}
  widthFrame={420}
  heightFrame={500}
  steps={14}
  fps={10}
  autoplay={true}
  loop={true}
/>

Example #2 Using parameters onMouseEnter and onMouseLeave for play and pause animation

live demo / edit source code

<Spritesheet
  className={`my-element__class--style`}
  image={`https://raw.githubusercontent.com/danilosetra/react-responsive-spritesheet/master/assets/images/examples/sprite-image-horizontal.png`}
  widthFrame={420}
  heightFrame={500}
  steps={14}
  fps={10}
  autoplay={false}
  loop={true}
  onMouseEnter={spritesheet => {
    spritesheet.play();
  }}
  onMouseLeave={spritesheet => {
    spritesheet.pause();
  }}
/>

Example #3

Using spritesheet instance to controls outside parameters, on your own functions.

live demo / edit source code

First, we use getInstance parameter to get instance and set this.spritesheetInstance to be used on your whole component, see below:

<Spritesheet
  className={`my-element__class--style`}
  image={`https://raw.githubusercontent.com/danilosetra/react-responsive-spritesheet/master/assets/images/examples/sprite-image-horizontal.png`}
  widthFrame={420}
  heightFrame={500}
  steps={14}
  fps={10}
  autoplay={false}
  loop={true}
  getInstance={spritesheet => {
    this.spritesheeInstance = spritesheet;
  }}
/>

Then, we can create buttons or whatever and set their own functions:

<div>
  <button onClick={this.myFunctionPlay.bind(this)}>play</button>
  <button onClick={this.myFunctionPause.bind(this)}>pause</button>
  <button onClick={this.myFunctionGetFrame.bind(this)}>alert current frame</button>
  <button onClick={this.myFunctionToggleDirection.bind(this)}>toggle direction</button>
</div>
myFunctionPlay(){
  this.spritesheeInstance.play();
}

myFunctionPause(){
  this.spritesheeInstance.pause();
}

myFunctionGetFrame(){
  alert(this.spritesheeInstance.getInfo('frame'));
}

myFunctionToggleDirection(){
  if(this.spritesheeInstance.getInfo('direction') === 'forward'){
    this.spritesheeInstance.setDirection('rewind');
  } else if(this.spritesheeInstance.getInfo('direction') === 'rewind'){
    this.spritesheeInstance.setDirection('forward');
  }
}

Example #4 Using background image

live demo / edit source code

<Spritesheet
  className={`my-element__class--style`}
  image={`https://raw.githubusercontent.com/danilosetra/react-responsive-spritesheet/master/assets/images/examples/sprite-image-horizontal.png`}
  widthFrame={420}
  heightFrame={500}
  steps={14}
  fps={10}
  autoplay={true}
  loop={true}
  background={`https://raw.githubusercontent.com/danilosetra/react-responsive-spritesheet/master/assets/images/examples/sprite-image-background.png`}
  backgroundSize={`cover`}
  backgroundRepeat={`no-repeat`}
  backgroundPosition={`center center`}
/>

2.3.9

6 years ago

2.3.8

6 years ago

2.3.6

6 years ago

2.3.5

6 years ago

2.3.4

6 years ago

2.3.3

6 years ago

2.3.2

6 years ago

2.3.1

6 years ago

2.3.0

6 years ago

2.2.3

6 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.1.4

7 years ago

2.1.3

7 years ago

2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.0

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago