CanvasRenderingContext2D
.isPointInPath()
method of the Canvas 2D API reports whether or not the specified point is contained in the current path.
ctx.isPointInPath(x, y [, fillRule]); ctx.isPointInPath(path, x, y [, fillRule]);
x
The x-axis coordinate of the point to check, unaffected by the current transformation of the context.
y
The y-axis coordinate of the point to check, unaffected by the current transformation of the context.
fillRule
"nonzero"
:
non-zero winding rule
. Default rule.
"evenodd"
:
even-odd winding rule
.
path
Path2D
path to check against. If unspecified, the current path is used.
布尔
true
if the specified point is contained in the current or specified path, otherwise
false
.
此范例使用
isPointInPath()
method to check if a point is within the current path.
<canvas id="canvas"></canvas> <p>In path: <code id="result">false</code></p>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const result = document.getElementById('result');
ctx.rect(10, 10, 100, 100);
ctx.fill();
result.innerText = ctx.isPointInPath(30, 70);
Whenever you move the mouse, this example checks whether the cursor is in a circular
Path2D
path. If yes, the circle becomes green, otherwise it is red.
<canvas id="canvas"></canvas>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// Create circle
const circle = new Path2D();
circle.arc(150, 75, 50, 0, 2 * Math.PI);
ctx.fillStyle = 'red';
ctx.fill(circle);
// Listen for mouse moves
canvas.addEventListener('mousemove', function(event) {
// Check whether point is inside circle
if (ctx.isPointInPath(circle, event.offsetX, event.offsetY)) {
ctx.fillStyle = 'green';
}
else {
ctx.fillStyle = 'red';
}
// Draw circle
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fill(circle);
});
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'CanvasRenderingContext2D.isPointInPath' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
isPointInPath
|
Chrome Yes | Edge 12 | Firefox 2 | IE Yes | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 4 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
路径
参数
|
Chrome Yes | Edge ≤18 | 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 |
完整支持
不支持
兼容性未知
CanvasRenderingContext2D
CanvasRenderingContext2D
addHitRegion()
arc()
arcTo()
beginPath()
bezierCurveTo()
clearHitRegions()
clearRect()
clip()
closePath()
createImageData()
createLinearGradient()
createPattern()
createRadialGradient()
drawFocusIfNeeded()
drawImage()
drawWidgetAsOnScreen()
drawWindow()
ellipse()
fill()
fillRect()
fillText()
getImageData()
getLineDash()
getTransform()
isPointInPath()
isPointInStroke()
lineTo()
measureText()
moveTo()
putImageData()
quadraticCurveTo()
rect()
removeHitRegion()
resetTransform()
restore()
rotate()
save()
scale()
scrollPathIntoView()
setLineDash()
setTransform()
stroke()
strokeRect()
strokeText()
transform()
translate()