I ran across this snippet of code and have “used” it as a reference for developing my own specific toggle function.
Raphael.js – if / else statement not toggling on click
I would like to apply an animation to the stroke-width per say when it is increased on click. I can’t seem to figure out how to add this animation in alongside the toggle function.
I figured this would be applied around the variables StrON and StrOFF so I have tried things such as:
var strOff = function() {
this.animate({ 'stroke-width': '1' }, 100);
};
var strOn = function() {
this.animate({ 'stroke-width': '5' }, 100);
};
and even just:
var strOff =
this.animate({ 'stroke-width': '1' }, 100);
var strOn =
this.animate({ 'stroke-width': '5' }, 100);
Sorry about the lazy syntax If I missed anything on the two examples of what I’ve tried. Thanks for any help.
Neither of these will work because strOn and strOff are not the right data type — they must be an object containing attributes for the selected and deselected states of a given rectangle. This represents a fundamental misunderstanding of what animate does: it is essentially an asynchronous version of
attr.You could solve your problem by simply restoring strOn and strOff to their original state and then calling this in the click handler for a given rectangle:
This still leaves you with a complexity issue. If you want to add a fourth or fifth rectangle, you will quickly drown in conditional logic. This sort of state information should, in my opinion, almost never be implemented like this. Instead, I recommend doing this:
Use a single, generic click handler.
This will iterate through all elements in the paper — if one of them is marked as “active,” it will be animated back into its inactive state.
Use
datato keep track of the selected rectangle:Using this approach, there’s no need to keep track of individual rectangles — only whether or not a given rectangle is selected or not.
Here’s an example supporting many rectangles.