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

87
Views
why my object don't change position when i add value
<hmtl>
<head>
<!--<script src='main.js'></script>-->
</head>
<body>
<canvas id='myCanvas'> </canvas>
<script>
function shape(x,y){
this.x=x;
this.y=y;
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "#FF0000";
ctx.fillRect( this.x, this.y, 50, 50);
}
}
var sqr=new shape(100,100)
sqr.x+=100
</script>
</body>
</hmtl>

why i add 100 to x and hope its positon will be (200,100) but when i open live sever it position still remain (100,100)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Because it will only change the value of x, you have to draw it again on the canvas and before drawing again we have to clear canvas using clearRect

<html>
   <head>
      <!--<script src='main.js'></script>-->
   </head>
   <body>
      <canvas id='myCanvas'> </canvas>
      <script>
         function shape(x,y){
              this.x=x;
              this.y=y;
              var canvas = document.getElementById("myCanvas");
              var ctx = canvas.getContext("2d");
              this.draw = ()=>{
              ctx.clearRect(0, 0, canvas.width, canvas.height);
              ctx.fillStyle = "#FF0000";
              ctx.fillRect( this.x, this.y, 50, 50);
              ctx.stroke();
            }
         }
         var sqr=new shape(100,100)
         sqr.draw();
         sqr.x+=-100
         sqr.draw();
      </script>
   </body>
</html>

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!