简体   繁体   English

使用画布确定形状的创建

[英]Determine shape creation with canvas

I want to test with JavaScript that a canvas shape was created. 我想用JavaScript测试是否创建了画布形状。

For example: I'm creating a circle: 例如:我正在创建一个圆:

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();

In this moment circle suppose to be created. 在这一刻,想创建一个圆圈。 I'm thinking to test it like this: 我正在考虑像这样测试它:

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

But I don't find this very elegant. 但是我觉得这不是很优雅。 Anyone has a better idea? 有人有更好的主意吗?

x 2 + y 2 < r 2 x 2 + y 2 <r 2

r - radius r-半径

This is the set of points for a circle. 这是一个圆的一组点。

All the values of x and y that meets the formula inequality are inside the circle. 满足公式不等式的所有xy值都在圆内。

So check for a point with values (-r< x <r,-r< y <r) to be true. 因此,请检查一个值为(-r< x <r,-r< y <r)是否为真。

And check for points (r+1,r+1) (r+1,-r-1) (-r-1,r+1) (-r-1,-r-1) to be false 并检查点(r+1,r+1) (r+1,-r-1) (-r-1,r+1) (-r-1,-r-1)为假

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM