How can I move a object with Javascript?

create a function and then use requestAnimationFrame(function name) inside it and the have your cars x location add on 1 each frame or to make it faster put it higher than on heres a quick example with a rect

let speed = 2;
let x = 20;
let y = 20;

function car(x, y) {
    ctx.beginPath();
    ctx.rect(x, y, 150, 100);
    ctx.stroke();
   
    
}
 
    
    
function move() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    car(x, y)
    x += speed;
   
 requestAnimationFrame(move)

}
    
   move() 
1 Like