CanvasRenderingContext2D.isPointInPath()方法

返回到: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

返回到:Canvas API:CanvasRenderingContext2D

阅读剩余 93%

作者:terry,如若转载,请注明出处:https://www.web176.com/canvas_api/8059.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年1月8日 上午11:11
下一篇 2023年1月8日 上午11:23

相关推荐

发表回复

登录后才能评论