2013-03-06 75 views
0

我想用JavaScript测试画布形状是否已创建。使用画布确定形状创建

例如:我创建了一圈:

context.beginPath(); 
context.fillStyle = this.color; 
context.arc(this.x, this.y, this.radius, 0, 2 * Math.PI, false); 
context.fill(); 
context.stroke(); 
context.closePath(); 

在这一瞬间圈假设要创建。 我想测试一下这样的:

context.isPointInPath(this.x+1,this.y+1) == true; 

但我不觉得这是非常优雅。 任何人有更好的主意?

回答

0

X + Y <ř

的R - 半径

这是一个圆的点的集合。

x and y所有值满足公式不等式都在圆圈内。

因此检查值为(-r< x <r,-r< y <r)的点是否为真。

,检查点(r+1,r+1)(r+1,-r-1)(-r-1,r+1)(-r-1,-r-1)是假

+0

你的意思是使用isPointInPath检查? – mrbadnews 2013-03-06 12:48:42

+0

是的,'isPointInPath'。也许'(r + 1,r + 1)'而不是'(r + 0.1,r + 0.1)'会更好,也许整数对像素更好 – kidwon 2013-03-06 13:24:55