CanvasRenderingContext2D .isPointInStroke() method of the Canvas 2D API reports whether or not the specified point is inside the area contained by the stroking of a path.

句法

ctx.isPointInStroke(x, y);
ctx.isPointInStroke(path, x, y);
					

参数

x

The x-axis coordinate of the point to check.

y

The y-axis coordinate of the point to check.

path
Path2D path to check against. If unspecified, the current path is used.

返回值

布尔
A Boolean, which is true if the point is inside the area contained by the stroking of a path, otherwise false .

范例

Checking a point in the current path

此范例使用 isPointInStroke() method to check if a point is within the area of the current path's stroke.

HTML

<canvas id="canvas"></canvas>
<p>In stroke: <code id="result">false</code></p>
					

JavaScript

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const result = document.getElementById('result');
ctx.rect(10, 10, 100, 100);
ctx.stroke();
result.innerText = ctx.isPointInStroke(50, 10);
					

结果

Checking a point in the specified path

Whenever you move the mouse, this example checks whether the cursor is in the stroke of an elliptical Path2D path. If yes, the ellipse's stroke becomes green, otherwise it is red.

HTML

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

JavaScript

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// Create ellipse
const ellipse = new Path2D();
ellipse.ellipse(150, 75, 40, 60, Math.PI * .25, 0, 2 * Math.PI);
ctx.lineWidth = 25;
ctx.strokeStyle = 'red';
ctx.fill(ellipse);
ctx.stroke(ellipse);
// Listen for mouse moves
canvas.addEventListener('mousemove', function(event) {
  // Check whether point is inside ellipse's stroke
  if (ctx.isPointInStroke(ellipse, event.offsetX, event.offsetY)) {
    ctx.strokeStyle = 'green';
  }
  else {
    ctx.strokeStyle = 'red';
  }
  // Draw ellipse
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.fill(ellipse);
  ctx.stroke(ellipse);
});
					

结果

规范

规范 状态 注释
HTML 实时标准
The definition of 'CanvasRenderingContext2D.isPointInStroke' in that specification.
实时标准

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
isPointInStroke Chrome Yes Edge 79 Firefox 20
20
不支持 19 — 20 Prefixed
Prefixed Implemented with the vendor prefix: moz
IE No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 20
20
不支持 19 — 20 Prefixed
Prefixed Implemented with the vendor prefix: moz
Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
路径 参数 Chrome Yes Edge 79 Firefox 31 IE No Opera Yes Safari No WebView Android Yes Chrome Android Yes Firefox Android 31 Opera Android ? Safari iOS ? Samsung Internet Android Yes

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改: