isPointInPath()是 Canvas 2D API 用于判断在当前路径中是否包含检测点的方法。
如果点 20,50 位于当前路径中,则绘制一个矩形:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML canvas isPointInPath()方法使用-森客兔子(thinktus.com)</title>
</head>
<body>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
您的浏览器不支持 HTML5 canvas 标签。
</canvas>
<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.rect(20,20,150,100);
if (ctx.isPointInPath(20,50))
{
ctx.stroke();
};
</script>
</body>
</html>
测试看看 ‹/›IEFirefoxOperaChromeSafari
Internet Explorer 9、Firefox、Opera、Chrome 和 Safari 支持 isPointInPath() 方法。
注意:Internet Explorer 8 及之前的版本不支持 <canvas> 元素。
如果指定点在当前路径中,则isPointInPath()方法返回true,否则返回false
JavaScript 语法: | context.isPointInPath(x,y); |
---|
参数 | 描述 |
---|---|
x | 要测试的 x 坐标。 |
y | 要测试的 y 坐标。 |