HLJ 发布于
2018-08-20 12:42:30

HTML5 Canvas setTransform函数重置转换图像

要重置HTML5 Canvas变换矩阵,我们可以使用setTransform()方法使用以下约定将变换矩阵设置为其默认状态:
html代码:
<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="578" height="200"></canvas>
    <script>
      var canvas = document.getElementById('myCanvas');
      var context = canvas.getContext('2d');
      var rectWidth = 150;
      var rectHeight = 75;

      // translate context to center of canvas
      context.translate(canvas.width / 2, canvas.height / 2);

      context.fillStyle = 'blue';
      context.fillRect(-rectWidth / 2, rectHeight / -2, rectWidth, rectHeight);

      // Reset Transform
      // 1 0 0
      // 0 1 0
      // 0 0 1

      // apply custom transform
      context.setTransform(1, 0, 0, 1, 0, 0);

      context.fillStyle = 'red';
      context.fillRect(0, 0, rectWidth, rectHeight);
    </script>
  </body>
</html>
当前文章内容为原创转载请注明出处:http://www.good1230.com/detail/2018-08-20/165.html
最后生成于 2023-06-18 18:38:05
此内容有帮助 ?
0