KinectJS:每次拖放旋转一个绝对旋转点的图像

时间:2013-07-24 07:08:51

标签: javascript canvas drag-and-drop rotation kineticjs

我想用kinectJS旋转关于绝对旋转点的图像。例如关于

var rotatePoint = {x: 500, y: 500}

通过单击图像并移动鼠标,即通过拖放图像来初始化旋转。它可以旋转大约相同的角度,鼠标正在绘制。

昨天我整天都在解决这个问题,但是找不到解决办法。

有什么想法吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

谢谢!

我最终得到了线索。 在下面的示例代码中,图像imgObj围绕

旋转
imgObj.rotPoint = {x: 500, y: 500}

要解决几个问题: 您不能只设置绝对旋转点,您必须相对于其标准位置(图像的左上边缘)更改偏移。然后你必须将图像移回,因为偏移的变化会移动图像。

对于漫游,我启用了拖动并使用了dragBoundFunc。

设置

return {x: this.getAbsolutePosition().x, y: this.getAbsolutePosition().y};

会向你保证,没有真正的拖延 - 只是轮换。

对于旋转本身,您需要六个值: 拖动开始时有三个值:

  • posMouseStart:鼠标的x-y位置
  • radMouseStart:正x轴与从旋转点到以弧度为单位的posMouseStart的矢量之间的角度
  • radImageStart:图像的当前旋转(可能已旋转?)

我通过绑定onmousedown来获取这些值,如果有拖放则只使用这些值。

拖放时会一直变化的三个值:

  • posMouseNow:当时鼠标的x-y位置
  • radMouseNow:正x轴和从旋转点到posMouseStart的矢量之间的角度,以弧度为单位。
  • radImageNow:当前旋转的图像(可能已经旋转了吗?)就在那一刻

我在dragBoundFunc中获取这些值。

在使用Math.atan2()获取角度时,你必须担心,你不是在xy坐标系中,而是在x - ( - y) - 坐标系中 - 所以使用:Math.atan2 (-y,X)。

通过从radMouseNow中减去radMouseStart,您将获得旋转所需的角度,以便将图像从起始位置移动到当前位置。但是,当我们围绕这个角度旋转时,图像会像疯了一样旋转。 为什么会那样? - " start"之间有几个毫秒的时间。和"现在"图像已经旋转的位置。所以:当你现在"现在"旋转,你不是在radMouseStart开始但是在radImageNow - radImageStart + radMouseStart。

\ o /所有问题都解决了\ o /

代码:

var imgObj = new Kinetic.Image
                 ({
                        image: YourImg,
                        x: YourSize.x,
                        y: YourSize.y,
                        draggable: true
                 });
imgObj.rotPoint = {x: 500, y: 500};
imgObj.setOffset
(
    imgObj.rotPoint.x - imgObj.getAbsolutePosition().x,
    imgObj.rotPoint.y - imgObj.getAbsolutePosition().y
);
imgObj.move(imgObj.getOffsetX(),imgObj.getOffsetY());
var o = {x: imgObj.rotPoint.x, y: imgObj.rotPoint.y}; // shortcut
imgObj.on('mousedown', function()
                         {
                             posMouseStart = stage.getMousePosition();
                             radMouseStart = Math.atan2(-(posMouseStart.y - o.y), posMouseStart.x - o.x);
                             radImageStart = this.getRotation();
                         });
imgObj.setDragBoundFunc(function(pos)
                        {
                            var posMouseNow = stage.getMousePosition();
                            var radMouseNow = Math.atan2(-(posMouseNow.y - o.y), posMouseNow.x - o.x);
                            var radImageNow = this.getRotation();

                            var radMouseDiff = -(radMouseNow - radMouseStart);

                            this.rotate(radImageStart + radMouseDiff - radImageNow);

                            return {x: this.getAbsolutePosition().x, y: this.getAbsolutePosition().y};
                        });

答案 1 :(得分:0)

您可以使用某些三角法

在固定的旋转点周围移动图像

使用Math.atan2计算鼠标位置相对于旋转点的角度:

    var dx=mouseX-rotationPointX;
    var dy=mouseY-rotationPointY;
    var radianAngle=Math.atan2(dy,dx);

使用Math.cos和Math.sin:

在rotationPoint周围移动图像
    var x=rotationPointX+radius*Math.cos(radianAngle)-imgWidth/2;
    var y=rotationPointY+radius*Math.sin(radianAngle)-imgHeight/2;
    image.setPosition(x,y);

以下是工作示例代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Prototype</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.5.4.min.js"></script>

<style>
body{ padding:15px; }
#container{
  border:solid 1px #ccc;
  margin-top: 10px;
  width:300px;
  height:300px;
}
</style>        
<script>
$(function(){

    var stage = new Kinetic.Stage({
        container: 'container',
        width: 300,
        height: 300
    });
    var layer = new Kinetic.Layer();
    stage.add(layer);

    var rx=150;
    var ry=150;
    var radius=75;

    var image;;
    var imgWidth=50;
    var imgHeight=50;

    var rotationPoint=new Kinetic.Circle({
        x:rx,
        y:ry,
        radius:10,
        fill:"green"
    });
    layer.add(rotationPoint);


    $(stage.getContent()).on('mousemove', function (event) {

        // get the current mouse position on the stage
        var pos=stage.getMousePosition();
        var mouseX=parseInt(pos.x);
        var mouseY=parseInt(pos.y);

        // calculate the mouse angle relative 
        // to the rotation point [rx,ry]
        var dx=mouseX-rx;
        var dy=mouseY-ry;
        var radianAngle=Math.atan2(dy,dx);

        // "normalize" the angle so it always is in a proper range (0-2*PI)
        radianAngle=(radianAngle+Math.PI*2)%(Math.PI*2);

        // calculate the new image x,y 
        // based on the angle
        var x=rx+radius*Math.cos(radianAngle)-imgWidth/2;
        var y=ry+radius*Math.sin(radianAngle)-imgHeight/2;
        image.setPosition(x,y);

        layer.draw();
    });


    var img=new Image();
    img.onload=function(){
        image=new Kinetic.Image({
            image:img,
            x:0,
            y:0,
            width:imgWidth,
            height:imgHeight,
        });
        layer.add(image);
        layer.draw();
    }
    img.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house-icon.png";


}); // end $(function(){});

</script>       
</head>

<body>
    <p>Move mouse to rotate image around</p>
    <p>the green dot which is located at</p>
    <p>the rotation point</p>
    <div id="container"></div>
</body>
</html>
相关问题