How Can I Check If Something Is A Raphael Object?
Solution 1:
To elaborate a little and add some more relevant info (it took me a little while to figure out the accepted answer, and I'm clearly not alone looking at the other answers, also, the accepted answer only works for one type of Raphael object: it solves the original question, this is a more complete resource).
Detecting Raphael elements
Using x.constructor.prototype == Raphael.el, you're taking x, the variable that might be a Raphael element (circle, path etc - not a Raphael set or paper object) and comparing the prototype of the function that constructed it with the prototype for Raphael elements in Raphael itself (Raphael is a function object, el is a defined property of it).
This works, but it also won't find raphael objects based on different prototypes to Raphael.el, like sets and paper objects:
Detecting Raphael sets
If you wanted to test if something was a Raphael set, the set prototype is in Raphael.st so you could test if a variable is a Raphael set using:
someSet.constructor.prototype == Raphael.st
Detecting Raphael paper objects
As for the equivalent for sniffing Raphael paper objects, since they are created using Raphael() function, you can use:
paper.constructor.prototype == Raphael.prototype
The above three are basically the same as...
someSet.constructor.prototype == paper.circle().constructor.prototype
...or...
someSet.constructor.prototype == paper.set().constructor.prototype
...or...
someSet.constructor.prototype == Raphael().constructor.prototype
...but without actually running those functions, so avoiding wasted processing (and avoiding Raphael() complaining that it hasn't been passed an ID).
Detecting sub-types of object (e.g. rectangle, circle...)
None of the above works for subtypes of Raphael elements - e.g. if you compare a circle with R.rect().constructor.prototype, it returns true.
This is because both circles and rectangles are made using the element prototype defined in Raphael.el. For these, however, Raphael makes it easy:
someRectangle.type == "rect"
someCircle.type == "circle"
...etc..
Solution 2:
Pablo posted an answer that was not quite correct but gave me inspiration towards finding a correct solution:
x.constructor.prototype == Raphael.el
Solution 3:
Can't you use the constructor property and check against the function that created the object (I'm assuming it's called Raphael but I haven't used the lib).
Edit
Checked the lib site, you actually do it that way:
obj.constructor === Raphael //true
Solution 4:
I wasnt able to use the mentioned answer. But what worked for me was to compare explicitly with string "Raphaël’s object".
Eg:
var textName = paper.getElementByPoint(e.pageX, e.pageY);
if (textName== "Raphaël’s object")
{
...
}
Post a Comment for "How Can I Check If Something Is A Raphael Object?"