2012-02-29 28 views
0

嘿,我正在实施使用Kineticjs的拖放操作 现在我想图像互相捕捉,如果他们接近.. 我看到了动物在海滩上的例子在KineticJs这对我没有任何帮助,因为我的功能 然后看到了jquery捕捉到网格的东西..但是如果我使用它,那么我将不得不摆脱KineticJs。是否有任何方法来实现jquery snap propery元素kineticJs 在jquery中导致它们已经通过了img的id使它可以拖动然后捕捉。 因此,如何能我用这个跟我kinetic.js图像jquery拖放和捕捉kinetic中的网格

jQuery的可拖动:http://jqueryui.com/demos/draggable/#snap-to

kinetic.js:http://www.kineticjs.com/

感谢

阿希什

回答

5

如果你打算为捕捉到一个nxm网格的东西,你想添加一个侦听器来调用一个捕捉它的函数。你可能会想在 “dragend”

http://www.html5canvastutorials.com/kineticjs/html5-canvas-drag-and-drop-events-tutorial/

box.on("dragend", function(){ 
    snaptogrid(box); 
    }); 

... 100x100的网格广场砖

function snaptogrid(YourObject){ 
    YourObject.x = Math.floor(YourObject.x/100) * 100 + 50; 
    YourObject.y = Math.floor(YourObject.y/100) * 100 + 50; 
    } 

前要做到这一点:如果YourObject.x = 325,那么你将有Math.floor(3.25)* 100 + 50 = 350.左边和顶部应该都在300,而中心在350.

编辑,oops错过了部分问题。为了捕捉到其他的广场,这里就是我想要做的:

function snaptoOther(YourSquares, index){ 
    var lastone = YourSquares.length-1; 
    var maxDist = 125; 
    var minDist = 75; 
    var squarelength = 100; 

    for(var i=0; i<lastone; i++) 
    { 
    var checkx = abs(YourSquares[index].x - YourSquares[i].x); 
    var checky = abs(YourSquares[index].y - YourSquares[i].y); 
    var multiplyX = Math.round((YourSquares[index].x - YourSquares[i].x)/squarelength); 
    var multiplyY = Math.round((YourSquares[index].y - YourSquares[i].y)/squarelength); 

     if(checkx < maxDist && checkx > minDist && i !== index) 
     { 
      YourSquares[index].x = YourSquares[i].x + multiplyX*squarelength; 
     } 
     if(checky < maxDist && checky > minDist && i !== index) 
     { 
      YourSquares[index].y = YourSquares[i].y + multiplyY*squarelength; 
     } 
    } 
} 
+0

非常感谢,,,一定会试试看 – ashishashen 2012-03-05 09:32:17

+1

谢谢..对于帮助..把它.. :) – ashishashen 2012-03-05 12:06:09

+1

很高兴听到。我是KineticJS的大力支持者,很高兴看到您使用它。我鼓励你在KineticJS论坛发帖分享你的创作。 – randompast 2012-03-06 05:04:01

0

我首先创建网格

var grid = new Array(gridSize); 
    for (var row = 0; row < gridSize; row++) { 
     grid[row] = new Array(gridSize);// the columns 
    } 

我再拿到细胞

function current_cell(mousePos) { 
     var x = mousePos.x + half_column_width; 
     var y = mousePos.y + half_column_height; 
     if ((x < column_width) || (y < column_height) || (x > board_width + column_width) || (y > board_height + column_height)) { 
     return false; 
     } 
     x = Math.min(x, (board_width * column_width) + column_width); 
     y = Math.min(y, (board_height * column_height) + column_height); 
     cell_x = Math.floor(x/column_width); 
     cell_y = Math.floor(y/column_height); 
     if (grid[cell_x][cell_y] == undefined) { 
     grid[cell_x][cell_y] = {}; 
     } 
     var cell = grid[cell_x][cell_y]; 
     cell.cell_x = cell_x; 
     cell.cell_y = cell_y; 
     cell.x = cell_x * piece_width; 
     cell.y = cell_y * piece_height; 
     return cell; 
    } 

然后在鼠标移动

shape.on('dragmove', function() { 

    var mousePos = stage.getMousePosition(); 
    cell = current_cell(mousePos) 
    shape.setPosition(cell.x, cell.y); 
});