ReactPixiFiber is a JavaScript library for writing PixiJS applications using React declarative style in React 16.
For React <16.0.0 see react-pixi
.
See Rotating Bunny demo.
import { render } from "react-dom";
import { Sprite, Stage } from "react-pixi-fiber";
import bunny from "./bunny.png";
function Bunny(props) {
return (
<Sprite texture={PIXI.Texture.fromImage(bunny)} {...props} />
);
}
render(
<Stage width={800} height={600} options={{ backgroundColor: 0x10bb99 }}>
<Bunny x={200} y={200} />
</Stage>,
document.getElementById("container")
);
This example will render PIXI.Sprite
object into a Root Container of PIXI.Application
on the page.
The HTML-like syntax; called JSX is not required to use with this renderer, but it makes code more readable. You can use Babel with a React preset to convert JSX into native JavaScript.
import { render, Text } from "react-pixi-fiber";
import * as PIXI from "pixi.js";
// Setup PixiJS Application
const canvasElement = document.getElementById("container")
const app = new PIXI.Application(800, 600, {
backgroundColor: 0x10bb99,
view: canvasElement
});
render(
<Text text="Hello World!" x={200} y={200} />,
app.stage
);
This example will render PIXI.Text
object into a Root Container of PIXI Application (created as app
) inside the <canvas id="container"></canvas>
element on the page.
- Run
yarn install
(ornpm install
) in the repository root. - Run
yarn install
(ornpm install
) in theexamples
directory. - Run
yarn start
(ornpm run start
) in theexamples
directory. - Wait few seconds and browse examples that will open in new browser window.
The current version assumes React >16.0.0 and PixiJS >4.4.0
yarn add react-pixi-fiber
or
npm install react-pixi-fiber --save
This package works flawlessly with Create React App – see examples above, they already use it.
Migrating from react-pixi
It is possible to use React Pixi Fiber as a drop-in replacement for react-pixi
.
Please note that it has only been tested with basic scenarios – it is not guaranteed to work flawlessly.
There are two options:
Change:
import ReactPIXI from "react-pixi";
// or
const ReactPIXI = require("react-pixi");
to:
import ReactPIXI from "react-pixi-fiber/react-pixi-alias";
// or
const ReactPIXI = require("react-pixi-fiber/react-pixi-alias");
resolve: {
alias: {
'react-pixi$': 'react-pixi-fiber/react-pixi-alias'
}
}
React Pixi Fiber currently supports following components:
Renders Root Container of any PIXI.Application
.
Expects the following props:
width
(can be also passed inoptions
),height
(can be also passed inoptions
),options
- seePIXI.Application
options.
Renders PIXI.Container
.
Renders PIXI.Graphics
.
Renders PIXI.particles.ParticleContainer
.
Renders PIXI.Sprite
.
Renders PIXI.extras.TilingSprite
.
Renders PIXI.Text
.
Renders PIXI.extras.BitmapText
.
Similarly to ReactDOM in React 16,
ReactPixiFiber is not ignoring unknown PIXI.DisplayObject
members – they are all passed through. You can read
more about Unknown Prop Warning in ReactDOM, however
ReactPixiFiber will not warn you about unknown members.
For setting properties on PixiJS types that are either PIXI.Point
s or PIXI.ObservablePoint
s you can use either
and array of integers or a comma-separated string of integers in the following forms: [x,y]
, "x,y"
, [i]
, "i"
.
In the case where two integers are provided, the first will be applied to the x
coordinate and the second will be
applied to the y
coordinate. In the case where a single integer if provided, it will be applied to both coordinates.
You can still create your own PIXI Point
or ObservablePoint
objects and assign them directly to the property.
These won't actually replace the property but they will be applied using the original object's .copy()
method.
PIXI.Application
is automatically provided using the following definition (either as a prop or in context):
app
– an instance of PixiJS Application, with properties like:loader
– Loader instance to help with asset loading,renderer
– WebGL or CanvasRenderer,ticker
– Ticker for doing render updates,view
– reference to the renderer's canvas element.
To get app
prop in your component you may wrap it with withApp
higher-order component:
import { render } from "react-dom";
import { Sprite, Stage, withApp } from "react-pixi-fiber";
import bunny from "./bunny.png";
class RotatingBunny extends Component {
state = {
rotation: 0,
};
componentDidMount() {
// Note that `app` prop is coming through `withApp` HoC
this.props.app.ticker.add(this.animate);
}
componentWillUnmount() {
this.props.app.ticker.remove(this.animate);
}
animate = delta => {
this.setState(state => ({
rotation: state.rotation + 0.1 * delta,
}));
};
render() {
return (
<Sprite
{...this.props}
texture={PIXI.Texture.fromImage(bunny)}
rotation={this.state.rotation}
/>
);
}
}
RotatingBunny.propTypes = {
app: PropTypes.object.isRequired,
};
const RotatingBunnyWithApp = withApp(RotatingBunny);
render(
<Stage width={800} height={600} options={{ backgroundColor: 0x10bb99 }}>
<RotatingBunnyWithApp x={200} y={200} />
</Stage>,
document.getElementById("container")
);
import { render } from "react-dom";
import { AppContext, Sprite, Stage } from "react-pixi-fiber";
import bunny from "./bunny.png";
class RotatingBunny extends Component {
state = {
rotation: 0,
};
componentDidMount() {
// Note that `app` prop is coming directly from AppContext.Consumer
this.props.app.ticker.add(this.animate);
}
componentWillUnmount() {
this.props.app.ticker.remove(this.animate);
}
animate = delta => {
this.setState(state => ({
rotation: state.rotation + 0.1 * delta,
}));
};
render() {
return (
<Sprite
{...this.props}
texture={PIXI.Texture.fromImage(bunny)}
rotation={this.state.rotation}
/>
);
}
}
RotatingBunny.propTypes = {
app: PropTypes.object.isRequired,
};
render(
<Stage width={800} height={600} options={{ backgroundColor: 0x10bb99 }}>
<AppContext.Consumer>
{app => (
<RotatingBunny app={app} x={200} y={200} />
)}
</AppContext.Consumer>
</Stage>,
document.getElementById("container")
);
This approach is not recommended as it is easier to just use withApp
HoC mentioned above.
import { render } from "react-dom";
import { Sprite, Stage } from "react-pixi-fiber";
import bunny from "./bunny.png";
class RotatingBunny extends Component {
state = {
rotation: 0,
};
componentDidMount() {
// Note that `app` is coming from context, NOT from props
this.context.app.ticker.add(this.animate);
}
componentWillUnmount() {
this.context.app.ticker.remove(this.animate);
}
animate = delta => {
this.setState(state => ({
rotation: state.rotation + 0.1 * delta,
}));
};
render() {
return (
<Sprite
{...this.props}
texture={PIXI.Texture.fromImage(bunny)}
rotation={this.state.rotation}
/>
);
}
}
// Note that here we tell React to apply `app` via legacy Context API
RotatingBunny.childContextTypes = {
app: PropTypes.object,
};
render(
<Stage width={800} height={600} options={{ backgroundColor: 0x10bb99 }}>
<RotatingBunny x={200} y={200} />
</Stage>,
document.getElementById("container")
);
ReactPixiFiber can recognize your custom components using API compatible with react-pixi
.
CustomPIXIComponent(behavior, type)
accepts a behavior
object with the following 4 properties and a type
string.
Use this to create an instance of [PIXI.DisplayObject].
This is your entry point to custom components and the only required method. Can be also passed as behavior
of type function
to CustomPIXIComponent
.
Use this to apply newProps
to your Component
in a custom way.
Note: this replaces the default method of transfering props
to the specified displayObject
. Call this.applyDisplayObjectProps(oldProps,newProps)
inside your customApplyProps
method if you want that.
Use this to do something after displayObject
is attached, which happens after componentDidMount
lifecycle method.
Use this to do something (usually cleanup) before detaching, which happens before componentWillUnmount
lifecycle method.
For example, this is how you could implement Rectangle
component:
// components/Rectangle.js
import { CustomPIXIComponent } from "react-pixi-fiber";
import * as PIXI from "pixi.js";
const TYPE = "Rectangle";
export const behavior = {
customDisplayObject: props => new PIXI.Graphics(),
customApplyProps: function(instance, oldProps, newProps) {
const { fill, x, y, width, height } = newProps;
instance.clear();
instance.beginFill(fill);
instance.drawRect(x, y, width, height);
instance.endFill();
}
};
export default CustomPIXIComponent(behavior, TYPE);
// App.js
import { render } from "react-pixi-fiber";
import * as PIXI from "pixi.js";
import Rectangle from "./components/Rectangle"
// Setup PixiJS Application
const canvasElement = document.getElementById("container")
const app = new PIXI.Application(800, 600, {
view: canvasElement
});
render(
<Rectangle
x={250}
y={200}
width={300}
height={200}
fill={0xFFFF00}
/>,
app.stage
);
Yes, it is easy, read migration guide.
Not yet.
The main purpose of this repository is to be able to render PixiJS objects inside React 16 Fiber architecture.
Development of React Pixi Fiber happens in the open on GitHub, and I would be grateful to the community for any contributions, including bug reports and suggestions.
Read below to learn how you can take part in improving React Pixi Fiber.
React Pixi Fiber has adopted a Contributor Covenant Code of Conduct that we expect project participants to adhere to. Please read the full text so that you can understand what actions will and will not be tolerated.
Read the contributing guide to learn about our development process, how to propose bugfixes and improvements, and how to build and test your changes to React Pixi Fiber.
You can help others and discuss in our gitter channel.
ReactPixiFiber is MIT licensed.
For making PIXI available in React for the first time.
For deeply explaining the concepts of Fiber architecture.
For helping me understand how to build an actual renderer.
On which this renderer was initially based.
React Contributors
For making an awesome project structure and documentation that is used in similar fashon here.