How to convert x,y coordinates to an angle?

前端 未结 6 839
暗喜
暗喜 2020-12-13 06:49

Microsoft provide an excellent SVG gradient maker so IE9 can also have \"CSS3\" gradients (click Custom).

I currently utilise their logic for my Fireworks and Dreamw

6条回答
  •  甜味超标
    2020-12-13 07:16

    If you get deltaX and deltaY from your coordinates then Math.atan2 returns the arctangent of the quotient of its arguments. The return value is in radians.

    var deltaX = x2 - x1;
    var deltaY = y2 - y1;
    var rad = Math.atan2(deltaY, deltaX); // In radians
    

    Then you can convert it to degrees as easy as:

    var deg = rad * (180 / Math.PI)
    

    enter image description here

    Edit

    There was some bugs in my initial answer. I believe in the updated answer all bugs are addressed. Please comment here if you think there is a problem here.

提交回复
热议问题