<canvas>是HTML5中新增加的一个元素,我们可以使用脚本(通常使用JavaScript)在上面绘制图形,就像个画布一样。我们可以用它来绘制图表、制作一些动画。默认大小为300px × 150px。

在<canvas>中绘制图形的方法中,isPointInPath()方法用于检测指定的点是否在绘制图形的路径中,存在返回ture,不存在返回false。

注:在代码部分,红色加粗部分是重点要注意的内容哦! 

在矩形中

在画布上绘制一个空心矩形,然后指定一个点,如果这个点在矩形的路径中,矩形的颜色为蓝色,否则为黑色。为了能清楚的看到那个点在哪里,我们后面再画上两条灰色的线,交叉的位置就是我们指定的点:

 <body>
<canvas id="drawEle">
您的浏览器不支持该标签
</canvas>
<script>
var c = document.getElementById("drawEle");
ctx = c.getContext("2d"); //矩形
ctx.beginPath();
ctx.rect(10,20,280,20);     //绘制矩形区域
if(ctx.isPointInPath(50,25)) { //判断(50,25)是否在矩形路径中
ctx.strokeStyle = "#0000FF"; //在则矩形是蓝色
}
else {
ctx.strokeStyle = "#000000"; //不在则矩形是黑色
}
ctx.stroke(); //绘制定位用的直线
ctx.beginPath();
ctx.strokeStyle = "#CCCCCC";
ctx.moveTo(50,0);
ctx.lineTo(50,150);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0,25);
ctx.lineTo(300,25);
ctx.stroke();
</script>
</body>

运行效果如下:

可以看到,我们定位的点再矩形区域内部,矩形的颜色变成了蓝色,也就是说这个点的位置在矩形的路径中。

在弧/曲线区域中

那我们再arc()创建的弧/曲线区域中定位试试看。之后也是使用两条灰色线交叉定位我们判断的点:

 <body>
<canvas id="drawEle">
您的浏览器不支持该标签
</canvas>
<script>
var c = document.getElementById("drawEle");
ctx = c.getContext("2d"); //弧/曲线
ctx.beginPath();
ctx.arc(150,75,50,0,1.5 * Math.PI); //绘制圆形区域
if(ctx.isPointInPath(170,55)) { //判断(170,55)是否在矩形路径中
ctx.strokeStyle = "#0000FF";    //在则曲线是蓝色
}
else {
ctx.strokeStyle = "#000000"; //不在则曲线是黑色
}
ctx.stroke(); //绘制定位用的直线
ctx.beginPath();
ctx.strokeStyle = "#CCCCCC";
ctx.moveTo(170,0);
ctx.lineTo(170,150);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0,55);
ctx.lineTo(300,55);
ctx.stroke();
</script>
</body>

运行效果如下:

哦,圆还是蓝色的,说明定位是在路径中的。。。不对,这样子看上去好像没有闭合啊,那我们给它填充一下颜色(换成fill()):

 <body>
<canvas id="drawEle">
您的浏览器不支持该标签
</canvas>
<script>
var c = document.getElementById("drawEle");
ctx = c.getContext("2d"); //弧/曲线
ctx.beginPath();
ctx.arc(150,75,50,0,1.5 * Math.PI); //绘制圆形区域
if(ctx.isPointInPath(170,55)) { //判断(170,55)是否在矩形路径中
ctx.fillStyle = "#0000FF"; //在则曲线是蓝色
}
else {
ctx.fillStyle = "#000000"; //不在则曲线是黑色
}
ctx.fill(); //绘制定位用的直线
ctx.beginPath();
ctx.strokeStyle = "#CCCCCC";
ctx.moveTo(170,0);
ctx.lineTo(170,150);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0,55);
ctx.lineTo(300,55);
ctx.stroke();
</script>
</body>

嗯,是没有超过区域,那我们让超过区域看看

 <body>
<canvas id="drawEle">
您的浏览器不支持该标签
</canvas>
<script>
var c = document.getElementById("drawEle");
ctx = c.getContext("2d"); //弧/曲线
ctx.beginPath();
ctx.arc(150,75,50,0,1.5 * Math.PI); //绘制圆形区域
if(ctx.isPointInPath(180,45)) { //判断(180,45)是否在矩形路径中
ctx.fillStyle = "#0000FF"; //在则曲线是蓝色
}
else {
ctx.fillStyle = "#000000"; //不在则曲线是黑色
}
ctx.fill(); //绘制定位用的直线
ctx.beginPath();
ctx.strokeStyle = "#CCCCCC";
ctx.moveTo(180,0);
ctx.lineTo(180,150);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0,45);
ctx.lineTo(300,45);
ctx.stroke();
</script>
</body>

变黑了,说明这个点不再路径中。

在直线中

接下来就到最简单的线条了。其实写这个笔记就是因为这个线条来着┑( ̄ ▽  ̄)┍

使用moveTo()和lineTo()结合,创建一根直线,定位点在路径中直线为蓝色,否则为黑色。使用一条灰色线交叉定位我们判断的点:

 <body>
