📎 Webclip
Building a Parallax Scrolling Game with Pixi.js
The article walks through a basic Pixi.js parallax scroller. It starts with an HTML page and canvas, adds the Pixi.js library, initializes a stage and renderer, then loads background textures as sprites.
Reading notes#
- A basic HTML page uses a centered 512 by 384 canvas and loads
pixi.dev.jsfrom a local project folder. - The page calls
init()on body load, andinit()is used as the main entry point for the scroller. - Pixi.js is initialized by creating a
PIXI.Stageand a renderer throughPIXI.autoDetectRenderer(). - The stage is rendered to the canvas with
renderer.render(stage). - Display objects are added to the stage with
addChild(), which controls stacking order. - The tutorial first uses
PIXI.Spriteto place a far background layer at the top-left of the stage. - A mid layer is added below it by positioning it at
y = 128and adding it after the far layer. - Parallax motion is introduced with an
update()loop driven byrequestAnimFrame(). - The far and mid layers move at different speeds to create depth.
PIXI.TilingSpriteis then used so the background textures wrap seamlessly instead of leaving gaps.- The scrolling effect is produced by changing
tilePosition.xinstead of moving the sprite itself. - The conclusion says the next steps are viewport and world positioning, adding a foreground layer, and refactoring away from global variables.
