html - Context.isPointInPath 用法

标签 html canvas

我用谷歌搜索了这个,但没有找到任何在 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

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

关于html - Context.isPointInPath 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7550788/

相关文章:

html - 从 HTML <li> 表单发送值

javascript - 在 HTML5 中呈现互连元素网络的最佳方式?

javascript - Fabric.js - 色调过滤器

javascript - 更改表单中的变量 onclick

c# - 矩形引用 Canvas 宽度超过 Canvas

canvas - 使用 Fabric JS 模拟位图字体的字距调整

javascript - 为什么 Canvas 在圆(弧)之间画线?

javascript - jquery上/下滑动没有动画

css - 放置在固定容器内时如何将固定 div 拉伸(stretch)到屏幕?

php - 如何存储多维表单数据?