Git Product home page Git Product logo

react-scene's Introduction

React Scene

Installation

npm install --save react-scene

Usage

React Scene is a component that expose different methods to handle a typic scene logic.

The methods are:

  • load Load everything that has to be loaded
  • build Build the scene (if you need to create or rend any object)
  • resize Resize the scene. It is also automatically called when the width or height props change on the scene
  • mute To mute the scene
  • unmute To unmute the scene
  • play Play the scene
  • pause Pause the scene
  • end End the scene
  • destroy Destroy any resources

Each method can be synchronous or asynchronous and will trigger lifecycle methods on the parent listening to them. Those lifecycles methods are in the following format sceneWill[Method] and sceneDid[Method] (Ex: sceneWillLoad, sceneDidLoad)

Basic usage

Creating a scene component
var React = require('react');
var ReactScene = require('react-scene');
var createjs = require('preload-js');

var Scene = React.createClass({
    
    render: function()
    {
        return (
            <div>
                
            </div>
        );
    },
    
    /**
     * Scene methods
     */
    load: function(e)
    {
        // Methods can be asynchronous by either calling the async method
        // on the first argument. The async method returns a function
        // that needs to be called when the action is done. Or you can
        // also return a promise and it will automatically wait for the
        // promise completion before calling sceneDidLoad
        var done = e.async();
        
        this.preloadQueue = new createjs.LoadQueue();
        this.preloadQueue.addEventListener('complete', done);
        this.preloadQueue.loadFile({
            id: 'photo',
            src: '/photo.jpg'
        });
    },
    
    build: function(e)
    {
        
    },
    
    resize: function(e)
    {
        
    },
    
    mute: function(e)
    {
        
    },
    
    unmute: function(e)
    {
        
    },
    
    play: function(e)
    {
        var that = this;
        setTimeout(function()
        {
            // If you need to control a scene within a scene. There is a scene
            // props containing the scene methods. Calling the `end` when the
            // playback is done is a good example.
            
            that.props.scene.end();
        }, 2000);
    },
    
    pause: function(e)
    {
        
    },
    
    end: function(e)
    {
        
    },
    
    destroy: function(e)
    {
        
    }
    
});

// Calling createScene with the Component as the first argument, will wrap
// your scene component in a ReactScene Component and assure that the methods
// you define will be called and the corresponding lifecycle methods.
module.exports = ReactScene.createScene(Scene);
Using the scene component
var React = require('react');
var Scene = require('./Scene');

var Story = React.createClass({
    
    render: function()
    {
        return (
            <div>
                <Scene
                    sceneWillLoad={this.onSceneWillLoad}
                    sceneDidLoad={this.onSceneDidLoad}
                    sceneWillPlay={this.onSceneWillPlay}
                    sceneDidPlay={this.onSceneDidPlay}
                    />
            </div>
        );
    },
    
    onSceneWillLoad: function()
    {
        console.log('Scene will load');
    },
    
    onSceneDidLoad: function()
    {
        console.log('Scene did load');
    },
    
    onSceneWillPlay: function()
    {
        console.log('Scene will play');
    },
    
    onSceneDidPlay: function()
    {
        console.log('Scene did play');
    }
    
});

module.exports = Story;
Controlling the scene
var React = require('react');
var Scene = require('./Scene');

var Story = React.createClass({
    
    getInitialState: function()
    {
        return {
            remote: null
        };
    },
    
    render: function()
    {
        return (
            <div>
                <div>
                    <button type="button" onClick={this.onClickPlay}>Play</button>
                </div>
                <Scene
                    ref="scene"
                    onRemote={this.onRemote}
                    sceneWillLoad={this.onSceneWillLoad}
                    sceneDidLoad={this.onSceneDidLoad}
                    sceneWillPlay={this.onSceneWillPlay}
                    sceneDidPlay={this.onSceneDidPlay}
                    sceneWillEnd={this.onSceneWillEnd}
                    sceneDidEnd={this.onSceneDidEnd}
                    />
            </div>
        );
    },
    
    onClickPlay: function(e)
    {
        e.preventDefault();
        
        // Scene methods can be called either by using a `ref` 
        this.refs.scene.play();
        
        // or using the remote provided by the `onRemote`
        this.state.remote.play();
    },
    
    onRemote: function(remote)
    {
        // When providing an onRemote props to a scene, the function
        // is called with a remote object as the first argument. This
        // object has every scene methods. It can be stored in the state
        // for later use.
        this.setState({
            remote: remote
        });
    },
    
    onSceneWillLoad: function()
    {
        console.log('Scene will load');
    },
    
    onSceneDidLoad: function()
    {
        console.log('Scene did load');
    },
    
    onSceneWillPlay: function()
    {
        console.log('Scene will play');
    },
    
    onSceneDidPlay: function()
    {
        console.log('Scene did play');
    },
    
    onSceneWillEnd: function()
    {
        console.log('Scene will end');
    },
    
    onSceneDidEnd: function()
    {
        console.log('Scene did end');
    }
    
});

module.exports = Story;

react-scene's People

Contributors

dmongeau avatar

Stargazers

Christopher Novelli avatar Gustavo Lanzas avatar  avatar

Watchers

 avatar James Cloos avatar Christopher Novelli avatar  avatar

Forkers

cnov20

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.