HLJ 发布于
2018-08-20 12:29:33

HTML5 Canvas Scale缩放函数

要缩放HTML5 Canvas,我们可以使用scale()转换方法来缩放画布上下文的x和y组件。
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);

      // scale y component
      context.scale(1, 0.5);

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