Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 5.37 KB  |  hits: 34  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Snap to grid operation using images,kinetic.js,javascript?
  2. <script src="kinetic-v3.8.0.min.js">
  3.   </script>
  4.   <link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
  5. <script src="jquery-1.7.1.js"></script>
  6. <script src="jquery.ui.core.js"></script>
  7. <script src="jquery.ui.widget.js"></script>
  8. <script src="jquery.ui.mouse.js"></script>
  9. <script src="jquery.ui.draggable.js"></script>
  10.  
  11.  
  12.  
  13.     <script>
  14.         function drawImage(imageObj){
  15.  
  16.             var stage = new Kinetic.Stage("container", 578, 500);
  17.             var layer = new Kinetic.Layer();
  18.             var x = stage.width / 2 - 200 / 2;
  19.             var y = stage.height / 2 - 137 / 2;
  20.             var width = 200;
  21.             var height = 137;
  22.  
  23.             // darth vader
  24.             var darthVaderImg = new Kinetic.Shape(function(){
  25.                 var context = this.getContext();
  26.  
  27.                 context.clearRect(x,y,width,height);
  28.                 context.drawImage(imageObj, x, y, width, height);
  29.                 // draw invisible detectable path for image
  30.                 context.beginPath();
  31.                 context.rect(x, y, width, height);
  32.                 context.closePath();
  33.  
  34.  
  35.           });
  36.  
  37.             // enable drag and drop
  38.             darthVaderImg.draggable(true);
  39.  
  40.             // add cursor styling
  41.             darthVaderImg.on("mouseover", function(){
  42.                 document.body.style.cursor = "pointer";
  43.             });
  44.             darthVaderImg.on("mouseout", function(){
  45.                 document.body.style.cursor = "default";
  46.             });
  47.             //remove image on double click
  48.             darthVaderImg.on("dblclick dbltap", function(){
  49.             layer.remove(darthVaderImg);
  50.  
  51.  
  52.             layer.draw();
  53.                 });
  54.             layer.add(darthVaderImg);
  55.             stage.add(layer);
  56.  
  57.             //events
  58.  
  59.         }
  60.  
  61.  
  62.          function drawImage2(imageObj){
  63.  
  64.             var stage = new Kinetic.Stage("container", 578, 500);
  65.             var layer = new Kinetic.Layer();
  66.  
  67.             var x = stage.width / 2 - 300 ;
  68.             var y = stage.height / 2 - 137 ;
  69.             var width = 200;
  70.             var height = 137;
  71.  
  72.             // darth vader
  73.  
  74.             var darthVaderImg2 = new Kinetic.Shape(function(){
  75.                 var context = this.getContext();
  76.  
  77.                 context.drawImage(imageObj, x, y, width, height);
  78.  
  79.                 // draw invisible detectable path for image
  80.                 context.beginPath();
  81.                 context.rect(x, y, width, height);
  82.                 context.closePath();
  83.  
  84.             });
  85.  
  86.             // enable drag and drop
  87.             darthVaderImg2.draggable(true);
  88.  
  89.             // add cursor styling
  90.             darthVaderImg2.on("mouseover", function(){
  91.                 document.body.style.cursor = "pointer";
  92.             });
  93.             darthVaderImg2.on("mouseout", function(){
  94.                 document.body.style.cursor = "default";
  95.             });
  96.             //remove image on double click
  97.             darthVaderImg2.on("dblclick dbltap", function(){
  98.             layer.remove(darthVaderImg2);
  99.  
  100.  
  101.             layer.draw();
  102.  
  103.  
  104.  
  105.                    });
  106.                 layer.add(darthVaderImg2);
  107.  
  108.              stage.add(layer);
  109.  
  110.  
  111.  
  112.           $( ".darthVaderImg2" ).draggable({ grid: [ 20,20 ] });
  113.  
  114.              }
  115.  
  116.  
  117.  
  118.                function load(img){
  119.                  // load image
  120.  
  121.                  var imageObj = new Image();
  122.                  imageObj.onload = function(){
  123.  
  124.                 drawImage(this);
  125.  
  126.                   };
  127.                    imageObj.src = img.src;
  128.                     };
  129.                  function load2(img){
  130.              // load image
  131.                  var imageObj = new Image();
  132.                 imageObj.onload = function(){
  133.                 drawImage2(this);
  134.                };
  135.                imageObj.src = img.src;
  136.                  };
  137.              </script>
  138.             <title>HTMl5 drag drop multiple elements</title></head>
  139.              <body onmousedown="return false;">
  140.               <div id="container">
  141.               </div>
  142.              <div id="check1">
  143.             <ul id="img"> <li><a href="#"onclick="load(document.getElementById('i1'))">
  144.             <img class="ui-widget-header" src="dog.png" id="i1" alt="doggie"     width="60"height="55"/>
  145.       </a></li>
  146.       <li>
  147.         <a href="#" onclick="load(document.getElementById('i2'))">
  148.         <img src="dog2.png" id="i2" alt="Pulpit rock" width="60" height="55"        /></a>
  149.     </li>
  150.     </ul>
  151.     </div>
  152.     <ul id="img1">
  153.         <li><a href="#" onclick="load2(document.getElementById('i4'))">
  154.             <img alt="doggie" src="beach.png" id="i4" width="60" height="55" />
  155.              </a></li>
  156.              <li><a href="#" onclick="load2(document.getElementById('i5'))">
  157.         <img alt="doggie" src="cat3.png" id="i5" width="60" height="55" /></a></li>
  158.        </ul>
  159.        </body>
  160.         </html>
  161.        
  162. $( ".KineticJsImage" ).draggable({ snap: true });
  163.        
  164. ..I don't have anything to work with here...
  165. You simply need to find where the image output is controlled and add a class
  166. of KineticJsImage to the code.
  167.        
  168. $(".someElement").draggable({ snap: false }); // drags wherever, does not snap.
  169.  $(".KineticJsImage").draggable({snap: true }); // drags everywhere, snaps to anything.
  170.  $(".KineticJsImage").draggable({snap: '.KineticJsImage' }); // This will ensure that
  171.  any element with the class of 'KineticJsImage' is not only draggable, but will snap
  172.  to any other element with the class of' 'KineticJsImage' that is also draggable.