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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:53:38+00:00 2026-06-17T11:53:38+00:00

I am quite new to javascript and to Raphael. I am trying to move

  • 0

I am quite new to javascript and to Raphael. I am trying to move a button-like rectangle with text inside. Here is my code :

window.onload = function() {
  var paper = new Raphael(document.getElementById('canvas_container'), "100%", "100%");
  var box1 = paper.rect(100, 100, 120, 50, 10).attr({fill: 'darkorange', stroke: '#3b4449', 'stroke-width': 2, cursor: 'pointer'});

  var box2 = paper.rect(400,100,120,50,10).attr({fill: 'lightblue', stroke: '#3b4449', 'stroke-width': 2});
  var text2 = paper.text(box2.attrs.x + box2.attrs.width/2,box2.attrs.y + box2.attrs.height/2,"[x: " + box2.attrs.x + " y: " + box2.attrs.y + "]").attr({"font-family": "arial", "font-size": 16});
  var button2 = paper.set();
  button2.push(box2); 
  button2.push(text2);

  box1.click(function(){
    // this did not work
    // button2.animate({x: 100, y: 50 }, 1000, 'bounce', function() { // callback function
    //  text2.attr('text',"[x: " + box2.attrs.x + " y: " + box2.attrs.y + "]");
    // });
    button2.animate({transform: "t100,100"}, 1000, 'bounce', function() { // callback function
        text2.attr('text',"[x: " + box2.attrs.x + " y: " + box2.attrs.y + "]");
    });
  });
}

The button2.animate({x: 100, y: 50 }, 1000, 'bounce'); line did not worked properly, the text was not in the right position at the end. By using the transform: I can not use coordinates, I would have to compute them. Also I am not able to get the right coordinates of the blue box at the end when using the transform method.

I was not able to find any answer yet, hope someone can help me.

Thank you

  • 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-17T11:53:39+00:00Added an answer on June 17, 2026 at 11:53 am

    Since you didn’t explain how exactly you want to move your button, I’m assuming you want to move the box2 above box1.

    There are some misunderstandings and errors in your code, allow me explain one by one.

    Why the first way cause text move to wrong position at end ?

    Because a set is NOT a group of element which knows its relative position inside the group. A set is merely a collection of elements which is designed for us to operate them in a more convenient way.

    So, the code below will move all element in the set to (100, 50)

    set.animate({x: 100, y: 50 }, 1000);
    

    and that’s why the text is there.

    I couldn’t find the document, but you can find some explanation here .

    Why x, y in attributes seems to be wrong when using transform ?

    No, the attribute is correct.

    When you transform an element, the result of the transformation will not reflect back to the attributes. You can think like this, when transform(), you are actually attach “transformation” to the elements. Therefore :

    paper.circle(100, 100, 5).transform("t100");
    

    You can describe the circle as :

    a circle at (100, 100) which will be moved 100px horizontally.

    but not – a circle at (200, 100) which will be moved 100px horizontally.


    So, here is the code that dose what you want, note that I’m using getBBox() to get coordinate of the button2 set.

      box1.click(function(){
        var moveX = box1.attr("x") - button2.getBBox().x;
        var moveY = (box1.attr("y") - 50) - button2.getBBox().y;
        button2.animate({transform: "t" + moveX + "," + moveY}, 1000, 'ease-in-out', function () {
          text2.attr('text', "[x: " + button2.getBBox().x + "y: " + button2.getBBox().x + "]");
        });
      });
    

    Welcome to SO, and suggest you to write a SSCCE next time.


    UPDATE

    I do not fully understand why the transformation does not reflect back
    to the attributes. If I move the circle at the position (100,100)
    100px horizontally it will results in a circle at position (200,100).
    This is what the bounding box gives me. So why I am not able to get
    the coordinates from the circle after the transformation and have to
    use the bounding-box method ?

    Transform DOSE NOT change the original attribute in the element, because it is something you attach to a element, not function that change a element directly. If you want to know attributes AFTER the transformation applied, you have to use getBBox(), or take a look about matrix.

    This is how Raphael.js works. Either you use bounding box function, or extend the Raphael.js by yourself like this

    I have changed my previous answer about how I describe transformation a little bit, hope it can help you to understand better this time.

    Your code works great but it has the drawback, that you have to
    compute the transformation values instead of simply setting the
    position. Is there any other way to move a rectangle with text inside
    to a position of your choice ?

    You can always write helper functions to do these ugly jobs for you anyway, I don’t see there’s anything wrong with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm quite new to JavaScript andI'm trying to do the following: I've two webpages
Quite new to maven here so let me explain first what I am trying
I am quite new to backbone.js with little experience in javaScript. I was trying
i'm quite new to Dojo, but i am trying to convert an existing javascript
I am quite new to Javascript and node.js and I'm trying to create a
I am quite new to JavaScript, I have written some code that just reloads
Hi I'm quite new to javascript and I want to get extract a text
Again, i'm quite new to Javascript and Extjs. code: var test = [ {
im quite new to php and javascript but still trying to get my webpage
Context: I'm quite new to what I like to think of as, serious javascript/jquery

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.