- HTML Canvas 教程
- HTML Canvas - 首页
- HTML Canvas - 简介
- 环境设置
- HTML Canvas - 第一个应用
- HTML Canvas - 绘制 2D 形状
- HTML Canvas - 路径元素
- 使用路径元素绘制 2D 形状
- HTML Canvas - 颜色
- HTML Canvas - 添加样式
- HTML Canvas - 添加文本
- HTML Canvas - 添加图像
- HTML Canvas - 画布时钟
- HTML Canvas - 变换
- 合成和剪裁
- HTML Canvas - 基本动画
- 高级动画
- HTML Canvas API 函数
- HTML Canvas - 元素
- HTML Canvas - 矩形
- HTML Canvas - 线
- HTML Canvas - 路径
- HTML Canvas - 文本
- HTML Canvas - 颜色和样式
- HTML Canvas - 图像
- HTML Canvas - 阴影和变换
- HTML Canvas 有用资源
- HTML Canvas - 快速指南
- HTML Canvas - 有用资源
- HTML Canvas - 讨论
HTML Canvas - isPointInPath() 方法
HTML Canvas 的 isPointInPath() 方法是 Canvas 2D API 的一部分,它检查指定点是否包含在当前路径中,并使用布尔值输出进行报告。
语法
以下是 HTML Canvas isPointInPath() 方法的语法:
CanvasRenderingContext2D.isPointInPath(x, y, path, rule);
参数
以下是此方法的参数列表:
| 序号 | 参数及描述 |
|---|---|
| 1 | x
要检查的点的 x 坐标。 |
| 2 | y
要检查的点的 y 坐标。 |
| 3 | path
要参考以检查提供的点是否包含在其中的路径。如果未给出路径,则使用当前路径。 |
| 4 | rule
用于确定点是否在路径中的算法。此参数采用两个输入值。
|
返回值
当 CanvasRenderingContext2D 接口上下文对象访问 isPointInPath() 方法时,它会返回一个布尔值,指示点是否在路径中。
示例 1
以下示例在 Canvas 元素中绘制一个圆圈,并使用 HTML Canvas isPointInPath() 方法检查提供的点是否在路径中。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Reference API</title>
<style>
body {
margin: 10px;
padding: 10px;
}
</style>
</head>
<body onload="Context();">
<canvas id="canvas" width="200" height="200" style="border: 1px solid black;"></canvas>
<p>Check the given point is in the path : <code id="check">false</code>
</p>
<script>
function Context() {
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var check = document.getElementById("check");
context.beginPath();
context.arc(100, 100, 75, 1 * Math.PI, 5 * Math.PI);
context.fill();
context.closePath();
check.innerText = context.isPointInPath(150, 150);
}
</script>
</body>
</html>
输出
以下代码在网页上返回输出如下:
示例 2
以下示例在 Canvas 元素中绘制一个描边的三角形,并检查提供的点是否在路径中。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Reference API</title>
<style>
body {
margin: 10px;
padding: 10px;
}
</style>
</head>
<body onload="Context();">
<canvas id="canvas" width="200" height="200" style="border: 1px solid black;"></canvas>
<p>Check the given point is in the shape : <code id="check">false</code>
</p>
<script>
function Context() {
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var check = document.getElementById("check");
context.strokeStyle = 'blue';
context.beginPath();
context.moveTo(100, 50);
context.lineTo(25, 150);
context.lineTo(175, 150);
context.lineTo(100, 50);
context.stroke();
context.closePath();
check.innerText = context.isPointInPath(15, 15);
}
</script>
</body>
</html>
输出
以下代码在网页上返回输出如下:
html_canvas_paths.htm
广告