HTML Canvas - rotate() 方法



HTML Canvas rotate() 方法将可用图形旋转到变换矩阵中用作特定角度的上下文对象。

语法

以下是 HTML Canvas restore() 方法的语法:

CanvasRenderingContext2D.rotate(angle);

参数

以下是该方法使用的参数:

序号 参数和说明
1

角度

将上下文对象旋转到的角度(以弧度为单位)。

返回值

它返回一个 CanvasRenderingContext2D 对象使用的旋转变换对象。

示例

以下示例取在 Canvas 上绘制的一条线,并将其旋转到作为参数传递给 HTML Canvas rotate() 方法的特定角度。

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Reference API</title>
   <style>
      body {
         margin: 10px;
         padding: 10px;
      }
   </style>
</head>
<body>
   <canvas id="canvas" width="250" height="200" style="border: 1px solid black;"></canvas>
   <script>
      var canvas = document.getElementById('canvas');
      var context = canvas.getContext('2d');
      context.beginPath();
      context.moveTo(25, 25);
      context.lineTo(150, 25);
      context.stroke();
      context.closePath();
      context.beginPath();
      context.rotate(18 * Math.PI / 180);
      context.moveTo(100, 100);
      context.lineTo(225, 100);
      context.stroke();
      context.closePath();
   </script>
</body>
</html>

输出

以下代码在网页上返回的输出为:

HTML Canvas Rotate method

示例

以下示例使用 rotate() 方法旋转在 Canvas 元素上绘制的正方形。

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Reference API</title>
   <style>
      body {
         margin: 10px;
         padding: 10px;
      }
   </style>
</head>
<body>
   <canvas id="canvas" width="300" height="450" style="border: 1px solid black;"></canvas>
   <script>
      var canvas = document.getElementById('canvas');
      var context = canvas.getContext('2d');
      context.strokeRect(25, 20, 150, 100);
      context.rotate(50 * Math.PI / 180);
      context.strokeRect(250, 10, 150, 100);
   </script>
</body>
</html>

输出

以下代码在网页上返回的输出为:

HTML Canvas Rotate method
html_canvas_shadows_and_transformations.htm
广告
© . All rights reserved.