2015-02-09 124 views
2

我想拖放畫布中的圖像,並且我在canvas中獲得了該圖像的完美座標。但每當我在畫布中移動同一圖像時,我都不會獲得該圖像的新座標。當我移動它時,我需要幫助來確定該圖像的新座標。如何獲得畫布中圖像元素的座標

這是我的代碼:

// get the offset position of the kinetic container 
    var $stageContainer=$("#container"); 
    var stageOffset=$stageContainer.offset(); 
    var offsetX=stageOffset.left; 
    var offsetY=stageOffset.top; 

    // create the Kinetic.Stage and layer 
    var stage = new Kinetic.Stage({ 
     container: 'container', 
     width: 350, 
     height: 350 
    }); 
    var layer = new Kinetic.Layer(); 
    stage.add(layer); 

    // start loading the image used in the draggable toolbar element 
    // this image will be used in a new Kinetic.Image 
    var image1=new Image(); 
    image1.onload=function(){ 
     $house.show(); 
    } 
    image1.src="https://dl.dropboxusercontent.com/u/139992952/multple/4top.png"; 

    // make the toolbar image draggable 
    $house.draggable({ 
     helper:'clone', 
    }); 

    // set the data payload 
    $house.data("url","house.png"); // key-value pair 
    $house.data("width","32"); // key-value pair 
    $house.data("height","33"); // key-value pair 
    $house.data("image",image1); // key-value pair 

    // make the Kinetic Container a dropzone 
    $stageContainer.droppable({ 
     drop:dragDrop, 
    }); 

    // handle a drop into the Kinetic container 
    function dragDrop(e,ui){ 

     // get the drop point 
     var x=parseInt(ui.offset.left-offsetX); 
     var y=parseInt(ui.offset.top-offsetY); 

     // get the drop payload (here the payload is the image) 
     var element=ui.draggable; 
     var data=element.data("url"); 
     var theImage=element.data("image"); 

     // create a new Kinetic.Image at the drop point 
     // be sure to adjust for any border width (here border==1) 
     var image = new Kinetic.Image({ 
     name:data, 
     x:x, 
     y:y, 
     image:theImage, 
     draggable: true 
     }); 
     layer.add(image); 
     layer.draw(); 
    } 
    body{padding:20px;} 
    #container{ 
     border:solid 1px #ccc; 
     margin-top: 10px; 
     width:350px; 
     height:350px; 
    } 
    #toolbar{ 
     width:350px; 
     height:35px; 
     border:solid 1px blue; 
    } 
    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script> 
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.7.2.min.js"></script> 
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script> 

    <h4>Drag from toolbar onto canvas. Then drag around canvas.</h4> 
    <div id="toolbar"> 
     <img id="house" width=32 height=32 src="https://dl.dropboxusercontent.com/u/139992952/multple/4top.png"><br> 
    </div> 
    <div id="container"></div> 

回答

1

每個KineticJS對象,像圖像對象,具有X,持有其當前位置y屬性。這樣

var x=myImageObject.x(); 
var y=myImageObject.y(); 

或者你也可以獲取包含當前x對象,Y:

在最近版本KineticJS的,你可以獲取對象的x,y使用

// myXY has x,y properties: myXY.x and myXY.y 
var myXY=myImageObject.position(); 
+0

但會你的代碼在第一次拖放操作後在畫布中移動它之後給我當前的x,y座標圖像?現在我只能在第一次放下操作後獲得圖像的x,y座標,而在移動該圖像之後,不會再獲取新圖像。 – 2015-02-10 06:45:27

+0

您在dragdrop事件處理程序中創建KineticJS對象時提供了初始x,y - 我看到您正在適當地爲jQuery拖放座標提供新的KineticJS對象。是的,.x()&.y()由KineticJS維護,因此它們反映了新KineticJS對象的任何拖動。 – markE 2015-02-10 06:50:02

+0

我明白了你的觀點。將圖像從一個地方移動到另一個地方後,我正在獲取圖像的更新值。你能幫我解決畫布中重疊的圖像嗎? – 2015-02-10 08:15:07