Skip to content

Emit plotly_animatingframe event on animated frames #953

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

Merged
merged 5 commits into from
Sep 20, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2219,7 +2219,7 @@ Plotly.animate = function(gd, frameOrGroupNameOrFrameList, animationOpts) {

var nextFrame = {
frame: computedFrame,
name: frameList[i].name,
name: frameList[i].name || computedFrame.name,
Copy link
Contributor

Choose a reason for hiding this comment

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

why do need a fallback here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's not a bad point. I can move that elsewhere (being careful not to mutate the user input itself) for this particular case. (It's the case when a plain object is provided instead of the name of a frame.)

Copy link
Contributor

Choose a reason for hiding this comment

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

No need to move it. I was just curious.

How hard would it to be to add a test for that case? If not so hard, then let's add one,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Moved. I added an extra container for frames internally so that I'm not cramming any extra properties together that could conceivably conflict.

frameOpts: frameOpts,
transitionOpts: transitionOpts,
};
Expand Down Expand Up @@ -2274,6 +2274,8 @@ Plotly.animate = function(gd, frameOrGroupNameOrFrameList, animationOpts) {
var newFrame = trans._currentFrame = trans._frameQueue.shift();

if(newFrame) {
gd.emit('plotly_animatingframe', newFrame.name);
Copy link
Contributor

Choose a reason for hiding this comment

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

Have you thought about adding more fields to the event data?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah. I have nothing against that since there's not another way to query frames by name short of looking it up yourself in the input data. What other fields do you think are relevant? I see:

transitionOpts, frameOpts, frame (the data itself), name.

Maybe all of them? They're just references, so not really any extra cost. Could pass as an object.

Copy link
Contributor

Choose a reason for hiding this comment

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

Making the event data an object seems like the way to go in case we want to add more stuff in their at some point.


trans._lastFrameAt = Date.now();
trans._timeToNext = newFrame.frameOpts.duration;

Expand Down
17 changes: 17 additions & 0 deletions test/jasmine/tests/animate_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,23 @@ describe('Test animate API', function() {
});
});

describe('frame events', function() {
it('emits an event when a frame is transitioned to', function(done) {
var frames = [];
gd.on('plotly_animatingframe', function(name) {
frames.push(name);
});

Plotly.animate(gd, ['frame0', 'frame1', {name: 'test'}, {data: []}], {
transition: {duration: 1},
frame: {duration: 1}
}).then(function() {
expect(frames).toEqual(['frame0', 'frame1', 'test', undefined]);
}).catch(fail).then(done);

});
});

describe('frame vs. transition timing', function() {
it('limits the transition duration to <= frame duration', function(done) {
Plotly.animate(gd, ['frame0'], {
Expand Down