I need help moving a rectangle to the left and right of the screen

So I am trying to cone a game where you need to use buttons to move left or right smoothly but I am trying to get the button to work but it isn’t here’s the code

const canvas=document.getElementById("myCanvas");
canvas.height=window.innerHeight;
canvas.width=400;

const ctx = canvas.getContext("2d")
let x = canvas.width/1.25
let car = ctx.fillRect(x, canvas.height/2, 75, 150)
while (true){
    let z = 0

    let leftButton = document.getElementById("left_button")
    leftButton.addEventListener ("click", function() {
        console.log("active")
    x+=1;
        
    });
    if (z = 1){
        break
    }
}


Can anyone help?

I tried to fix it a lot of times but it just didn’t work but it didn’t work.