我正在制作一个快速创建 CSS 阴影的实用程序,并意识到这只能在 IE 中使用 IE filters 完成。 .然而,Shadow Filter使用方向而不是 (x, y) 坐标:
filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#000000');"
如何从(x,y)坐标计算方向?
编辑:使用来自 this link 的回复和更多详细信息:我修改了我的代码如下:
function(x, y){
var d = Math.atan2(x, y) * (180 / Math.PI);
if(d < 0){ d = 180 - d; }
return d;
}
如果您传入分别用作 X、Y 的 Horizontal Offset 和 Vertical Offset,您将获得从 0 到 359 的度数。
最佳答案
方向以度为单位。
所以
x = cos(direction)
y = sin(direction)
如果您的计算器以弧度计算(任何正常的计算器都应该如此),您可以将度数转换为弧度
radians = degrees / 180 * pi
其中 pi = 3.14159... 或 Math.PI
要走另一条路,请使用“atan”
radians = Math.atan(y / x)
在 Javascript 中,您有一个
Math.atan2
以 y 和 x 作为参数的函数。radians = Math.atan2(y, x)
弧度到度是:
degrees = radians / pi * 180
所以结果是:
direction = Math.atan2(y, x) / Math.PI * 180
关于Javascript:从 X、Y 坐标获取方向的公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3967238/