2012-02-28 56 views
1

我可以使用jquery實現捕捉網格操作與KineticJS 中的圖像嗎? (http://jqueryui.com/demos/draggable/snap-to.html) 就像我在畫布中有很少可拖動的圖像,並且我希望它們限制畫布內部的移動...使用圖像對齊網格操作,kinetic.js,javascript?

當畫布中的某個靠近另一個時,是否有可能將2個圖像拼接在一起?? 而且可以在使用kinetic.js或JavaScript來實現......

感謝 阿希什

這裏是代碼.. 這是一個有點複雜。我的意思是,我從外面canvas..and加載圖像有兩種sets..now我想要一套能夠捕捉到其他..

<script src="kinetic-v3.8.0.min.js"> 
    </script> 
    <link rel="stylesheet" href="../../themes/base/jquery.ui.all.css"> 
<script src="jquery-1.7.1.js"></script> 
<script src="jquery.ui.core.js"></script> 
<script src="jquery.ui.widget.js"></script> 
<script src="jquery.ui.mouse.js"></script> 
<script src="jquery.ui.draggable.js"></script> 



    <script> 
     function drawImage(imageObj){ 

      var stage = new Kinetic.Stage("container", 578, 500); 
      var layer = new Kinetic.Layer(); 
      var x = stage.width/2 - 200/2; 
      var y = stage.height/2 - 137/2; 
      var width = 200; 
      var height = 137; 

      // darth vader 
      var darthVaderImg = new Kinetic.Shape(function(){ 
       var context = this.getContext(); 

       context.clearRect(x,y,width,height); 
       context.drawImage(imageObj, x, y, width, height); 
       // draw invisible detectable path for image 
       context.beginPath(); 
       context.rect(x, y, width, height); 
       context.closePath(); 


      }); 

      // enable drag and drop 
      darthVaderImg.draggable(true); 

      // add cursor styling 
      darthVaderImg.on("mouseover", function(){ 
       document.body.style.cursor = "pointer"; 
      }); 
      darthVaderImg.on("mouseout", function(){ 
       document.body.style.cursor = "default"; 
      }); 
      //remove image on double click 
      darthVaderImg.on("dblclick dbltap", function(){ 
      layer.remove(darthVaderImg); 


      layer.draw(); 
       }); 
      layer.add(darthVaderImg); 
      stage.add(layer); 

      //events 

     } 


     function drawImage2(imageObj){ 

      var stage = new Kinetic.Stage("container", 578, 500); 
      var layer = new Kinetic.Layer(); 

      var x = stage.width/2 - 300 ; 
      var y = stage.height/2 - 137 ; 
      var width = 200; 
      var height = 137; 

      // darth vader 

      var darthVaderImg2 = new Kinetic.Shape(function(){ 
       var context = this.getContext(); 

       context.drawImage(imageObj, x, y, width, height); 

       // draw invisible detectable path for image 
       context.beginPath(); 
       context.rect(x, y, width, height); 
       context.closePath(); 

      }); 

      // enable drag and drop 
      darthVaderImg2.draggable(true); 

      // add cursor styling 
      darthVaderImg2.on("mouseover", function(){ 
       document.body.style.cursor = "pointer"; 
      }); 
      darthVaderImg2.on("mouseout", function(){ 
       document.body.style.cursor = "default"; 
      }); 
      //remove image on double click 
      darthVaderImg2.on("dblclick dbltap", function(){ 
      layer.remove(darthVaderImg2); 


      layer.draw(); 



        }); 
       layer.add(darthVaderImg2); 

      stage.add(layer); 



      $(".darthVaderImg2").draggable({ grid: [ 20,20 ] }); 

      } 



       function load(img){ 
       // load image 

       var imageObj = new Image(); 
       imageObj.onload = function(){ 

       drawImage(this); 

        }; 
        imageObj.src = img.src; 
        }; 
       function load2(img){ 
      // load image 
       var imageObj = new Image(); 
       imageObj.onload = function(){ 
       drawImage2(this); 
       }; 
       imageObj.src = img.src; 
       }; 
      </script> 
      <title>HTMl5 drag drop multiple elements</title></head> 
      <body onmousedown="return false;"> 
       <div id="container"> 
       </div> 
      <div id="check1"> 
      <ul id="img"> <li><a href="#"onclick="load(document.getElementById('i1'))"> 
      <img class="ui-widget-header" src="dog.png" id="i1" alt="doggie"  width="60"height="55"/> 
     </a></li> 
     <li> 
     <a href="#" onclick="load(document.getElementById('i2'))"> 
     <img src="dog2.png" id="i2" alt="Pulpit rock" width="60" height="55"  /></a> 
    </li> 
    </ul> 
    </div> 
    <ul id="img1"> 
     <li><a href="#" onclick="load2(document.getElementById('i4'))"> 
      <img alt="doggie" src="beach.png" id="i4" width="60" height="55" /> 
      </a></li> 
      <li><a href="#" onclick="load2(document.getElementById('i5'))"> 
     <img alt="doggie" src="cat3.png" id="i5" width="60" height="55" /></a></li> 
     </ul> 
     </body> 
     </html> 

回答

1

這是非常可能的。不過,它需要比普通的jQuery用戶更熟悉一些。

首先,實現捕捉到:

這是一個簡單的想法。你使用jQuery UI庫。通過在所有具有'KineticJsImage'類的元素上調用'snap-to',爲'snap-to'特性添加必要的功能。

$(".KineticJsImage").draggable({ snap: true }); 

其次,通過KineticJs將會傳播的所有圖像,我們添加了類「KineticJsImage」

..I don't have anything to work with here... 
You simply need to find where the image output is controlled and add a class 
of KineticJsImage to the code. 

正如你在第一個問題中提到,你會發現捕捉到操作。在該頁面上的演示中的第二個框使用通用(我上面提到的代碼)snap:true參數。當你調用這個函數時,你告訴頁面將所有可拖動的元素與'KineticJsImage'類一起捕捉到已被聲明爲拖動的ANY元素。你想達到

$(".someElement").draggable({ snap: false }); // drags wherever, does not snap. 
$(".KineticJsImage").draggable({snap: true }); // drags everywhere, snaps to anything. 
$(".KineticJsImage").draggable({snap: '.KineticJsImage' }); // This will ensure that 
any element with the class of 'KineticJsImage' is not only draggable, but will snap 
to any other element with the class of' 'KineticJsImage' that is also draggable. 

一切都做能夠與jQuery UI的和內提供的可拖動/ droppable的擴展。

鬼混,試圖弄清楚。當你不能時,回來的代碼,我們會告訴你從那裏去哪裏。 $(」

+0

謝謝...我一定會試試這個..現在這裏的東西是..JQuery是使元素可拖動..但我的元素已經拖動..所以我有點困惑與..和Ive得到實施這個地方..n怎麼..讓我嘗試一些..並且一定會分享代碼..然後請幫助我..謝謝很多 – ashishashen 2012-02-28 09:21:12

+0

我可以問爲什麼你不能擺脫舊的擴展,並使用jQuery UI Draggable獨立? – Ohgodwhy 2012-02-28 09:25:38

+0

導致我的老闆告訴我在html5 canvas中實現東西.. – ashishashen 2012-02-28 09:39:03

2

您可以通過使用dragBoundFunc做到這一點不同

 return { 
     x: Math.round(pos.x/grid) * grid, 
     y: Math.round(pos.y/grid) * grid 
     } 

我已經創建了一個完整的捕捉例子:http://jsfiddle.net/PTzsB/1/