<canvas id="drawEle">
您的浏览器不支持该标签
</canvas>
<script>
var c = document.getElementById("drawEle");
ctx = c.getContext("2d"); //直线
ctx.beginPath();
ctx.moveTo(40,40);
ctx.lineTo(260,40);
if(ctx.isPointInPath(150,40)) { //判断(150,40)是否在矩形路径中
ctx.strokeStyle = "#0000FF"; //在则直线是蓝色
}
else {
ctx.strokeStyle = "#000000"; //不在则直线是黑色
}
ctx.stroke(); //绘制定位用的直线
ctx.beginPath();
ctx.strokeStyle = "#CCCCCC";
ctx.moveTo(150,0);
ctx.lineTo(150,150);
ctx.stroke();
</script>
</body>

欸,怎么是黑色的啊,我不是定位点的y轴和线条两个点的y轴都重合了么?

就是这个情况,定位点如果定位在使用moveTo()和lineTo()绘制的直线中间自动生成的线上,是会返回false的!

那把定位点完全重合moveTo()的点试试看:

 <body>
<canvas id="drawEle">
您的浏览器不支持该标签
</canvas>
<script>
var c = document.getElementById("drawEle");
ctx = c.getContext("2d"); //直线
ctx.beginPath();
ctx.moveTo(40,40);
ctx.lineTo(260,40);
if(ctx.isPointInPath(40,40)) { //判断(40,40)是否在直线路径中
ctx.strokeStyle = "#0000FF"; //在则直线是蓝色
}
else {
ctx.strokeStyle = "#000000"; //不在则直线是黑色
}
ctx.stroke(); //绘制定位用的直线
ctx.beginPath();
ctx.strokeStyle = "#CCCCCC";
ctx.moveTo(40,0);
ctx.lineTo(40,150);
ctx.stroke();
</script>
</body>

蓝了,蓝了,它蓝了!

那接下来我们用moveTo()和lineTo()做一个闭合的图形康康,定位就设在一条边中间,用两条灰色的线条交叉标记我们定位的点:

 <body>
<canvas id="drawEle">
您的浏览器不支持该标签
</canvas>
<script>
var c = document.getElementById("drawEle");
ctx = c.getContext("2d"); //直线
ctx.beginPath();
ctx.moveTo(40,40);
ctx.lineTo(260,40);
ctx.lineTo(260,130);
ctx.closePath();
if(ctx.isPointInPath(150,40)) { //判断(150,40)是否在路径中
ctx.fillStyle = "#0000FF"; //在则直线是蓝色
}
else {
ctx.fillStyle = "#000000"; //不在则直线是黑色
}
ctx.fill(); //绘制定位用的直线
ctx.beginPath();
ctx.strokeStyle = "#CCCCCC";
ctx.moveTo(150,0);
ctx.lineTo(150,150);
ctx.stroke(); ctx.beginPath();
ctx.moveTo(0,40);
ctx.lineTo(300,40);
ctx.stroke();
</script>
</body>

这个区域这个时候是蓝的了。也就是说不是闭合区域的时候,moveTo()和lineTo()中的线条是不算在区域内的,得闭合后才算。(对于这个观点,其实我是觉得有点怪怪的,感觉这个观点应该是接近正确答案但它不是正确答案)

在加粗的直线末端中

假如线条宽度有20px,上面我们知道只有和路径点重合了才算,那加粗的线条还是和路径点重合了算还是路径点y轴(假设是横线,那么就当它是长方形吧,竖这的边上任意一点)也算,这也是刚刚突然想到的,试试看:

 <body>
<canvas id="drawEle">
您的浏览器不支持该标签
</canvas>
<script>
var c = document.getElementById("drawEle");
ctx = c.getContext("2d"); //直线
ctx.beginPath();
ctx.moveTo(40,40);
ctx.lineTo(260,40);
ctx.lineWidth = 40;
if(ctx.isPointInPath(40,35)) { //判断(40,35)是否在矩形路径中
ctx.strokeStyle = "#0000FF"; //在则直线是蓝色
}
else {
ctx.strokeStyle = "#000000"; //不在则直线是黑色
}
ctx.stroke(); //绘制定位用的直线
ctx.beginPath();
ctx.lineWidth = 1;
ctx.strokeStyle = "#CCCCCC";
ctx.moveTo(40,0);
ctx.lineTo(40,150);
ctx.stroke(); ctx.beginPath();
ctx.moveTo(0,35);
ctx.lineTo(300,35);
ctx.stroke();
</script>
</body>

看起来还是得和路径点重合。

那加粗后闭合呢?

 <body>
<canvas id="drawEle">
您的浏览器不支持该标签
</canvas>
<script>
var c = document.getElementById("drawEle");
ctx = c.getContext("2d"); //直线
ctx.beginPath();
ctx.moveTo(40,40);
ctx.lineTo(260,40);
ctx.lineTo(260,130);
ctx.closePath();
ctx.lineWidth = 40;
if(ctx.isPointInPath(40,35)) { //判断(40,35)是否在矩形路径中
ctx.f = "#0000FF"; //在则直线是蓝色
}
else {
ctx.strokeStyle = "#000000"; //不在则直线是黑色
}
ctx.stroke(); //绘制定位用的直线
ctx.beginPath();
ctx.lineWidth = 1;
ctx.strokeStyle = "#CCCCCC";
ctx.moveTo(40,0);
ctx.lineTo(40,150);
ctx.stroke(); ctx.beginPath();
ctx.moveTo(0,35);
ctx.lineTo(300,35);
ctx.stroke();
</script>
</body>

