This repository has been archived on 2024-06-15. You can view files and clone it, but cannot push or open issues or pull requests.
snippets/point-on-sphere.js

17 lines
742 B
JavaScript
Raw Normal View History

2022-11-26 19:02:05 +00:00
// Get angle on circle from mouse position
function getAngleOnCircleFromOffsetMouse(evt) {
const x = (evt.pageX - element.offset().left);
const y = (evt.pageY - element.offset().top);
// https://math.stackexchange.com/a/127615
const center = { x: width / 2, y: width / 2 };
const xCenter = (x - center.x);
const yCenter = (y - center.y);
const circlePoint = {
x: center.x + radius * (xCenter / Math.sqrt(xCenter * xCenter + yCenter * yCenter)),
y: center.y + radius * (yCenter / Math.sqrt(xCenter * xCenter + yCenter * yCenter)),
};
const angle = Math.atan2(circlePoint.y - center.y, circlePoint.x - center.x) + Math.PI / 2;
return angle < 0 ? Math.PI * 2 - Math.abs(angle) : angle;
}