如何旋转元素以查看鼠标位置



我正在尝试旋转一个矩形以始终指向鼠标位置。

我试过了:

document.getElementById('viewport').onmousemove = function(e){
     var scratch = Physics.scratchpad();
     mousePos = scratch.vector().set(e.x, e.y);
    var newAngle = box.state.pos.angle(mousePos);
    box.state.angular.pos = newAngle;
    scratch.done();
};

谢谢。

可以试试这样做:

document.getElementById('viewport').onmousemove = function(e){
    var scratch = Physics.scratchpad();
    // assuming your viewport is the whole screen
    mousePos = scratch.vector().set(e.pageX, e.pageY); 
    mousePos.vsub( box.state.pos ); // get vector pointing towards mouse pos
    var newAngle = mousePos.angle(); // get angle with respect to x axis
    box.state.angular.pos = newAngle;
    scratch.done();
};

相关内容

  • 没有找到相关文章

最新更新