Skip to content

Commit

Permalink
feat: init car [#1]
Browse files Browse the repository at this point in the history
  • Loading branch information
d3p1 committed Oct 12, 2024
1 parent 0971f2f commit c94c589
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 4 deletions.
35 changes: 31 additions & 4 deletions docs/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,57 @@
* @description App
* @author C. M. de Picciotto <d3p1@d3p1.dev> (https://d3p1.dev/)
*/
import Car from './core/car.js'

export default class App {
/**
* @type {HTMLCanvasElement}
* @type {Car}
*/
canvas
car

/**
* @type {CanvasRenderingContext2D}
*/
context

/**
* @type {HTMLCanvasElement}
*/
canvas

/**
* Constructor
*/
constructor() {
constructor(car = new Car(0, 0, 50, 100)) {
this.#initCanvas()
this.#initCar(car)
}

/**
* Run
*
* @returns {void}
*/
run() {}
run(t = 0) {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height)

this.car.update(t)
this.car.draw(this.context)

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

/**
* Init car
*
* @param {Car} car
* @returns {void}
*/
#initCar(car) {
this.car = car
this.car.centerX = this.canvas.width / 2
this.car.centerY = this.canvas.height / 2
}

/**
* Init canvas
Expand Down
70 changes: 70 additions & 0 deletions docs/js/core/car.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @description Car
* @author C. M. de Picciotto <d3p1@d3p1.dev> (https://d3p1.dev/)
*/
export default class Car {
/**
* @type {number}
*/
centerX

/**
* @type {number}
*/
centerY

/**
* @type {number}
*/
width

/**
* @type {number}
*/
height

/**
* Constructor
*
* @param {number} centerX
* @param {number} centerY
* @param {number} width
* @param {number} height
*/
constructor(centerX, centerY, width, height) {
this.centerX = centerX
this.centerY = centerY
this.width = width
this.height = height
}

/**
* Update car
*
* @param {number} t
* @returns {void}
*/
update(t) {
console.log(t)
}

/**
* Draw car
*
* @param {CanvasRenderingContext2D} context
* @returns {void}
* @note Just in case, it is used `beginPath()`, but I think that
* this method is only necessary when it is drawn a path using
* drawing commands like `lineTo()`, `arc()`, etc.
*/
draw(context) {
context.beginPath()
context.rect(
this.centerX - this.width / 2,
this.centerY - this.height / 2,
this.width,
this.height,
)
context.fill()
}
}

0 comments on commit c94c589

Please sign in to comment.