Changeset 143598 in webkit


Ignore:
Timestamp:
Feb 21, 2013 6:57:41 AM (11 years ago)
Author:
keishi@webkit.org
Message:

Add animation class for new calendar picker
https://bugs.webkit.org/show_bug.cgi?id=110132

Reviewed by Kent Tamura.

Adding animation related classes as part of the calendar picker
redesign(Bug 109439).

No new tests. Code is not used yet.

  • Resources/pagepopups/calendarPicker.js:

(AnimationTimingFunction.Linear): Parameter t should be a number between 0 and 1.
(AnimationTimingFunction.EaseInOut): Ditto.
(AnimationManager): All animators are managed by this class so we
can dispatch "animationFrameWillFinish" event after all the updates.
(AnimationManager.prototype._startAnimation):
(AnimationManager.prototype._stopAnimation):
(AnimationManager.prototype.add): Adds an animator to the list of running animators.
(AnimationManager.prototype.remove): Removes an animator.
(AnimationManager.prototype._animationFrameCallback): Callback for requestAnimationFrame.
(AnimationManager.prototype._needsAnimationFrame): Returns true if we should request the next animation frame.
(AnimationManager.prototype.on): If we add a callback, request animation frame.
(AnimationManager.prototype.removeListener):
(Animator): Animates between the from value and to value.
(Animator.prototype.setFrom): Sets the from value.
(Animator.prototype.setTo): Sets the to value.
(Animator.prototype.start):
(Animator.prototype.stop):
(Animator.prototype.onAnimationFrame): Called by AnimationManager.

