Pregunta

Iam searching for a solution, in html5-canvas how to clear the scale function? If I scaled a shape, for next shape it should not scale or scale less thank you

  <!DOCTYPE html>
       <html>
       <body>



     <canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
       Your browser does not support the HTML5 canvas tag.</canvas>

       <script>

      var c=document.getElementById("myCanvas");
      var ctx=c.getContext("2d");

      // first draw
      ctx.strokeRect(5,5,25,15);

      //scale
      ctx.scale(2,2);
      ctx.strokeRect(5,5,25,15);

      // how to clear scale this ? It should draw like first 
      ctx.scale(-1,-1); // this is not working
      ctx.strokeRect(5,5,25,15);

    </script> 

   </body>
</html>
¿Fue útil?

Solución

Since you scaled by 2, you can un-scale by 1/2

 // first draw
  ctx.strokeRect(5,5,25,15);

  //scale
  ctx.scale(2,2);
  ctx.strokeRect(5,5,25,15);

  // un-scale
  ctx.scale(.5,.5); // unscale by 1/2
  ctx.strokeRect(5,5,25,15);

Another example, if you had scaled by 3 you would unscale by 1/3

// scale

ctx.scale(3,3);

// unscale

ctx.scale(1/3,1/3);

Otros consejos

I think the most convenient solution for your problem is to save the context and then restore it after you are done drawing the stuff you want to be scaled.

// Draw the first rect with size 20x20
ctx.strokeRect(50,75,20,20);

// Save context
ctx.save();

// Double the scale
ctx.scale(2, 2);

// Draw the second rect with the same size
ctx.strokeRect(65,35,20,20);

// Restore context
ctx.restore();

// Draw the last rect, still with size 20x20
ctx.strokeRect(220,75,20,20);

Example: http://jsfiddle.net/8tXVL/

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top