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

  • Home
  • SEARCH
  • 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 9270901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:29:05+00:00 2026-06-18T15:29:05+00:00

I bumped into the following problem, hope someone will know how to help me:

  • 0

I bumped into the following problem, hope someone will know how to help me:

I work with the JavaScript library Raphael. Now, what I want to do is, when I have many Raphael SVG elements, to simply select more elements with “rectangle selection”, i.e. by dragging the mouse starting from the graph’s background to create a selection rectangle (I hope I was clear enough), and move the elements which are in this rectangle.

For now, I’ve found something like this (someone posted it from a previous question of mine):

var paper = Raphael(0, 0, '100%', '100%');

var circle = paper.circle(75, 75, 50);
var rect = paper.rect(150, 150, 50, 50);

var set = paper.set();

set.push(circle, rect);
set.attr({
    fill: 'red',
    stroke: 0
});

var ox = 0;
var oy = 0;
var dragging = false;

set.mousedown(function(event) {
    ox = event.screenX;
    oy = event.screenY;
    set.attr({
        opacity: .5
    });
    dragging = true;
});

set.mousemove(function(event) {
    if (dragging) {
        set.translate(event.screenX - ox, event.screenY - oy);
        ox = event.screenX;
        oy = event.screenY;
    }
});

set.mouseup(function(event) {
    dragging = false;
    set.attr({
        opacity: 1
    });
});

This code can be executed on jsfiddle. But, as you can see, this selects ALL the elements, by simply adding them to a Raphael set.

Now, I think that my problem will be solved by:

  1. Making a rectangle selection
  2. Adding the nodes which are in the rectangle to a Raphael set
  3. Move only the selected items (i.e. move only the items which are in the Raphael set with set.mousemove)

My problem for now would be the first two issues.

Any ideas how to do this?

Thank you in advance!

  • 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-18T15:29:06+00:00Added an answer on June 18, 2026 at 3:29 pm

    Fun problem. You can do this by placing a rectangular “mat” the size of the canvas behind all of your other objects and attaching a drag event to it for selecting other elements. (Note this solution uses the newer version of Raphael, 2.1.0:

    var paper = Raphael(0, 0, '100%', '100%');
    
    //make an object in the background on which to attach drag events
    var mat = paper.rect(0, 0, paper.width, paper.height).attr("fill", "#FFF");
    
    var circle = paper.circle(75, 75, 50);
    var rect = paper.rect(150, 150, 50, 50);
    var set = paper.set();
    
    set.push(circle, rect);
    set.attr({
        fill: 'red',
        stroke: 0
    });
    //the box we're going to draw to track the selection
    var box;
    //set that will receive the selected items
    var selections = paper.set();
    

    Now, we add a drag event — similar to the mouseover events but with three functions (see documentation), and draw a box to track the selection space:

    //DRAG FUNCTIONS
    //when mouse goes down over background, start drawing selection box
    function dragstart (x, y, event) {
        box = paper.rect(x, y, 0, 0).attr("stroke", "#9999FF");    
    }
    
    // When mouse moves during drag, adjust box.
    // If the drag is to the left or above original point,
    // you have to translate the whole box and invert the dx 
    // or dy values since .rect() doesn't take negative width or height
    function dragmove (dx, dy, x, y, event) {
        var xoffset = 0,
            yoffset = 0;
        if (dx < 0) {
            xoffset = dx;
            dx = -1 * dx;
        }
        if (dy < 0) {
            yoffset = dy;
            dy = -1 * dy;
        }
        box.transform("T" + xoffset + "," + yoffset);
        box.attr("width", dx);    
        box.attr("height", dy);    
    }
    
    function dragend (event) {
        //get the bounds of the selections
        var bounds = box.getBBox();
        box.remove();
        reset();
        console.log(bounds);
        for (var c in set.items) {
            // Here, we want to get the x,y vales of each object
            // regardless of what sort of shape it is.
            // But rect uses rx and ry, circle uses cx and cy, etc
            // So we'll see if the bounding boxes intercept instead
    
            var mybounds = set[c].getBBox();
            //do bounding boxes overlap?
            //is one of this object's x extremes between the selection's xe xtremes?
            if (mybounds.x >= bounds.x && mybounds.x <= bounds.x2 || mybounds.x2 >= bounds.x && mybounds.x2 <= bounds.x2) {
                //same for y
                if (mybounds.y >= bounds.y && mybounds.y <= bounds.y2 || mybounds.y2 >= bounds.y && mybounds.y2 <= bounds.y2) {
                    selections.push(set[c]);       
                }
            }
            selections.attr("opacity", 0.5);
        }
    }
    
    function reset () {
        //empty selections and reset opacity;
        selections = paper.set();
        set.attr("opacity", 1);    
    }
    
    mat.drag(dragmove, dragstart, dragend);
    mat.click(function(e) {
       reset(); 
    });
    

    Just like that, you have a new set (selections) that contains every object that was selected by the mouse drag. You can then apply your mouseover events from the original to that set.

    Note that this will select circle objects if you nick the corner of their bounding box with your selection box, even if it doesn’t overlap with the area of the circle. You could make a special case for circles if this is a problem.

    jsFiddle

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

Sidebar

Related Questions

I have recently started programming in WPF and bumped into the following problem. I
Now i know to use the method of float.Parse but have bumped into a
I bumped into the following problem: I'm writing a Linux bash script which does
Bumped into another templates problem: The problem: I want to partially specialize a container-class
I've bumped into a problem while working at a project. I want to crawl
I bumped into this problem when I created a chart that need a axis
I'm working on a quiz for school but I've bumped into a problem. I
Recently I bumped into a problem where my OpenGL program would not render textures
During usage of NHibernate ... One strange problem that I have bumped into was
I bumped into the following last night and I'm still at a loss as

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.