HLJ 发布于
2018-08-20 12:39:59

HTML5 Canvas translate函数和scale函数文字镜像转换

要使用HTML5 Canvas创建镜像变换,我们可以在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');

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

      // flip context horizontally
      context.scale(-1, 1);

      context.font = '30pt Calibri';
      context.textAlign = 'center';
      context.fillStyle = 'blue';
      context.fillText('Hello World!', 0, 0);
    </script>
  </body>
</html>
当前文章内容为原创转载请注明出处:http://www.good1230.com/detail/2018-08-20/164.html
最后生成于 2023-06-18 18:38:04
此内容有帮助 ?
0