我正在编写一些 Canvas 代码来绘制一个圆,但它从未被绘制过。最后我发现这是因为我输入了 Math.Pi
而不是 Math.PI
。
在 Chrome 开发工具中,我可以看到 Math.Pi
是 undefined
,但它从未抛出任何异常。这是 Chrome 中的错误还是对 undefined
进行计算实际上合法?
这是我的代码:
ctx.save();
ctx.lineWidth = 2;
ctx.fillStyle = '#FF0000';
ctx.beginPath();
ctx.arc(can.x, can.y, 15, 0, Math.Pi * 2, true);
ctx.fill();
ctx.restore();
ctx
引用canvas.getContext("2d");
。
最佳答案
undefined * 2
的计算结果为 NaN
,并且 canvas 2D context specification说:
Except where otherwise specified, for the 2D context interface, any method call with a numeric argument whose value is infinite or a NaN value must be ignored.
因此,一个参数为 NaN
的 ctx.arc
调用意味着该调用会被静默忽略(即什么都不绘制),而不是抛出错误。
关于javascript - 用 Math.Pi 计算不会报错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8552736/