Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7920655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:18:05+00:00 2026-06-03T16:18:05+00:00

I’m new to jquery I have implemented drag and drop operation using Kinetic.js I

  • 0

I’m new to jquery
I have implemented drag and drop operation using Kinetic.js
I have some images in html and I’m passing them to a javascript function and making them draggable..There are two sets of images..
Now I want to make them snap to each other if they get near.
I’m new to jquery so I don’t know how can I pass the kinetic js image variable into a jquery operation where they have passed the img id tag.
Also I’m not able to figure out where to place the jquery function and how..

Here is the code..
Someone please help..

        <html>
            <head>
                  <style>

           canvas {
                border: 1px solid #9C9898;
                    }

            #img
                {
                position:absolute;
                left:700px;
                top:150px;
                }
            #img1
                {
                position:absolute;
                left:800px;
                top:150px;
                }
    </style>

Loading jquery libraries

    <script src="kinetic-v3.8.0.min.js"></script>
    <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>

Kinetic.js- to load image and make em draggable

    <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.drawImage(imageObj, x, y, width, height);
                // draw invisible detectable path for image

Need some help with this jquery function how to use it and pass the kinetic js draggable image here

                $(function() {
                $(this).draggable({ grid: [ 80, 80 ] });
                });

                context.beginPath();
                context.rect(x, y, width, height);
                context.closePath();  
          });

draggable function

            // 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);
          }



        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>

Html defining operation on image click

            <title>HTMl5 drag drop multiple elements</title></head>
            <body onmousedown="return false;">
            <div id="container">
                </div>
             <div id="check1">
            <ul id="img" class="draggable ui-widget-content"    > <li><a href="#" class="draggable ui-widget-content" onclick="load(document.getElementById('i1'))">
            <img src="dog.png" id="i1" class="draggable ui-widget-content" 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:18:07+00:00Added an answer on June 3, 2026 at 4:18 pm

    I was able to modify kinetic-v3.9.3.js to enable dragging with snap-to with the following patch (diff format):

    813a814,825
    >      * set drag grid constraint
    >      */
    >     setDragGridConstraint: function(constraint) {
    >         this.attrs.dragGridConstraint = constraint;
    >     },
    >     /**
    >      * get drag grid constraint
    >      */
    >     getDragGridConstraint: function() {
    >         return this.attrs.dragGridConstraint;
    >     },
    >     /**
    1847a1860,1861
    >                     var dgc = node.attrs.dragGridConstraint;
    >                     
    1858a1873,1877
    >                     if(dgc !== undefined) {
    >                         newNodePos.x = Math.floor(newNodePos.x/dgc.x)*dgc.x + (dgc.x/2);
    >                         newNodePos.y = Math.floor(newNodePos.y/dgc.y)*dgc.y + (dgc.y/2);
    >                     }
    > 
    

    To set this up for something like a rect, make sure you set draggable to true on your entity, then you can use setDragGridConstraint() to set up your snap to distances as below.

            var box = new Kinetic.Rect({
                x: rectX,
                y: rectY,
                width: 100,
                height: 50,
                fill: "#00D2FF",
                stroke: "black",
                strokeWidth: 4,
                draggable: true
            });
    
            box.setDragGridConstraint({x: 50 , y: 50});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have thousands of HTML files to process using Groovy/Java and I need to
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.