Layered scrolling backgrounds that add depth. Parallax factors per layer, seamless horizontal and vertical tiling, auto-scrolling layers (clouds, runners), pixel-perfect scrolling without seams or shimmer, foreground layers over the player, depth tinting and fog, vertical parallax, layer sizes and performance, and parallax in Phaser (tileSprite), Pixi (TilingSprite) and three.js. Use for side-s...
Download skill (.zip) Files on GitHubParallax backgrounds
Canvas 2D (drop-in)
// layers far → near. factor 0 = fixed sky, 1 = moves with the world, >1 = foreground (in front of the player)
const LAYERS = [
{ img: images.sky, factor: 0, y: 0 },
{ img: images.mountains, factor: 0.2, y: 40 },
{ img: images.clouds, factor: 0.35, y: 10, auto: 12 }, // also drifts 12 px/s
{ img: images.trees, factor: 0.6, y: 120 },
];
const FOREGROUND = [{ img: images.grass, factor: 1.4, y: 250 }];
function drawLayers(ctx, layers, camX, camY, viewW, time) {
for (const L of layers) {
const w = L.img.width;
let off = (camX * L.factor + (L.auto ?? 0) * time) % w; // wrap
if (off < 0) off += w;
const x0 = -Math.round(off); // integer: no shimmer or seams
const y = Math.round(L.y - camY * (L.factorY ?? L.factor * 0.3)); // gentler vertical parallax
for (let x = x0; x < viewW; x += w) ctx.drawImage(L.img, x, y);
}
}
// render (screen space, before cam.apply): drawLayers(ctx, LAYERS, cam.left, cam.top, W, time)
// … world … then drawLayers(ctx, FOREGROUND, cam.left, cam.top, W, time)
Rules
- Layer images tile seamlessly left to right (check the edges match) and are at least as wide as the view.
- Round the final x per layer (pixel art). For smooth art, keep sub-pixel but use one consistent rounding for everything.
- Factors: sky 0, far 0.1–0.25, mid 0.4–0.6, near 0.7–0.9, foreground 1.2–1.5 (sparse, so it doesn’t hide the action).
- Depth: far layers lighter, bluer and lower contrast (atmospheric perspective). That’s what really sells depth.
- 3–5 layers is plenty. Big layer images: draw them once into a cached canvas at the right scale.
- Runners: auto-scroll by game speed (
camX = distance), and speed changes stay smooth. - Vertical games (climbing, shooters): the same math on y.
Engines
- Phaser:
this.add.tileSprite(0, 0, W, H, "mountains").setOrigin(0).setScrollFactor(0); each framelayer.tilePositionX = cam.scrollX * 0.2. - Pixi:
new TilingSprite({ texture, width: W, height: H });layer.tilePosition.x = -camX * factor. - three.js 2.5D: real depth: planes at different z with a perspective camera, or an orthographic camera and manual offsets.
Check
Scroll the full level width: no seams, gaps or jitter, the foreground never hides enemies or bullets, and it stays at 60 fps on phones (canvas-performance).