Skip to content

Commit

Permalink
feat: add camera movement [#1]
Browse files Browse the repository at this point in the history
  • Loading branch information
d3p1 committed Oct 14, 2024
1 parent b68c115 commit b6ca609
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion docs/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,25 @@ export default class App {
* @param {number} t Elapsed time in milliseconds
* since the init of the animation
* @returns {void}
* @todo Encapsulate logic of context `save()`, `translate()`
* and `restore()` related to camera movement
*/
run(t = 0) {
this.timer.update(t)

this.context.clearRect(0, 0, this.canvas.width, this.canvas.height)

this.context.save()
this.context.translate(0, -this.car.centerY * 0.3)

this.car.update(this.timer.delta)
this.world.update(this.timer.delta)

this.road.draw(this.context)
this.car.draw(this.context)

this.context.restore()

requestAnimationFrame(this.run.bind(this))
}

Expand Down Expand Up @@ -114,7 +121,7 @@ export default class App {
#initCar() {
this.car = new Car(
this.road.getLaneCenterFromLaneIndex(2),
this.canvas.height / 2,
this.canvas.height - this.canvas.height * 0.1,
50,
100,
500,
Expand Down

0 comments on commit b6ca609

Please sign in to comment.