Context 的 isPointInPath() 可用于判断某个点是否在路径上。如果指定的点位于当前路径中,则返回 True。语法为:context.isPointInPath(x,y);
参数描述
x测试点的 x 坐标y测试点的 y 坐标
context.strokeStyle = 'green';
context.lineWidth = 5;
context.moveTo(100, 100);
context.lineTo(150, 100);
context.lineTo(150, 150);
context.lineTo(100, 100);
context.stroke();
let isPointInPath1 = context.isPointInPath(100, 100);
let isPointInPath2 = context.isPointInPath(110, 100);
console.log("isPointInPath1 -> " + isPointInPath1);
console.log("isPointInPath2 -> " + isPointInPath2);
context.closePath();
运行结果: