Parallax backgrounds/endless scroll
See also:
System:
needs a single variable to control the scrolling – otherwise there may be a gap which changes over time…
Covered in AGDwJS p 316 (Creating the Scrolling Background)
Requirements:
- needs to be dynamic speed
- needs to not have gaps
- layers move at different relative speeds
- functionality easily allows for going in either direction
This one works by –
2 instances of image
one next to another
when image 2 gets to 0, reset both instances: image 1 to 0, image 2 to width of
Layers
(JavaScript Game Development Course for Beginners)
setup in html
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<meta http-equiv=”X-UA-Compatible” content=”IE=edge”>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<title>Parallax Backgrounds</title>
<link rel=”stylesheet” href=”style.css”>
</head>
<body>
<canvas id=”canvas1″></canvas>
<script src=”script.js”></script>
</body>
</html>
css
body {
background: black;
}
#canvas1 {
position: absolute;
border: 3px solid white;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 800px;
height: 700px;
}
setting up in js
const canvas = document.getElementById(‘canvas1’);
const ctx = canvas.getContext(‘2d’);
const CANVAS_WIDTH = canvas.width = 800;
const CANVAS_HEIGHT = canvas.height = 800;
How to do in AGDwHTML5 (rudimentary one on p316…)
Resources
the kittykat thing…