没有区别,也就是说加粗的部分并不算在路径内。


参考资料:MDN - Canvas​Rendering​Context2D.isPoint​InPath() : https://developer.mozilla.org/zh-CN/docs/Web/API/CanvasRenderingContext2D/isPointInPath

<canvas>中isPointInPath()方法在不同绘制内容中的效果的更多相关文章

  1. jquery中Live方法不可用,Jquery中Live方法失效

    jquery中Live方法不可用,Jquery中Live方法失效 >>>>>>>>>>>>>>>>> ...

  2. Properties集合中的方法store和Properties集合中的方法load

    Properties集合中的方法store public class Demo01Properties { public static void main(String[] args) throws ...

  3. C#后台程序与HTML页面中JS方法互调(功能类似于Ajax中的DWR)

    此方法适用于 C#中嵌入WebBrowser(浏览器) 通过浏览器中加载的页面与C#的后台代码进行交互. 一.C#程序 1.在C#窗体中添加WebBrowser(浏览器),将页面的URL添加到浏览器中 ...

  4. ①创建项目testpackage ②在pack2.B中添加方法f ③在类A中添加如下三个成员变量:int型的私有变量i float型的变量f double型的公有变量d 在pack1.B的main方法中为对象a的成员变量f和d分别赋值为2和3 在pack2.C的main方法中为对象a的成员变量d赋值为3

    package pack1; public class A { private int i; float f; public double d; public float getF() { retur ...

  5. JS中substring()方法(用于提取字符串中介于两个指定下标之间的字符)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. Java中wait()方法为什么要放在同步块中?(lost wake-up 问题)

    问题起源 事情得从一个多线程编程里面臭名昭著的问题"Lost wake-up problem"说起. 这个问题并不是说只在Java语言中会出现,而是会在所有的多线程环境下出现. 假 ...

  7. 在eclipse中查找一个类中的方法在其他哪个类中被调用了

    选中你所要查看的方法名,ctrl+shift+G就可以查看所有调用过该方法的地方了.在Search视图里面可以查看得到这个样子是可以的,你也可以按Ctrl+H全文检索一下

  8. 关于tx:method和pointcut中的方法,即事务管理中的方法和切入点表达式中的方法具体如何执行

    <tx:advice id="transaction" tranction-manager="transactionManager"> <tx ...

  9. jQuery中的方法

    jQuery中的方法来操作HTML标签中的属性 attr(name)    获取当前对象的指定的属性的值 attr(key,value)  给当前对象设置属性值 attr(properties)  一 ...

随机推荐

  1. apex透视自瞄无后子弹追踪飞天加速辅助

    apex透视自瞄无后子弹追踪飞天加速辅助apex透视自瞄无后子弹追踪飞天加速辅助apex透视自瞄无后子弹追踪飞天加速辅助apex透视自瞄无后子弹追踪飞天加速辅助apex透视自瞄无后子弹追踪飞天加速辅助 ...

  2. Python条件判断 if-else for循环 while循环 break continue

    条件判断 if-else if-else语句是通过if 后面的是否为真,当为True,就执行if代码块后面的,如果为False,同时又有else语句,执行else后面的内容.没有else,什么都不执行 ...

  3. 设计模式二之观察者模式(Subject-Observer)

    观察者模式定义了一系列对象之间的一对多关系,当一个主题对象改变状态,其他所有的依赖者都会收到通知. 好了,你可能会觉得上面的描述略微复杂,较难理解,那么现在我们将用一个简单的例子去讲解这个模式. 我们 ...

  4. python基础 字典练习

    练习1:info = [ {'wangming': { 'money':1111, 'car':['bmo','bsj'], 'info':{ 'phone':1511111, 'age':18} } ...

  5. FBV和CBV装饰器

    FBV装饰器: def cook(request): err_msg="" if request.method == "GET": return render( ...

  6. 【转】Android-Input input&按键布局文件

    https://source.android.com/devices/input 输入 Android 输入子系统名义上是由遍历系统多个层的事件管道组成. 输入管道 在最低层,物理输入设备会生成描述状 ...

  7. vuex数据持久化插件--指定持久化特定的值

    指定需要持久化的state,配置如下 import createPersistedState from "vuex-persistedstate" conststore = new ...

  8. 网易云课堂----java视频教程

    http://study.163.com/course/introduction.htm?courseId=1003203062#/courseDetail?tab=1

  9. python sheet写入数据

    import pandas as pd from pandas import DataFrame import openpyxl from openpyxl import load_workbook ...

  10. oracle导入导出功能

    1.普通版:oracle导入导出功能:导出exp 用户名/密码@SID file=f:\xx.dmp owner=用户名 导入imp 用户名/密码@SID full=y file=f:\xx.dmp ...