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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:53:48+00:00 2026-06-13T19:53:48+00:00

How can I make a palette behaviour (elements being dragged and dropped from a

  • 0

How can I make a palette behaviour (elements being dragged and dropped from a ‘palette’ to a ‘canvas’) in raphaelJS?

  • 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-13T19:53:49+00:00Added an answer on June 13, 2026 at 7:53 pm

    You’ll have to add to every palette element this startFunction:

    //DragFunctions is the object that has all the 3 d&d methods, clearer in the complete file
    paletteStart: function () {
        // keep the relative coords at the start of the drag
        this.ox = 0;
        this.oy = 0;
    
        // as we are dragging the palette element, we clone it to leave one in his place.
        var newPaletteObj = this.clone();
    
        //we give the new palette element the behaviour of a palette element
        DragFunctions.addDragAndDropCapabilityToPaletteOption(newPaletteObj);
    
        //nice animation
        this.animate({
            "opacity": 0.5
        }, 500);
    }
    

    Now we need the function while the element is being dragged:

    move: function (dx, dy) {
        // calculate translation coords
        var new_x = dx - this.ox;
        var new_y = dy - this.oy;
    
        // transforming coordinates
        this.transform('...T' + new_x + ',' + new_y);
    
        // save the new values for future drags
        this.ox = dx;
        this.oy = dy;
    }
    

    And finally, the function executed at finish dropping:

    paletteUp: function () {
        if (!DragFunctions.isInsideCanvas(this)) {
            this.remove();
            //notify the user as you want!
        } else {
            //Giving the new D&D behaviour
            this.undrag();
            //give the element the new d&d functionality!
            this.animate({
                "opacity": 1
            }, 500);
        }
    }
    

    2 things to comment here, when the element is dropped, you will have to remove the palette behaviour and give it another one (a plain d&d functionality), if not, it will continue cloning elements all around.
    Here I give you some nice behaviour to give them:

    start: function () {
        // keep the relative coords at the start of the drag
        this.ox = 0;
        this.oy = 0;
        // animate attributes to a "being dragged" state
        this.animate({
            "opacity": 0.5
        }, 500);
    },
    //same move function
    up: function () {
        if (!DragFunctions.isInsideCanvas(this)) {
            this.animate({
                transform: '...T' + (-this.ox) + ',' + (-this.oy)
            }, 1000, "bounce");
        }
        this.animate({
            "opacity": 1
        }, 500);
    },
    
    //and the method that gives the behaviour
    addDragAndDropCapabilityToSet: function (compSet) {
        compSet.drag(this.move, this.start, this.up, compSet, compSet, compSet);
    }
    

    And as you may also see, we have a validator that sees if the element is inside the canvas, it is a very useful function, here:

    isInsideCanvas: function (obj) {
        var canvasBBox = //get your 'canvas'
        var objectBBox = obj.getBBox();
        var objectPartiallyOutside = !Raphael.isPointInsideBBox(canvasBBox, objectBBox.x, objectBBox.y) || !Raphael.isPointInsideBBox(canvasBBox, objectBBox.x, objectBBox.y2) || !Raphael.isPointInsideBBox(canvasBBox, objectBBox.x2, objectBBox.y) || !Raphael.isPointInsideBBox(canvasBBox, objectBBox.x2, objectBBox.y2);
        return !(objectPartiallyOutside);
    } Finally,
    the place to call to give the element all this behaviour:
    
    //this works for elements and sets
    addDragAndDropCapabilityToPaletteOption: function (compSet) {
        compSet.drag(this.move, this.paletteStart, this.paletteUp, compSet, compSet, compSet);
    }
    

    A demo of this is in a website I created to play with raphael, called comoformamos.com
    The hole code is in a github gist or hosted on github so if you want to get a little deeper in the code feel free to do it.

    Explained more beautifully at this blog entry: devhike, I’m the author.

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

Sidebar

Related Questions

I can make a variable's name from two variables' value: $a = 'tea'; $b
I can make a obj to use the canvas to draw like this: MyObj.myDiv
I can make a sequence of numbers like this: s = seq(from=1, to=10, by=1)
We can make all elements of class 'button' as JQuery UI Buttons using the
I can make my program write a .dat file with an array of Node
How can make a link within a facebox window that redirects it to another
I can make a single row IKImageBrowserView by setting the [imageBrowser setContentResizingMask:NSViewWidthSizable]; but in
I can make a log in for easily, so that's not the problem. What
I can make batch or vbs file on windows and run. this can automate
Can someone tell me how I can make a table be 100% height in

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.