【问题标题】:html5 canvas clear scale functionhtml5 canvas清除比例功能
【发布时间】:2014-04-01 23:05:38
【问题描述】:

我正在寻找解决方案,在html5-canvas中如何清除缩放功能? 如果我缩放了一个形状,对于下一个形状,它不应该缩放或缩小 谢谢

  <!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>

【问题讨论】:

    标签: html canvas html5-canvas scale


    【解决方案1】:

    由于您按 2 缩放,因此您可以按 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);
    

    另一个例子,如果你已经缩放了 3,你会取消缩放 1/3

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

    【讨论】:

      【解决方案2】:

      我认为解决您的问题最方便的方法是保存上下文,然后在绘制完要缩放的内容后恢复它。

      // 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);
      

      示例:http://jsfiddle.net/8tXVL/

      【讨论】:

        猜你喜欢
        • 2016-12-19
        • 1970-01-01
        • 1970-01-01
        • 2012-08-09
        • 2012-08-09
        • 1970-01-01
        • 2012-05-10
        • 2012-06-23
        • 1970-01-01
        相关资源
        最近更新 更多