CanvasRenderingContext2D.scrollPathIntoView()

实验性: 这是一项实验性技术
在将其用于生产之前,请仔细检查浏览器兼容性表格

CanvasRenderingContext2D.scrollPathIntoView() 是 Canvas 2D API 将当前或给定的路径滚动到窗口的方法。类似于 Element.scrollIntoView()

语法

void ctx.scrollPathIntoView();
void ctx.scrollPathIntoView(path);

参数

path

使用的Path2D 路径。

示例

使用 scrollPathIntoView 方法

这是一段使用 scrollPathIntoView 方法的简单的代码片段。

HTML

html
<canvas id="canvas"></canvas>

JavaScript

js
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");

ctx.beginPath();
ctx.fillRect(10, 10, 30, 30);
ctx.scrollPathIntoView();

可编辑示例

修改下面的代码并在线查看 canvas 的变化:

规范

Specification
HTML Standard
# dom-context-2d-scrollpathintoview-dev

浏览器兼容性

BCD tables only load in the browser

参见