Context.isPointInPath用法

时间:2011-09-26 04:57:17

标签: html5 canvas

我用谷歌搜索了这个,但没有找到任何在HTML 5中使用Context.isPointInPath的例子。

我知道如果该点在当前路径上,它应该返回true,但你究竟如何使用它?您应该在context.beginPath()cotext.closePath()之间使用它(或填写*)

我试过了:

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.fillRect(0,0, 100,100);
ctx.isPointInPath(50,50); // Returned false
ctx.isPointInPath(50,0);  // Tried it on the actual path, not the filled region, returned false
ctx.isPointInPath(50,8);  // My canvas had the default 8 offset, returned false
ctx.isPointInPath(50,9);  // Canvas had a border of 1px, returned false

我不知道出了什么问题,但是他们都返回了假,我从来没有一个人回来。

最后,我关闭了路径并检查了值,仍然返回false。

1 个答案:

答案 0 :(得分:9)

您对isPointInPath()的所有调用都返回false,因为在处理上下文时实际上并未创建任何路径。 fillRect()函数不会创建路径。它只是用你之前指定的颜色填充画布上的一些像素。

相反,您需要使用其中一个路径修改功能,例如rect()moveTo()。有关isPointInPath()和路径函数的所有详细信息,请参阅此处的画布规范:

isPointInPath():http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-ispointinpath

路径功能:http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#complex-shapes-%28paths%29