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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:39:11+00:00 2026-05-30T11:39:11+00:00

Is it possible to define a custom attribute for a raphael element? e.g. r.circle(25,50,10).attr({fill:’#b71e16′,

  • 0

Is it possible to define a custom attribute for a raphael element?

e.g.

r.circle(25,50,10).attr({fill:'#b71e16', stroke:'#71140f', 'my_custom_attribute':'a value'});

Reason I need this is I want to do some quite complex animation on a whole set of elements and I want somewhere to store the original y coordinate for each one.

  • 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-05-30T11:39:12+00:00Added an answer on May 30, 2026 at 11:39 am

    Is the custom attribute you want:

    1. A simple store for arbitrary data, to be recorded and retrieved?
    2. An attribute where a custom action needs to be taken when it is changed (like the attributes controlled with Raphael’s .attr() and .animate() )?
    3. Something you want to force into the attributes of the output SVG / VML markup on the page / DOM? (not normally recommended, but sometimes needed)

    1. Custom data storage and retrieval

    I’m 99% sure that the official, intended way to store arbitrary data in Raphael is to use the .data() function, e.g.

    var circle = r.circle(25,50,10).attr({fill:'#b71e16', stroke:'#71140f'});
    // set it
    circle.data('custom-attribute', 'value');
    
    // get it
    data = circle.data('custom-attribute');
    alert(data);
    

    Note that as of Raphael 2.1 this works for elements, not sets. When I need to assign data to a set I tend to set it with a for loop and get it with someSet[0].data() – a bit of a cludge, but it works.

    Annoyingly the documentation for .data doesn’t say anything at all about what it is for (at time of writing)… but .data() in jQuery, data-* in HTML5, etc etc all have this purpose, using it like this works, and others on SO talk about it being intended to be used it like this, so I’m pretty confident that this is the intended method for attaching arbitrary data to Raphael objects.


    2. Custom functions triggered by attr() or animate()

    If you need a custom attribute that behaves like Raphael attributes – triggering a function or transformation when changed using attr or animate (kind of like a Raphael hook) – that’s what paper.customAttributes is for. It defines a function that is executed any time the named custom attr is set for any element in that paper object. The return object is applied to the element’s standard attributes.

    The offical docs have some pretty useful examples for this one, here’s an adapted one:

    // A custom attribute with multiple parameters:
    paper.customAttributes.hsb = function (h, s, b) {
        return {fill: "hsb(" + [h, s, b].join(",") + ")"};
    };
    var c = paper.circle(10, 10, 10);
    // If you want to animate a custom attribute, always set it first - null isNaN
    c.attr({hsb: "0.5 .8 1"});
    c.animate({hsb: [1, 0, 0.5]}, 1e3);
    

    Note that this within each customAttribute execution is the Raphael object for which the attr is being set. This means…


    3. Forcing custom attribute into the SVG or VML markup in the browser

    Raphael doesn’t really support this, so only do this if you really, really need to. But if you really do need something in the markup that Raphael just doesn’t support, you can create a rudimentary control for manipulating it using attr and animate by using paper.customAttributes and element.node (note that the documentation for element.node is pretty much just the highly unhelpful “Don’t mess with it” – the reason you shouldn’t mess with it is, it gives you the SVG or VML element directly, which means Raphael doesn’t know about any of the changes you make to it, which may put your Raphael object out of sync with the element it controls, potentially breaking stuff. Unless you’re careful, and use a technique like this…).

    Here’s an example (assuming jQuery is also being used, jQuery isn’t essential but is more convenient) that sets the SVG property dy, which allows you to control line spacing of Raphael text (note – example code not yet tested in VML/IE. will update if it doesn’t work in VML mode):

    Live jsfiddle example

    paper.customAttributes.lineHeight = function( value ) {
        // Sets the SVG dy attribute, which Raphael doesn't control
        var selector = Raphael.svg ? 'tspan' : 'v:textpath';
        var $node = $(this.node);
        var $tspans = $node.find(selector);
        $tspans.each(function(){
            // use $(this).attr in jquery v1.6+, fails for SVG in <= v1.5
            // probably won't work in IE
            this.setAttribute('dy', value );
        });
        // change no default Raphael attributes
        return {};
    }
        // Then to use it...
        var text = paper.text(50,50,"This is \n multi-line \n text");
        // If you want to animate a custom attribute, always set it first - null isNaN
        text.attr({lineHeight: 0});
        text.animate({lineHeight: 100},500);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone knows if it is possible to define/declare on your own custom attribute
is it possible to define a custom UnmanagedType for the MarshalAs attribute class? Specifically
I have noticed that it is possible to define a custom class and then
In VBP its possible to define user defined actions by creating a custom COM
Does anyone know if it possible to define the equivalent of a java custom
Is it possible to define custom configuration in config/environments/*.rb or config/environment.rb file. Is yes,
We all know that it's possible to define custom jQuery selectors that start with
Is it possible to define a custom filter so that NUnit will only run
Is it possible to define custom colours for the MKPinAnnotationView colour rather than the
Is it possible to define a custom format for <xsl:number> ? I have the

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.