2012-02-29 9 views
0

ちょっと、私はKineticjsを使ってドラッグ&ドロップ操作を実装しています 今、画像が近づくと、画像が互いにスナップします。 私の機能性のために私には役に立たなかったKineticJs グリッドにjqueryのスナップが見えましたが、それを使用すると、KineticJsを取り除かなければなりません。 jqueryの原因は、imgのidを渡してドラッグ可能にしてからスナップすることです。あなたがつもりならhttp://www.kineticjs.com/Jqueryをドラッグ&ドロップしてkineticSSのグリッドにスナップする

おかげ

アシシュ

答えて

5

http://jqueryui.com/demos/draggable/#snap-to

kinetic.js: は、どのように私は私のkinetic.js画像ドラッグ

jQueryを使ってこれを使用することができますnxmグリッドにスナップするものは、スナップする関数を呼び出すリスナーを追加したいと思うでしょう。おそらく、元

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

広場のタイルのために100×100グリッド... "dragendイベント"

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

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

でこれをしたいと思います: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

+1

ありがとうございました。助けを求めて...それを忘れました。:) – ashishashen

+1

喜んで聞いてください。私はKineticJSの大きな支持者であり、あなたがそれを使用しているのを見てうれしいです。あなたの創造を共有するためにKineticJSフォーラムに投稿することをお勧めします。 – randompast

0

私は最初のグリッド

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

を作成し、私はその後のMouseMove

にセルその後、

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); 
}); 
関連する問題