다른 요소 주위로 요소를 회전하려고합니다. 나는이 코드를 조합했다. 코드가 잘 작동한다고해도, 요소의 회전에 약간 문제가있다. 은 매우입니다. 파란색 점은 아래쪽에있을 때 궤도를 떠납니다.특정 지점을 중심으로 회전
코드를 사용했지만 해결할 수 없었습니다. 당신은 당신은 점의 중심을 계산하지만 좌상의 점을 사용하여 위치하는 Felix Eve's answer.
function markCircleCenter(circ, orig) {
if (typeof circ == "string") {
var elm = document.getElementById(circ);
} else { var circle = circ; }
if (typeof orig == "string") {
var mark = document.getElementById(orig);
} else { var elm = orig; }
var width = parseInt(window.getComputedStyle(elm).width, 10);
var height = parseInt(window.getComputedStyle(elm).height, 10);
var top = parseInt(window.getComputedStyle(elm).top, 10);
var left = parseInt(window.getComputedStyle(elm).left, 10);
var markWidth = parseInt(window.getComputedStyle(mark).width, 10);
var markHeight = parseInt(window.getComputedStyle(mark).height, 10);
var circle = {
circle: elm,
x: width/2 + left,
y: height/2 + top,
};
mark.style.position = "absolute";
mark.style.top = circle.y - markWidth/2 + "px";
mark.style.left = circle.x - markHeight/2 + "px";
document.body.insertBefore(mark, elm.nextSibling);
return circle;
}
function placeObjIntoOrbit(obj) {
if (typeof obj == "string") {
var obj = document.getElementById(obj);
} else { var obj = obj; }
var objPos = {
obj: obj,
x: origin.x,
y: parseInt(window.getComputedStyle(origin.circle).top, 10) - parseInt(window.getComputedStyle(obj).width, 10)/2,
};
obj.style.position = "absolute";
obj.style.top = objPos.y + "px";
obj.style.left = objPos.x + "px";
return objPos;
}
function rotateObj(pointX, pointY, originX, originY, angle) {
var angle = angle * Math.PI/180.0;
return {
x: Math.cos(angle) * (pointX-originX) - Math.sin(angle) * (pointY-originY) + originX,
y: Math.sin(angle) * (pointX-originX) + Math.cos(angle) * (pointY-originY) + originY,
};
}
var angle = 0;
var origin = markCircleCenter("circle", "origin");
var point = placeObjIntoOrbit("point");
function spin() {
if (angle >= 360) { angle = 0; }
angle += 2;
var newCoor = rotateObj(point.x, point.y, origin.x, origin.y, angle);
point.obj.style.left = newCoor.x + "px";
point.obj.style.top = newCoor.y + "px";
}
setInterval("spin('point')", 1000/30);
안녕하세요. 코드에서 오류를 발견하도록 사람들에게 요청하는 것은 특히 생산적이지 않습니다. 디버거를 사용하거나 인쇄 문을 추가하여 프로그램의 진행 상황을 추적하고 발생할 것으로 예상되는 것과 비교하여 문제를 격리해야합니다. 이 둘이 갈라지면 문제를 발견했습니다. (그리고 필요하다면 [최소 테스트 케이스] (http://stackoverflow.com/help/mcve)를 구성해야합니다.) –