Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single API for animating colors and other properties #6191

Merged
merged 5 commits into from
Mar 4, 2020
Merged

Single API for animating colors and other properties #6191

merged 5 commits into from
Mar 4, 2020

Conversation

yassilah
Copy link
Contributor

@yassilah yassilah commented Mar 2, 2020

Currently, I cannot animate colors using the animate method of my objects as I would do for other properties. This PR allows fabric to check whether the current animated property should be considered a "colorProperty" and call the animating method accordingly. It adds the "colorProperties" property to the fabric.Object prototype to allow for extension.

Tests are included.
Here is a working demo: https://jsfiddle.net/pzb6yno5/

@asturur
Copy link
Member

asturur commented Mar 2, 2020

Hi @yassipad thanks for the PR. Please remove the built files from your build before committing.

git checkout master dist will make the trick.

@@ -174,6 +174,8 @@ fabric.util.object.extend(fabric.Object.prototype, /** @lends fabric.Object.prot
propPair = property.split('.');
}

var propIsText = _this.colorProperties.includes(property) || (propPair && _this.colorProperties.includes(propPair[propPair.length - 1]));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we still support full es5 browsers, and includes is not part of it, you have to use indexOf.

Also propPair is supported only as a 2 word key, like shadow.color please use propPair[1] instead of length - 1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure thing, though actually this is also another issue i've faed when updating points-based shapes (e.g. Polyline) as it requires at least a three-level props tree. Would it be something you'd consider useful to allow for more than just two-level? i made a fix for my project that works well, so i can always make another PR

@@ -174,6 +174,8 @@ fabric.util.object.extend(fabric.Object.prototype, /** @lends fabric.Object.prot
propPair = property.split('.');
}

var propIsText = _this.colorProperties.includes(property) || (propPair && _this.colorProperties.includes(propPair[propPair.length - 1]));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

propIsText can be called propIsColor and can be reused later.

});
};

if (_this.colorProperties.includes(property) || propPair && _this.colorProperties.includes(propPair[propPair.length - 1])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the same check as above, and we can reuse the same boolean we already calculated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, i'll change that

*/
colorProperties: (
'fill stroke backgroundColor borderColor'
).split(' '),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is borderColor something we should consider animatable? if yes, why not also cornerColor, cornerStrokeColor and all the rest?
I would leave borderColor out for now. But of course if you think is really important, argument why.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i personally wouldn't use either, it was just for testing so I think we can leave it out for now

if (index === properties.length - 1) {
done();
}
}, 1000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use the animation callback instead of a timeout?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, i was just copying the other tests but i can do that! you mean in a onComplete callback?

@yassilah
Copy link
Contributor Author

yassilah commented Mar 2, 2020

Hey, thanks for your quick feedback, i'll do the necessary changes :)

*/
colorProperties: (
'fill stroke backgroundColor'
).split(' '),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i want to merge this as it is, but i think i will come back and move colorProperties to animation util level maybe.
Too busy to think of it now and not so important.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, the reason I put it on the object class is because I thought it might be easier to create a new shape class with a custom property name that involved coloring but as you like :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i know but so many options.

@asturur
Copy link
Member

asturur commented Mar 4, 2020

thanks!

@asturur asturur merged commit 6ff7229 into fabricjs:master Mar 4, 2020
asturur pushed a commit that referenced this pull request Mar 9, 2020
@asturur asturur mentioned this pull request Mar 12, 2020
asturur added a commit that referenced this pull request Mar 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants