HTML 画布 - scrollPathIntoView() 方法



HTML 画布 scrollPathIntoView() 方法采用 CanvasRenderingContext2D 接口上下文对象并滚动所给路径以使其可见。

语法

以下是 HTML 画布 scrollPathIntoView() 方法的语法 −

CanvasRenderingContext2D.scrollPathIntoView(cur_path);

参数

以下是此方法的参数列表 −

序列号 参数和说明
1 cur_path

将应用该方法的路径。

返回值

当向 CanvasRenderingContext2D 接口上下文对象应用以下方法时,该对象的路径在视图中可用。

示例 1

以下示例展示 HTML 画布 scrollPathIntoView() 方法如何在画布元素内使用。

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Reference API</title>
   <style>
      body {
         margin: 10px;
         padding: 10px;
      }
   </style>
</head>
<body onload="Context();">
   <canvas id="canvas" width="200" height="200" style="border: 1px solid black;"></canvas>
   <script>
      function Context() {
         var canvas = document.getElementById("canvas");
         var context = canvas.getContext("2d");
         context.beginPath();
         context.moveTo(100, 50);
         context.lineTo(50, 100);
         context.lineTo(150, 100);
         context.lineTo(100, 50);
         context.fillStyle = 'brown';
         context.fill();
         context.closePath();
         context.strokeRect(30, 30, 150, 100);
         context.scrollPathIntoView();
      }
   </script>
</body>
</html>

输出

上述代码在网页上返回的输出为 −

HTML Canvas ScrollPathIntoView Method

示例 2

以下示例展示当 scrollPathIntoView() 方法绘制的形状边界比原始画布元素大时,如何隐藏画布元素中绘制的形状。

<!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="200" style="border: 1px solid black;"></canvas>
   <script>
      var canvas = document.getElementById('canvas');
      var context = canvas.getContext('2d');
      context.beginPath();
      // remove the below line to observe the difference
      context.scrollPathIntoView();
      context.strokeRect(30, 30, 350, 200);
   </script>
</body>
</html>

输出

上述代码在网页上返回的输出为 −

HTML Canvas ScrollPathIntoView Method
html_canvas_paths.htm
广告
© . All rights reserved.