我用谷歌搜索了这个,但没有找到任何在 HTML 5 中使用 Context.isPointInPath
的例子。
我知道如果该点在当前路径上,它应该返回 true,但您究竟如何使用它?您是否应该在 context.beginPath()
和 cotext.closePath()
之间使用它(或者 fill* 就此而言)
我试过这个:
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,而我从来没有返回过 true。
最后,我关闭了路径并检查了值,仍然返回 false。
最佳答案
您对 isPointInPath()
的所有调用都返回 false,因为您在使用上下文时实际上并未创建任何路径。 fillRect()
函数不创建路径。它只是用您之前指定的任何颜色填充 Canvas 上的一些像素。
相反,您需要使用一种路径修改函数,例如 rect()
或 moveTo()
。有关 isPointInPath()
和路径函数的所有详细信息,请参阅此处的 Canvas 规范:
isPointInPath(): http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-ispointinpath
关于html - Context.isPointInPath 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7550788/