Location:
trunk/Source/WebCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r143597 r143598  
     12013-02-21  Keishi Hattori  <keishi@webkit.org>
     2
     3        Add animation class for new calendar picker
     4        https://bugs.webkit.org/show_bug.cgi?id=110132
     5
     6        Reviewed by Kent Tamura.
     7
     8        Adding animation related classes as part of the calendar picker
     9        redesign(Bug 109439).
     10
     11        No new tests. Code is not used yet.
     12
     13        * Resources/pagepopups/calendarPicker.js:
     14        (AnimationTimingFunction.Linear): Parameter t should be a number between 0 and 1.
     15        (AnimationTimingFunction.EaseInOut): Ditto.
     16        (AnimationManager): All animators are managed by this class so we
     17        can dispatch "animationFrameWillFinish" event after all the updates.
     18        (AnimationManager.prototype._startAnimation):
     19        (AnimationManager.prototype._stopAnimation):
     20        (AnimationManager.prototype.add): Adds an animator to the list of running animators.
     21        (AnimationManager.prototype.remove): Removes an animator.
     22        (AnimationManager.prototype._animationFrameCallback): Callback for requestAnimationFrame.
     23        (AnimationManager.prototype._needsAnimationFrame): Returns true if we should request the next animation frame.
     24        (AnimationManager.prototype.on): If we add a callback, request animation frame.
     25        (AnimationManager.prototype.removeListener):
     26        (Animator): Animates between the from value and to value.
     27        (Animator.prototype.setFrom): Sets the from value.
     28        (Animator.prototype.setTo): Sets the to value.
     29        (Animator.prototype.start):
     30        (Animator.prototype.stop):
     31        (Animator.prototype.onAnimationFrame): Called by AnimationManager.
     32
    1332013-02-21  Andrey Adaikin  <aandrey@chromium.org>
    234
  • trunk/Source/WebCore/Resources/pagepopups/calendarPicker.js

    r143576 r143598  
    707707        callbacksForType[i].apply(this, Array.prototype.slice.call(arguments, 1));
    708708    }
     709};
     710
     711// Parameter t should be a number between 0 and 1.
     712var AnimationTimingFunction = {
     713    Linear: function(t){
     714        return t;
     715    },
     716    EaseInOut: function(t){
     717        t *= 2;
     718        if (t < 1)
     719            return Math.pow(t, 3) / 2;
     720        t -= 2;
     721        return Math.pow(t, 3) / 2 + 1;
     722    }
     723};
     724
     725/**
     726 * @constructor
     727 * @extends EventEmitter
     728 */
     729function AnimationManager() {
     730    EventEmitter.call(this);
     731
     732    this._isRunning = false;
     733    this._runningAnimatorCount = 0;
     734    this._runningAnimators = {};
     735    this._animationFrameCallbackBound = this._animationFrameCallback.bind(this);
     736}
     737
     738AnimationManager.prototype = Object.create(EventEmitter.prototype);
     739
     740AnimationManager.EventTypeAnimationFrameWillFinish = "animationFrameWillFinish";
     741
     742AnimationManager.prototype._startAnimation = function() {
     743    if (this._isRunning)
     744        return;
     745    this._isRunning = true;
     746    window.webkitRequestAnimationFrame(this._animationFrameCallbackBound);
     747};
     748
     749AnimationManager.prototype._stopAnimation = function() {
     750    if (!this._isRunning)
     751        return;
     752    this._isRunning = false;
     753};
     754
     755/**
     756 * @param {!Animator} animator
     757 */
     758AnimationManager.prototype.add = function(animator) {
     759    if (this._runningAnimators[animator.id])
     760        return;
     761    this._runningAnimators[animator.id] = animator;
     762    this._runningAnimatorCount++;
     763    if (this._needsTimer())
     764        this._startAnimation();
     765};
     766
     767/**
     768 * @param {!Animator} animator
     769 */
     770AnimationManager.prototype.remove = function(animator) {
     771    if (!this._runningAnimators[animator.id])
     772        return;
     773    delete this._runningAnimators[animator.id];
     774    this._runningAnimatorCount--;
     775    if (!this._needsTimer())
     776        this._stopAnimation();
     777};
     778
     779AnimationManager.prototype._animationFrameCallback = function(now) {
     780    if (this._runningAnimatorCount > 0) {
     781        for (var id in this._runningAnimators) {
     782            this._runningAnimators[id].onAnimationFrame(now);
     783        }
     784    }
     785    this.dispatchEvent(AnimationManager.EventTypeAnimationFrameWillFinish);
     786    if (this._isRunning)
     787        window.webkitRequestAnimationFrame(this._animationFrameCallbackBound);
     788};
     789
     790/**
     791 * @return {!boolean}
     792 */
     793AnimationManager.prototype._needsTimer = function() {
     794    return this._runningAnimatorCount > 0 || this.hasListener(AnimationManager.EventTypeAnimationFrameWillFinish);
     795};
     796
     797/**
     798 * @param {!string} type
     799 * @param {!Function} callback
     800 * @override
     801 */
     802AnimationManager.prototype.on = function(type, callback) {
     803    EventEmitter.prototype.on.call(this, type, callback);
     804    if (this._needsTimer())
     805        this._startAnimation();
     806};
     807
     808/**
     809 * @param {!string} type
     810 * @param {!Function} callback
     811 * @override
     812 */
     813AnimationManager.prototype.removeListener = function(type, callback) {
     814    EventEmitter.prototype.removeListener.call(this, type, callback);
     815    if (!this._needsTimer())
     816        this._stopAnimation();
     817};
     818
     819AnimationManager.shared = new AnimationManager();
     820
     821/**
     822 * @constructor
     823 * @extends EventEmitter
     824 */
     825function Animator() {
     826    EventEmitter.call(this);
     827
     828    this.id = Animator._lastId++;
     829    this._from = 0;
     830    this._to = 0;
     831    this._delta = 0;
     832    this.duration = 100;
     833    this.step = null;
     834    this._lastStepTime = null;
     835    this.progress = 0.0;
     836    this.timingFunction = AnimationTimingFunction.Linear;
     837}
     838
     839Animator.prototype = Object.create(EventEmitter.prototype);
     840
     841Animator._lastId = 0;
     842
     843Animator.EventTypeDidAnimationStop = "didAnimationStop";
     844
     845/**
     846 * @param {!number} value
     847 */
     848Animator.prototype.setFrom = function(value) {
     849    this._from = value;
     850    this._delta = this._to - this._from;
     851};
     852
     853/**
     854 * @param {!number} value
     855 */
     856Animator.prototype.setTo = function(value) {
     857    this._to = value;
     858    this._delta = this._to - this._from;
     859};
     860
     861Animator.prototype.start = function() {
     862    this._lastStepTime = Date.now();
     863    this.progress = 0.0;
     864    this._isRunning = true;
     865    this.currentValue = this._from;
     866    AnimationManager.shared.add(this);
     867};
     868
     869Animator.prototype.stop = function() {
     870    if (!this._isRunning)
     871        return;
     872    this._isRunning = false;
     873    this.currentValue = this._to;
     874    this.step(this);
     875    AnimationManager.shared.remove(this);
     876    this.dispatchEvent(Animator.EventTypeDidAnimationStop, this);
     877};
     878
     879/**
     880 * @param {!number} now
     881 */
     882Animator.prototype.onAnimationFrame = function(now) {
     883    this.progress += (now - this._lastStepTime) / this.duration;
     884    if (this.progress >= 1.0) {
     885        this.progress = 1.0;
     886        this.stop();
     887        return;
     888    }
     889    this.currentValue = this.timingFunction(this.progress) * this._delta + this._from;
     890    this.step(this);
     891    this._lastStepTime = now;
    709892};
    710893
Note: See TracChangeset for help on using the changeset viewer.