使用图像、dynamic.js、javascript进行网格捕捉操作

Snap to grid operation using images,kinetic.js,javascript?

本文关键字:网格 操作 javascript 图像 dynamic js      更新时间:2023-09-26

我可以在KineticJS中用图像实现网格捕捉操作吗使用jquery?(http://jqueryui.com/demos/draggable/snap-to.html)就像我在画布内有一些可拖动的图像,我希望它们限制画布内的移动。。。

当画布中的一个图像靠近另一个图像时,两个图像是否可以捕捉在一起??它可以使用kinetic.js或javascript实现吗。。。

谢谢Ashish

这是代码。。这有点复杂。我的意思是我正在从画布外加载图像。。有两套。。现在我希望一个集能够捕捉到另一个集。。

  <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>

您可以使用dragBoundFunc来实现这一点。

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

我已经创建了一个完整的捕捉示例:http://jsfiddle.net/PTzsB/1/

我提交了一个不使用jQuery的问题答案。相反,你可以应用一个补丁,在HTML5画布上的KineticJS中通过捕捉到网格进行拖放。

使用带有kineticJs的jquery可拖动UI使元素捕捉到网格?

这一切都是可能的。不过,它需要比普通jQuery用户更熟悉一点。

首先,实现快照到:

这是一个简单的想法。您使用的是jQuery UI库。通过调用类为"KineticJsImage"的所有元素上的"snap-to",可以为"snap-to-"功能添加必要的函数。

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

第二,对于KineticJs提供的所有图像,我们添加了类"KineticJ图像"

..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"类的可拖动元素捕捉到也已声明为draggle的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.

您想要实现的一切都可以通过jQueryUI和其中提供的可拖动/可丢弃扩展来实现。

糊弄一下,试着弄明白。当你做不到的时候,带着代码回来,我们会告诉你从那里去哪里。$("