简体   繁体   English

使用图像,kinetic.js,javascript对齐网格操作?

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

Can I implement snap to grid operation with images in KineticJS using jquery? 我可以使用jquery在KineticJS中使用图像实现对齐网格操作吗? ( http://jqueryui.com/demos/draggable/snap-to.html ) Like I have few draggable images inside a canvas and I want them to restrict the movement inside a canvas... http://jqueryui.com/demos/draggable/snap-to.html )就像我在画布中有很少的可拖动图像,我希望它们限制画布内的移动...

Is it even possible that 2 images can snap together when one comes near the other in the canvas?? 当一个图像在画布中靠近另一个图像时,它们是否可以一起拍摄? And can it be achieved using kinetic.js or javascript... 可以使用kinetic.js或javascript实现吗?

Thanks Ashish 谢谢Ashish

Here is the code.. it's a little complicated. 这是代码..它有点复杂。 I mean I'm loading images from outside the canvas..and there are two sets..now I want one set to be able to snap to other.. 我的意思是我正在从画布外面加载图像..并且有两套..现在我想要一套能够捕捉到其他..

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

You can do this different by using the dragBoundFunc. 您可以使用dragBoundFunc执行此操作。

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

I have created a complete snapping example: http://jsfiddle.net/PTzsB/1/ 我创建了一个完整的对齐示例: http//jsfiddle.net/PTzsB/1/

I submitted an answer to this question that doesn't use jQuery. 我提交了一个不使用jQuery的问题的答案。 Instead, there is a patch you can apply which gives you drag and drop with snap to grid in KineticJS on the HTML5 canvas. 相反,您可以应用一个补丁,它可以在HTML5画布上的KineticJS中使用捕捉到网格进行拖放。

Using jquery draggable UI with kineticJs to make elements snap to grid? 使用带有kineticJs的jquery draggable UI来使元素对齐网格?

This is all very possible. 这一切都很有可能。 It requires being a bit more familiar than the average jQuery user, though. 但它需要比普通的jQuery用户更熟悉。

First, implementing the snap-to: 首先,实现snap-to:

This is a simple idea. 这是一个简单的想法。 You use the jQuery UI Library. 您使用jQuery UI库。 You add the necessary function for the 'snap-to' feature, by invoking 'snap-to' on all elements with the class of 'KineticJsImage'. 通过在“KineticJsImage”类的所有元素上调用“snap-to”,可以为“snap-to”功能添加必要的功能。

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

Second, for all images propogated by KineticJs, we add the class 'KineticJsImage' 其次,对于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.

As you mentioned in your first question, you'd found the snap-to operation. 正如您在第一个问题中提到的,您已经找到了快照操作。 The 2nd box in the demo on that page uses the generic (code I mentioned above, as well) snap: true parameter. 该页面演示中的第二个框使用了通用(我上面提到的代码)snap:true参数。 When you invoke this, you're telling the page to snap all draggable elements with the class of 'KineticJsImage' to ANY element that has ALSO been declared draggle. 当你调用它时,你告诉页面将所有带有'KineticJsImage'类的可拖动元素捕捉到任何已被声明为draggle的元素。

 $(".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.

Everything you want to achieve is doing able with jQuery UI and the draggable / droppable extensions provided within. 您想要实现的一切都是使用jQuery UI和其中提供的可拖动/可放置扩展。

Fool around and try to figure out out. 傻瓜,试着搞清楚。 When you can't, come back with the code and we'll show you where to go from there. 如果你不能,请回到代码,我们会告诉你去哪里。 $(" $(”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM