Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

83
Views
How to move the canvas backward in javascript?

I try to think how the box can move backward when it hit 1000px (canvas.width) ,but I don’t know how it solves after If condition, so there’s my script, anyone can help me ?

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);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!