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 8969441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:35:29+00:00 2026-06-15T17:35:29+00:00

In kineticjs I am creating dynamic rectangles that are draggable. However when I create

  • 0

In kineticjs I am creating dynamic rectangles that are draggable. However when I create a new rectangle, the rectangle behind it automatically drags. I dont want this to happen.

You can see the behaviour in demo at http://jsfiddle.net/sandeepy02/8kGVD/12/

Step 1: Choose “create rectangle” and create rectangles.
Step 2: Choose “Move rectangle” and move the rectangles.
Step 3: Choose “create rectangle” and create rectangles. This causes the rectangles previously created to also move. This is unwanted.

<html>
    <head>
        <script>
function valButton(radios) {
    var btn = document.getElementsByName(radios);
    var cnt = -1;
    for (var i = btn.length - 1; i > -1; i--) {
        if (btn[i].checked) {
            cnt = i;
            i = -1;
        }
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
}

window.onload = function() {
    layer = new Kinetic.Layer();
    stage = new Kinetic.Stage({
        container: "container",
        width: 320,
        height: 320
    });
    background = new Kinetic.Rect({
        x: 0,
        y: 0,
        width: stage.getWidth(),
        height: stage.getHeight(),
        fill: "white"
    });


    layer.add(background);
    stage.add(layer);

    moving = false;

    stage.on("mousedown touchstart", function() {
        var btnName = valButton("group2");
        if (btnName == "1") {
            if (moving) {
                moving = false;
                layer.draw();
            } else {
                var mousePos = stage.getMousePosition();
                rect = new Kinetic.Rect({
                    x: 22,
                    y: 7,
                    width: 0,
                    height: 0,
                    fill: 'red',
                    stroke: 'black',
                    strokeWidth: 4,
                    draggable: true
                });
                layer.add(rect);
                //start point and end point are the same
                rect.setX(mousePos.x);
                rect.setY(mousePos.y);
                rect.setWidth(0);
                rect.setHeight(0);
                moving = true;
                layer.drawScene();
            }
        }
        document.all.text.innerText = btnName +" "+moving;

    }); //end of mousedown
    stage.on("mousemove touchmove", function() {
        var btnName = valButton("group2");
        if (btnName == "1") {
            if (moving) {
                var mousePos = stage.getMousePosition();
                var x = mousePos.x;
                var y = mousePos.y;
                rect.setWidth(mousePos.x - rect.getX());
                rect.setHeight(mousePos.y - rect.getY());
                moving = true;
                layer.drawScene();
            }
        }
        else if (btnName == "3") {
            layer.draw();
        }
        document.all.text.innerText = btnName +" "+moving;
    }); //end of mousemove
    stage.on("mouseup touchend", function() {
        var btnName = valButton("group2");
        if (btnName == "1") {
            moving = false;
        }
        document.all.text.innerText = btnName +" "+moving;
    }); //end of mouseup
};
        </script>
    </head>
    <body>

        <h2>Toggle buttons</h2>
<div class="toggle-btn-grp">
    <label onclick="" class="toggle-btn"><input type="radio" value="1" name="group2"/> Create Rectangle</label>
    <label onclick="" class="toggle-btn"><input type="radio" value="3" name="group2"/>Move Rectangle</label>
</div>

        <div id="container" ></div>
                <div id="text" >abc</div>

    </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-15T17:35:30+00:00Added an answer on June 15, 2026 at 5:35 pm

    Here is your updated function to fix the problem –

        stage.on("mousedown touchstart", function() {
        var btnName = valButton("group2");
        if (btnName == "Create") {
            if (moving) {
                moving = false;
                layer.draw();
            } else {
                var mousePos = stage.getMousePosition();
                rect = new Kinetic.Rect({
                    x: 0,
                    y: 0,
                    width: 0,
                    height: 0,
                    fill: 'red',
                    stroke: 'black',
                    strokeWidth: 4,
                    draggable: true
                });
                layer.add(rect);
                //start point and end point are the same
                rect.setX(mousePos.x);
                rect.setY(mousePos.y);
                rect.setWidth(0);
                rect.setHeight(0);
                moving = true;
                rect.on("mousemove touchmove", function() {
                    var btnName = valButton("group2");
                    if (btnName == "Create") {
                        this.setDraggable(false);
                    }
                    else if (btnName == "Move") {
                        this.setDraggable(true);
                    }
                    document.all.text.innerText = btnName +" rect move MovingFlag: "+moving;
                }); //end of rect mousemove
                layer.drawScene();
            }
        }
        document.all.text.innerText = btnName +" MovingFlag: "+moving;
    
    }); //end of mousedown
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using KineticJS, i've been trying to create a group that has just a circle
Hey I am implementing the drag and drop operations using Kineticjs Now I want
I am trying to create a .d.ts file for the KineticJS library. So far
Currently I'm creating an web application with KineticJS, which includes dragging and dropping on
I'm trying to figure out why creating a canvas using KineticJS isn't working on
I want to show only intersection of two shape by using KineticJS . How
I want to create sort of a 3D effect with the shapes I generated
I'm trying to get KineticJS to work with Google Closure Compiler. KineticJS, however, generated
Hello guys I am a noob in KineticJS and I want to know how
I am quite new to using HTML5 Canvas and KineticJS so please forgive me

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.