返回到:Canvas API:CanvasRenderingContext2D
本节课,我们学习CanvasRenderingContext2D.isPointInPath()方法。
CanvasRenderingContext2D.isPointInPath()
是 Canvas 2D API 用于判断在当前路径中是否包含检测点的方法。
语法
boolean ctx.isPointInPath(x, y);
boolean ctx.isPointInPath(x, y, fillRule);
boolean ctx.isPointInPath(path, x, y);
boolean ctx.isPointInPath(path, x, y, fillRule);
参数
x
检测点的 X 坐标
y
检测点的 Y 坐标
fillRule
用来决定点在路径内还是在路径外的算法。 允许的值:
nonzero
非零环绕规则,默认的规则。
evenodd
奇偶环绕原则。
path
Path2D
应用的路径。
返回值
Boolean
一个 Boolean 值,当检测点包含在当前或指定的路径内,返回 true;否则返回 false。
示例
使用 isPointInPath
方法
这是一段简单的代码片段,使用 isPointInPath
方法检查某点是否在当前的路径内。
HTML
<canvas id="canvas"></canvas>
JavaScript
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.rect(10, 10, 100, 100);
ctx.stroke();
console.log(ctx.isPointInPath(10, 10)); // true
修改下面的代码,在线查看 canvas 的变化并在你的 控制台 中观察日志信息:
HTML
x
91
91
1
2
<html>
3
<head>
4
<meta charset="utf-8">
5
<meta name="robots" content="noindex, nofollow">
6
<style>
7
body {
8
padding: 0;
9
margin: 0;
10
}
11
12
svg:not(:root) {
13
display: block;
14
}
15
16
.playable-code {
17
background-color: #f4f7f8;
18
border: none;
19
border-left: 6px solid #558abb;
20
border-width: medium medium medium 6px;
21
color: #4d4e53;
22
height: 100px;
23
width: 90%;
24
padding: 10px 10px 0;
25
}
26
27
.playable-canvas {
28
border: 1px solid #4d4e53;
29
border-radius: 2px;
30
}
31
32
.playable-buttons {
33
text-align: right;
34
width: 90%;
35
padding: 5px 10px 5px 26px;
36
}
37
</style>
38
39
<title>CanvasRenderingContext2D.isPointInPath() | Web176教程www.web176.com</title>
40
41
</head>
42
<body>
43
44
<canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas>
45
<div class="playable-buttons">
46
<input id="edit" type="button" value="Edit" />
47
<input id="reset" type="button" value="Reset" />
48
</div>
49
<textarea id="code" class="playable-code">
50
ctx.rect(10, 10, 100, 100);
51
ctx.stroke();
52
console.log(ctx.isPointInPath(10, 10)); // true</textarea>
53
54
55
56
<script>
57
var canvas = document.getElementById("canvas");
58
var ctx = canvas.getContext("2d");
59
60
ctx.rect(10, 10, 100, 100);
61
ctx.stroke();
62
console.log(ctx.isPointInPath(10, 10)); // true
63
64
var canvas = document.getElementById("canvas");
65
var ctx = canvas.getContext("2d");
66
var textarea = document.getElementById("code");
67
var reset = document.getElementById("reset");
68
var edit = document.getElementById("edit");
69
var code = textarea.value;
70
71
function drawCanvas() {
72
ctx.clearRect(0, 0, canvas.width, canvas.height);
73
eval(textarea.value);
74
}
75
76
reset.addEventListener("click", function() {
77
textarea.value = code;
78
drawCanvas();
79
});
80
81
edit.addEventListener("click", function() {
82
textarea.focus();
83
})
84
85
textarea.addEventListener("input", drawCanvas);
86
window.addEventListener("load", drawCanvas);
87
88
</script>
89
90
</body>
91
</html>
返回到:Canvas API:CanvasRenderingContext2D
阅读剩余 93%
作者:terry,如若转载,请注明出处:https://www.web176.com/canvas_api/8059.html