为什么要加 270 度才能正确旋转?
Why is it necessary to add 270 degrees to correct rotation?
计算两点之间的角度似乎与 DOM 旋转不一致
我已经编写了代码来计算两点之间的角度。我使用 HTML canvas 在点之间绘制一条线,我还显示了一个 DOM 元素,该元素具有一个箭头字符,我旋转该箭头字符以指示矢量的方向。不明白为什么要加270度来修正箭头的位置
见下面这一行:radians += (3 * Math.PI / 2); // why 270 degrees?
class Vector {
constructor(x=0, y=0) {
this.x = x;
this.y = y;
}
angleFromVectors(anchor, point) {
return Math.atan2(anchor.y - point.y, anchor.x - point.x);
}
draw(v1, v2) {
let canvas = document.getElementById('canvas');
if (canvas.getContext) {
let ctx = canvas.getContext('2d');
ctx.moveTo(v1.x, v1.y);
ctx.lineTo(v2.x, v2.y)
ctx.stroke();
}
}
rotateArrow(degrees) {
let arrow = document.getElementById('arrow');
arrow.style.webkitTransform = 'rotate('+ degrees + 'deg)';
}
}
let v1 = new Vector(180, 180);
let v2 = new Vector(100, 190);
let vector = new Vector();
let radians = vector.angleFromVectors(v1, v2);
radians += (3 * Math.PI / 2); // why 270 degrees?
let degrees = radians * (180 / Math.PI);
console.log(`radians: ${radians}, degrees: ${degrees}`);
vector.draw(v1, v2);
vector.rotateArrow(degrees);
Codepen 在这里:https://codepen.io/cjus/pen/bzKvwO
箭头是字符⬆
。因为它是向上的,如果你想让它向左,你需要旋转它。你也可以旋转它 -90deg
.
顺便说一句,你实际上是在旋转它 262.875deg
。
计算两点之间的角度似乎与 DOM 旋转不一致
我已经编写了代码来计算两点之间的角度。我使用 HTML canvas 在点之间绘制一条线,我还显示了一个 DOM 元素,该元素具有一个箭头字符,我旋转该箭头字符以指示矢量的方向。不明白为什么要加270度来修正箭头的位置
见下面这一行:radians += (3 * Math.PI / 2); // why 270 degrees?
class Vector {
constructor(x=0, y=0) {
this.x = x;
this.y = y;
}
angleFromVectors(anchor, point) {
return Math.atan2(anchor.y - point.y, anchor.x - point.x);
}
draw(v1, v2) {
let canvas = document.getElementById('canvas');
if (canvas.getContext) {
let ctx = canvas.getContext('2d');
ctx.moveTo(v1.x, v1.y);
ctx.lineTo(v2.x, v2.y)
ctx.stroke();
}
}
rotateArrow(degrees) {
let arrow = document.getElementById('arrow');
arrow.style.webkitTransform = 'rotate('+ degrees + 'deg)';
}
}
let v1 = new Vector(180, 180);
let v2 = new Vector(100, 190);
let vector = new Vector();
let radians = vector.angleFromVectors(v1, v2);
radians += (3 * Math.PI / 2); // why 270 degrees?
let degrees = radians * (180 / Math.PI);
console.log(`radians: ${radians}, degrees: ${degrees}`);
vector.draw(v1, v2);
vector.rotateArrow(degrees);
Codepen 在这里:https://codepen.io/cjus/pen/bzKvwO
箭头是字符⬆
。因为它是向上的,如果你想让它向左,你需要旋转它。你也可以旋转它 -90deg
.
顺便说一句,你实际上是在旋转它 262.875deg
。