-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Make Chart.Animation/animations/Tooltip
importable
#5382
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,172 +1,43 @@ | ||
/* global window: false */ | ||
'use strict'; | ||
|
||
var defaults = require('./core.defaults'); | ||
var Element = require('./core.element'); | ||
var helpers = require('../helpers/index'); | ||
|
||
defaults._set('global', { | ||
animation: { | ||
duration: 1000, | ||
easing: 'easeOutQuart', | ||
onProgress: helpers.noop, | ||
onComplete: helpers.noop | ||
} | ||
}); | ||
|
||
module.exports = function(Chart) { | ||
|
||
Chart.Animation = Element.extend({ | ||
chart: null, // the animation associated chart instance | ||
currentStep: 0, // the current animation step | ||
numSteps: 60, // default number of steps | ||
easing: '', // the easing to use for this animation | ||
render: null, // render function used by the animation service | ||
|
||
onAnimationProgress: null, // user specified callback to fire on each step of the animation | ||
onAnimationComplete: null, // user specified callback to fire when the animation finishes | ||
}); | ||
|
||
Chart.animationService = { | ||
frameDuration: 17, | ||
animations: [], | ||
dropFrames: 0, | ||
request: null, | ||
|
||
/** | ||
* @param {Chart} chart - The chart to animate. | ||
* @param {Chart.Animation} animation - The animation that we will animate. | ||
* @param {Number} duration - The animation duration in ms. | ||
* @param {Boolean} lazy - if true, the chart is not marked as animating to enable more responsive interactions | ||
*/ | ||
addAnimation: function(chart, animation, duration, lazy) { | ||
var animations = this.animations; | ||
var i, ilen; | ||
|
||
animation.chart = chart; | ||
|
||
if (!lazy) { | ||
chart.animating = true; | ||
} | ||
|
||
for (i = 0, ilen = animations.length; i < ilen; ++i) { | ||
if (animations[i].chart === chart) { | ||
animations[i] = animation; | ||
return; | ||
} | ||
} | ||
|
||
animations.push(animation); | ||
|
||
// If there are no animations queued, manually kickstart a digest, for lack of a better word | ||
if (animations.length === 1) { | ||
this.requestAnimationFrame(); | ||
} | ||
}, | ||
|
||
cancelAnimation: function(chart) { | ||
var index = helpers.findIndex(this.animations, function(animation) { | ||
return animation.chart === chart; | ||
}); | ||
|
||
if (index !== -1) { | ||
this.animations.splice(index, 1); | ||
chart.animating = false; | ||
} | ||
}, | ||
|
||
requestAnimationFrame: function() { | ||
var me = this; | ||
if (me.request === null) { | ||
// Skip animation frame requests until the active one is executed. | ||
// This can happen when processing mouse events, e.g. 'mousemove' | ||
// and 'mouseout' events will trigger multiple renders. | ||
me.request = helpers.requestAnimFrame.call(window, function() { | ||
me.request = null; | ||
me.startDigest(); | ||
}); | ||
} | ||
}, | ||
|
||
/** | ||
* @private | ||
*/ | ||
startDigest: function() { | ||
var me = this; | ||
var startTime = Date.now(); | ||
var framesToDrop = 0; | ||
var exports = module.exports = Element.extend({ | ||
chart: null, // the animation associated chart instance | ||
currentStep: 0, // the current animation step | ||
numSteps: 60, // default number of steps | ||
easing: '', // the easing to use for this animation | ||
render: null, // render function used by the animation service | ||
|
||
if (me.dropFrames > 1) { | ||
framesToDrop = Math.floor(me.dropFrames); | ||
me.dropFrames = me.dropFrames % 1; | ||
} | ||
|
||
me.advance(1 + framesToDrop); | ||
|
||
var endTime = Date.now(); | ||
|
||
me.dropFrames += (endTime - startTime) / me.frameDuration; | ||
|
||
// Do we have more stuff to animate? | ||
if (me.animations.length > 0) { | ||
me.requestAnimationFrame(); | ||
} | ||
}, | ||
|
||
/** | ||
* @private | ||
*/ | ||
advance: function(count) { | ||
var animations = this.animations; | ||
var animation, chart; | ||
var i = 0; | ||
|
||
while (i < animations.length) { | ||
animation = animations[i]; | ||
chart = animation.chart; | ||
|
||
animation.currentStep = (animation.currentStep || 0) + count; | ||
animation.currentStep = Math.min(animation.currentStep, animation.numSteps); | ||
|
||
helpers.callback(animation.render, [chart, animation], chart); | ||
helpers.callback(animation.onAnimationProgress, [animation], chart); | ||
|
||
if (animation.currentStep >= animation.numSteps) { | ||
helpers.callback(animation.onAnimationComplete, [animation], chart); | ||
chart.animating = false; | ||
animations.splice(i, 1); | ||
} else { | ||
++i; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* Provided for backward compatibility, use Chart.Animation instead | ||
* @prop Chart.Animation#animationObject | ||
* @deprecated since version 2.6.0 | ||
* @todo remove at version 3 | ||
*/ | ||
Object.defineProperty(Chart.Animation.prototype, 'animationObject', { | ||
get: function() { | ||
return this; | ||
} | ||
}); | ||
onAnimationProgress: null, // user specified callback to fire on each step of the animation | ||
onAnimationComplete: null, // user specified callback to fire when the animation finishes | ||
}); | ||
|
||
/** | ||
* Provided for backward compatibility, use Chart.Animation#chart instead | ||
* @prop Chart.Animation#chartInstance | ||
* @deprecated since version 2.6.0 | ||
* @todo remove at version 3 | ||
*/ | ||
Object.defineProperty(Chart.Animation.prototype, 'chartInstance', { | ||
get: function() { | ||
return this.chart; | ||
}, | ||
set: function(value) { | ||
this.chart = value; | ||
} | ||
}); | ||
// DEPRECATIONS | ||
|
||
/** | ||
* Provided for backward compatibility, use Chart.Animation instead | ||
* @prop Chart.Animation#animationObject | ||
* @deprecated since version 2.6.0 | ||
* @todo remove at version 3 | ||
*/ | ||
Object.defineProperty(exports.prototype, 'animationObject', { | ||
get: function() { | ||
return this; | ||
} | ||
}); | ||
|
||
}; | ||
/** | ||
* Provided for backward compatibility, use Chart.Animation#chart instead | ||
* @prop Chart.Animation#chartInstance | ||
* @deprecated since version 2.6.0 | ||
* @todo remove at version 3 | ||
*/ | ||
Object.defineProperty(exports.prototype, 'chartInstance', { | ||
get: function() { | ||
return this.chart; | ||
}, | ||
set: function(value) { | ||
this.chart = value; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* global window: false */ | ||
'use strict'; | ||
|
||
var defaults = require('./core.defaults'); | ||
var helpers = require('../helpers/index'); | ||
|
||
defaults._set('global', { | ||
animation: { | ||
duration: 1000, | ||
easing: 'easeOutQuart', | ||
onProgress: helpers.noop, | ||
onComplete: helpers.noop | ||
} | ||
}); | ||
|
||
module.exports = { | ||
frameDuration: 17, | ||
animations: [], | ||
dropFrames: 0, | ||
request: null, | ||
|
||
/** | ||
* @param {Chart} chart - The chart to animate. | ||
* @param {Chart.Animation} animation - The animation that we will animate. | ||
* @param {Number} duration - The animation duration in ms. | ||
* @param {Boolean} lazy - if true, the chart is not marked as animating to enable more responsive interactions | ||
*/ | ||
addAnimation: function(chart, animation, duration, lazy) { | ||
var animations = this.animations; | ||
var i, ilen; | ||
|
||
animation.chart = chart; | ||
|
||
if (!lazy) { | ||
chart.animating = true; | ||
} | ||
|
||
for (i = 0, ilen = animations.length; i < ilen; ++i) { | ||
if (animations[i].chart === chart) { | ||
animations[i] = animation; | ||
return; | ||
} | ||
} | ||
|
||
animations.push(animation); | ||
|
||
// If there are no animations queued, manually kickstart a digest, for lack of a better word | ||
if (animations.length === 1) { | ||
this.requestAnimationFrame(); | ||
} | ||
}, | ||
|
||
cancelAnimation: function(chart) { | ||
var index = helpers.findIndex(this.animations, function(animation) { | ||
return animation.chart === chart; | ||
}); | ||
|
||
if (index !== -1) { | ||
this.animations.splice(index, 1); | ||
chart.animating = false; | ||
} | ||
}, | ||
|
||
requestAnimationFrame: function() { | ||
var me = this; | ||
if (me.request === null) { | ||
// Skip animation frame requests until the active one is executed. | ||
// This can happen when processing mouse events, e.g. 'mousemove' | ||
// and 'mouseout' events will trigger multiple renders. | ||
me.request = helpers.requestAnimFrame.call(window, function() { | ||
me.request = null; | ||
me.startDigest(); | ||
}); | ||
} | ||
}, | ||
|
||
/** | ||
* @private | ||
*/ | ||
startDigest: function() { | ||
var me = this; | ||
var startTime = Date.now(); | ||
var framesToDrop = 0; | ||
|
||
if (me.dropFrames > 1) { | ||
framesToDrop = Math.floor(me.dropFrames); | ||
me.dropFrames = me.dropFrames % 1; | ||
} | ||
|
||
me.advance(1 + framesToDrop); | ||
|
||
var endTime = Date.now(); | ||
|
||
me.dropFrames += (endTime - startTime) / me.frameDuration; | ||
|
||
// Do we have more stuff to animate? | ||
if (me.animations.length > 0) { | ||
me.requestAnimationFrame(); | ||
} | ||
}, | ||
|
||
/** | ||
* @private | ||
*/ | ||
advance: function(count) { | ||
var animations = this.animations; | ||
var animation, chart; | ||
var i = 0; | ||
|
||
while (i < animations.length) { | ||
animation = animations[i]; | ||
chart = animation.chart; | ||
|
||
animation.currentStep = (animation.currentStep || 0) + count; | ||
animation.currentStep = Math.min(animation.currentStep, animation.numSteps); | ||
|
||
helpers.callback(animation.render, [chart, animation], chart); | ||
helpers.callback(animation.onAnimationProgress, [animation], chart); | ||
|
||
if (animation.currentStep >= animation.numSteps) { | ||
helpers.callback(animation.onAnimationComplete, [animation], chart); | ||
chart.animating = false; | ||
animations.splice(i, 1); | ||
} else { | ||
++i; | ||
} | ||
} | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@loicbourgois I had to disable this rule, else we can't run
gulp lint
on Windows with Git configured to AutoCrlf.