What is pixi.js?
Pixi.js is a fast 2D rendering engine that uses WebGL and HTML5 Canvas to create interactive graphics and animations. It is widely used for creating games, interactive applications, and other visual content on the web.
What are pixi.js's main functionalities?
Creating a Basic Application
This code initializes a new Pixi.js application with a specified width and height, and appends the canvas element to the document body.
const app = new PIXI.Application({ width: 800, height: 600 });
document.body.appendChild(app.view);
Loading and Displaying a Sprite
This code loads an image and creates a sprite from it. The sprite is then positioned at the center of the canvas and added to the stage.
PIXI.Loader.shared.add('bunny', 'path/to/bunny.png').load((loader, resources) => {
const bunny = new PIXI.Sprite(resources.bunny.texture);
bunny.x = app.renderer.width / 2;
bunny.y = app.renderer.height / 2;
bunny.anchor.set(0.5);
app.stage.addChild(bunny);
});
Animating a Sprite
This code adds an animation loop that rotates the sprite continuously. The `delta` parameter ensures smooth animation regardless of the frame rate.
app.ticker.add((delta) => {
bunny.rotation += 0.1 * delta;
});
Creating Graphics
This code creates a new graphics object, draws a filled rectangle, and adds it to the stage.
const graphics = new PIXI.Graphics();
graphics.beginFill(0xDE3249);
graphics.drawRect(50, 50, 100, 100);
graphics.endFill();
app.stage.addChild(graphics);
Other packages similar to pixi.js
three
Three.js is a popular 3D library that provides a wide range of features for creating 3D graphics and animations. While Pixi.js focuses on 2D rendering, Three.js is more suitable for complex 3D scenes and visualizations.
phaser
Phaser is a fast, robust, and versatile game framework that supports both 2D and 3D rendering. It is more game-oriented compared to Pixi.js, offering built-in physics, input handling, and other game development features.
konva
Konva is a 2D canvas library that provides a high-level API for creating complex shapes, animations, and interactions. It is similar to Pixi.js but focuses more on ease of use and simplicity for creating interactive graphics.
Pixi Renderer
*** IMPORTANT - V2 API CHANGES ***
A heads up for anyone updating their version of pixi.js to version 2, as we have changed a couple of bits that you need to be aware of. Fortunately, there are only two changes, and both are small.
1: Creating a renderer now accepts an options parameter that you can add specific settings to:
// an optional object that contains the settings for the renderer
var options = {
view:myCanvas,
resolution:1
};
var renderer = new PIXI.WebGLRenderer(800, 600, options)
2: A PIXI.RenderTexture
now accepts a PIXI.Matrix
as its second parameter instead of a point. This gives you much more flexibility:
myRenderTexture.render(myDisplayObject, myMatrix)
Check out the docs for more info!
JavaScript 2D Renderer
The aim of this project is to provide a fast lightweight 2D library that works
across all devices. The Pixi renderer allows everyone to enjoy the power of
hardware acceleration without prior knowledge of webGL. Also, it's fast.
If you’re interested in pixi.js then feel free to follow me on twitter
(@doormat23) and I will keep you posted! And
of course check back on our site as
any breakthroughs will be posted up there too!
Demos
Thanks to @photonstorm for providing those
last 2 examples and allowing us to share the source code :)
Docs
Documentation can be found here
Resources
Tutorials and other helpful bits
Pixi.js forum
Road Map
- Create a Typescript definition file for Pixi.js
- Implement Flash animation to pixi
- Update Loader so that it support XHR2 if it is available
- Improve the Documentation of the Project
- Create an Asset Loader Tutorial
- Create a MovieClip Tutorial
- Create a small game Tutorial
Contribute
Want to be part of the pixi.js project? Great! All are welcome! We will get there quicker together :)
Whether you find a bug, have a great feature request or you fancy owning a task from the road map above feel free to get in touch.
Make sure to read the Contributing Guide
before submitting changes.
How to build
PixiJS is built with Grunt. If you don't already have this, go install Node and NPM then install the Grunt Command Line.
$> npm install -g grunt-cli
Then, in the folder where you have downloaded the source, install the build dependencies using npm:
$> npm install
Then build:
$> grunt
This will create a minified version at bin/pixi.js and a non-minified version at bin/pixi.dev.js.
It also copies the non-minified version to the examples.
Current features
- WebGL renderer (with automatic smart batching allowing for REALLY fast performance)
- Canvas renderer (Fastest in town!)
- Full scene graph
- Super easy to use API (similar to the flash display list API)
- Support for texture atlases
- Asset loader / sprite sheet loader
- Auto-detect which renderer should be used
- Full Mouse and Multi-touch Interaction
- Text
- BitmapFont text
- Multiline Text
- Render Texture
- Spine support
- Primitive Drawing
- Masking
- Filters
Usage
var renderer = new PIXI.WebGLRenderer(800, 600);
document.body.appendChild(renderer.view);
var stage = new PIXI.Stage;
var bunnyTexture = PIXI.Texture.fromImage("bunny.png");
var bunny = new PIXI.Sprite(bunnyTexture);
bunny.position.x = 400;
bunny.position.y = 300;
bunny.scale.x = 2;
bunny.scale.y = 2;
stage.addChild(bunny);
requestAnimationFrame(animate);
function animate() {
bunny.rotation += 0.01;
renderer.render(stage);
requestAnimationFrame(animate);
}
This content is released under the (http://opensource.org/licenses/MIT) MIT License.