Untitled
unknown
plain_text
3 years ago
703 B
8
Indexable
// initialize canvas and context
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// create car object with starting position and speed
const car = {
x: 50,
y: 50,
speed: 5
};
// create function to draw car on canvas
function drawCar(x, y) {
ctx.fillStyle = 'red';
ctx.fillRect(x, y, 50, 25);
}
// create function to update car position and draw on canvas
function update() {
// clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// update car position based on speed
car.x += car.speed;
// draw car on canvas
drawCar(car.x, car.y);
// call update function again
requestAnimationFrame(update);
}
// start game
update();
Editor is loading...