Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

88
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda