如何在 javascript 中向后移动 canvas?

How to move the canvas backward in javascript?

我在想当盒子达到1000px时如何向后移动(canvas.width),但我不知道它在If条件后如何解决,所以这是我的脚本,任何人都可以帮助我吗?

var canvas = document.querySelector("canvas");
var canvasCT = canvas.getContext("2d");
var x = 50;

function draw() {
    canvas.width = canvas.width;
    canvasCT.fillStyle = "blue";
    canvasCT.fillRect(x, 50, 100, 100);
}

function run() {
    draw();
    x += 5 ;
    if (x > 1000) {
    ......
    }
}
setInterval(run, 10);

var canvas = document.querySelector("canvas");
var canvasCT = canvas.getContext("2d");
var x = 50;
var speed = 5;

function draw() {
    canvas.width = canvas.width;
    canvasCT.fillStyle = "blue";
    canvasCT.fillRect(x, 50, 100, 100);
}

function run() {
    draw();
    x += speed;
    if (x > 1000 || x < 0) {
      speed = -speed;
    }
}
setInterval(run, 10);
<canvas width=1000></canvas>