0

ねえ、Kineticjsを使用してドラッグアンドドロップ操作を実装しています。画像が近づいたら、互いにスナップさせたいです。KineticJsでビーチの動物の例を見ましたが、私の機能には役に立たなかったので、jqueryスナップを見ました。グリッドに..しかし、それを使用する場合は、KineticJsを削除する必要があります.kineticJsの要素にjqueryスナップを適切に実装する方法はありますか?jqueryでは、imgのIDを渡してドラッグ可能にしますそしてスナップします。では、これをkinetic.js画像でどのように使用できますか

jqueryドラッグ可能:http://jqueryui.com/demos/draggable/#snap-to

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

ありがとう

Ashish

4

2 に答える 2

5

nxmグリッドにスナップするものを探している場合は、それをスナップする関数を呼び出すリスナーを追加する必要があります。あなたはおそらく「ドラッグエンド」でこれをしたいと思うでしょう

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になります。

編集、おっと質問の一部を逃しました。他の正方形にスナップするために、これが私がすることです:

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;
        }
   }
}
于 2012-03-05T09:14:51.660 に答える
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;
    }

その後、mousemoveで

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

  var mousePos = stage.getMousePosition();
  cell = current_cell(mousePos)
  shape.setPosition(cell.x, cell.y);
});
于 2013-09-29T22:09:38.953 に答える