isPointInPath()是 Canvas 2D API 用于判斷在當(dāng)前路徑中是否包含檢測(cè)點(diǎn)的方法。
如果點(diǎn) 20,50 位于當(dāng)前路徑中,則繪制一個(gè)矩形:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML canvas isPointInPath()方法使用-菜鳥教程(cainiaoplus.com)</title>
</head>
<body>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
您的瀏覽器不支持 HTML5 canvas 標(biāo)簽。
</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>測(cè)試看看 ?/?IEFirefoxOperaChromeSafari
Internet Explorer 9、Firefox、Opera、Chrome 和 Safari 支持 isPointInPath() 方法。
注意:Internet Explorer 8 及之前的版本不支持 <canvas> 元素。
如果指定點(diǎn)在當(dāng)前路徑中,則isPointInPath()方法返回true,否則返回false
| JavaScript 語法: | context.isPointInPath(x,y); |
|---|
| 參數(shù) | 描述 |
|---|---|
| x | 要測(cè)試的 x 坐標(biāo)。 |
| y | 要測(cè)試的 y 坐標(biāo)。 |