Parallax scrolling

Pick a foreground landmark and a distant one. Which crosses the screen first? Switch parallax off and compare.
Interactive demo#TilesAndWorldsINSIDE
Far-away layers scroll slower than near ones, faking depth.
Look out of a train window: fences rush by, hills drift, the moon barely moves. Scroll each background layer at its own speed and a flat scene gains distance. Turn it off and everything slides as one painted board.
Code
namespace parallax_lesson;
import stage;
import std;
import math;
fn wrap(x: f64, period: f64) -> f64 { (x % period + period) % period }
export "parallax" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
let enabled = toggle("on");
let scroll = time * 46;
let drift = wrap(scroll * (if enabled { 0.12 } else { 1.0 }), 320);
artwork("valley", -drift, 0, 320, H);
artwork("valley", 320 - drift, 0, 320, H);
for i in 7 {
let x = wrap(i * 46 - scroll * (if enabled { 0.4 } else { 1.0 }), 322) - 30;
tree(x, GY + 2, 1.0 + (i % 3) * 0.24);
}
for i in 3 {
tree(wrap(i * 90 - scroll, 270) - 30, GY, 2);
}
for i in 22 {
let x = floor(i * 8 - wrap(scroll, 8) + 0.5);
tile_image(0, x, GY, 1);
tile_image(1, x, GY + 8, 1);
tile_image(1, x, GY + 16, 1);
}
actor(60, GY, walk(time));
}

Comments
No comments yet.