- 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 - 基本动画
- 高级动画
- 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 - isPointInStroke() 方法
HTML Canvas 的isPointInStroke() 方法是 Canvas 2D API 的一部分,用于检查指定的点是否在描边的路径内,并使用布尔值报告结果。
语法
以下是 HTML Canvas isPointInStroke() 方法的语法:
CanvasRenderingContext2D.isPointInStroke(x, y, path);
参数
以下是此方法的参数列表:
| 序号 | 参数及描述 |
|---|---|
| 1 | x
要检查的点的 x 坐标。 |
| 2 | y
要检查的点的 y 坐标。 |
| 3 | path
要参考的路径,用于检查提供的点是否在其内部。如果没有给出路径,则使用当前路径。 |
返回值
当CanvasRenderingContext2D 接口上下文对象访问isPointInStroke() 方法时,它将返回一个布尔值,指示该点是否在描边的路径内。
示例 1
下面的示例在 Canvas 元素内绘制一个三角形,并使用 HTML Canvas isPointInStroke() 方法检查给定点是否在其内部。
<!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 shape is stroked or not at the given point : <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.moveTo(100, 50);
context.lineTo(50, 100);
context.lineTo(150, 100);
context.lineTo(100, 50);
context.fillStyle = 'brown';
context.fill();
context.closePath();
check.innerText = context.isPointInStroke(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="100" style="border: 1px solid black;"></canvas>
<p>Check the given shape is stroked or not at the given point : <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.font = '55px Verdana';
context.fillStyle = 'green';
context.fillText('Hello', 10, 50);
check.innerText = context.isPointInStroke(100, 30);
}
</script>
</body>
</html>
输出
以上代码在网页上返回的输出为:
示例 3
下面的示例在 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="150" style="border: 1px solid black;"></canvas>
<p>Check the given shape is stroked or not at the given point : <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 = 'grey';
context.strokeRect(20, 20, 150, 100);
check.innerText = context.isPointInStroke(100, 100);
}
</script>
</body>
</html>
输出
以上代码在网页上返回的输出为:
html_canvas_paths.htm
广告