API Reference 0.3.24dart_web_toolkit_animationAnimationSchedulerImplTimer

AnimationSchedulerImplTimer class

Implementation using a timer for browsers that do not support animation frames.

class AnimationSchedulerImplTimer extends AnimationSchedulerImpl {

 /**
  * The default time in milliseconds between frames. 60 fps == 16.67 ms.
  */
 static int DEFAULT_FRAME_DELAY = 16;

 /**
  * The minimum delay in milliseconds between frames. The minimum delay is
  * imposed to prevent freezing the UI.
  */
 static int MIN_FRAME_DELAY = 5;

 /**
  * The list of animations that are currently running.
  */
 List<AnimationHandleImplTimer> animationRequests = new List<AnimationHandleImplTimer>();

 /**
  * The timer that updates all animations.
  */
 Timer timer;

 AnimationSchedulerImplTimer() {
   timer = new Timer.get(updateAnimations);
 }

 AnimationHandle requestAnimationFrame(AnimationCallback callback, [dart_html.Element element = null]) {
   // Save the animation frame request.
   AnimationHandleImplTimer requestId = new AnimationHandleImplTimer(callback, this);
   animationRequests.add(requestId);

   // Start the timer if it isn't started.
   if (animationRequests.length == 1) {
     timer.schedule(DEFAULT_FRAME_DELAY);
   }

   // Return the request id.
   return requestId;
 }

 bool isNativelySupported() {
   return true;
 }

 void cancelAnimationFrame(AnimationHandle requestId) {
   // Remove the request from the list.
   int index = animationRequests.indexOf(requestId);
   if (index != -1) {
     animationRequests.removeAt(index);
   }

   // Stop the timer if there are no more requests.
   if (animationRequests.length == 0) {
     timer.cancel();
   }
 }

 /**
  * Iterate over all animations and update them.
  */
 void updateAnimations() {
   // Copy the animation requests to avoid concurrent modifications.
   List<AnimationHandleImplTimer> curAnimations = new List<AnimationHandleImplTimer>.from(animationRequests);

   // Iterate over the animation requests.
   Duration duration = new Duration();
   for (AnimationHandleImplTimer requestId in curAnimations) {
     // Remove the current request.
     int index = animationRequests.indexOf(requestId);
     if (index != -1) {
       animationRequests.removeAt(index);
     }

     // Execute the callback.
     //cduration.inMilliseconds); //getStartMillis());
     requestId.getCallback().execute(new DateTime.now().millisecondsSinceEpoch);
   }

   // Reschedule the timer if there are more animation requests.
   if (animationRequests.length > 0) {
     /*
      * In order to achieve as close to 60fps as possible, we calculate the new
      * delay based on the execution time of this method. The delay will be
      * less than 16ms, assuming this method takes more than 1ms to complete.
      */
     timer.schedule(dart_math.max(MIN_FRAME_DELAY, DEFAULT_FRAME_DELAY - duration.inMilliseconds)); //elapsedMillis()));
   }
 }
}

Extends

AnimationScheduler > AnimationSchedulerImpl > AnimationSchedulerImplTimer

Static Properties

int DEFAULT_FRAME_DELAY #

The default time in milliseconds between frames. 60 fps == 16.67 ms.

static int DEFAULT_FRAME_DELAY = 16

int MIN_FRAME_DELAY #

The minimum delay in milliseconds between frames. The minimum delay is imposed to prevent freezing the UI.

static int MIN_FRAME_DELAY = 5

Constructors

new AnimationSchedulerImplTimer() #

Creates a new Object instance.

Object instances have no meaningful state, and are only useful through their identity. An Object instance is equal to itself only.

docs inherited from Object
AnimationSchedulerImplTimer() {
 timer = new Timer.get(updateAnimations);
}

Properties

List<AnimationHandleImplTimer> animationRequests #

The list of animations that are currently running.

List<AnimationHandleImplTimer> animationRequests = new List<AnimationHandleImplTimer>()

Timer timer #

The timer that updates all animations.

Timer timer

Methods

void cancelAnimationFrame(AnimationHandle requestId) #

void cancelAnimationFrame(AnimationHandle requestId) {
 // Remove the request from the list.
 int index = animationRequests.indexOf(requestId);
 if (index != -1) {
   animationRequests.removeAt(index);
 }

 // Stop the timer if there are no more requests.
 if (animationRequests.length == 0) {
   timer.cancel();
 }
}

bool isNativelySupported() #

Check if the implementation is natively supported.

@return true if natively supported, false if not

docs inherited from AnimationSchedulerImpl
bool isNativelySupported() {
 return true;
}

AnimationHandle requestAnimationFrame(AnimationCallback callback, [Element element = null]) #

Schedule an animation, letting the browser decide when to trigger the next step in the animation.

Using this method instead of a timeout is preferred because the browser is in the best position to decide how frequently to trigger the callback for an animation of the specified element. The browser can balance multiple animations and trigger callbacks at the optimal rate for smooth performance.

@param callback the callback to fire @return a handle to the requested animation frame @param element the element being animated

docs inherited from AnimationScheduler
AnimationHandle requestAnimationFrame(AnimationCallback callback, [dart_html.Element element = null]) {
 // Save the animation frame request.
 AnimationHandleImplTimer requestId = new AnimationHandleImplTimer(callback, this);
 animationRequests.add(requestId);

 // Start the timer if it isn't started.
 if (animationRequests.length == 1) {
   timer.schedule(DEFAULT_FRAME_DELAY);
 }

 // Return the request id.
 return requestId;
}

void updateAnimations() #

Iterate over all animations and update them.

void updateAnimations() {
 // Copy the animation requests to avoid concurrent modifications.
 List<AnimationHandleImplTimer> curAnimations = new List<AnimationHandleImplTimer>.from(animationRequests);

 // Iterate over the animation requests.
 Duration duration = new Duration();
 for (AnimationHandleImplTimer requestId in curAnimations) {
   // Remove the current request.
   int index = animationRequests.indexOf(requestId);
   if (index != -1) {
     animationRequests.removeAt(index);
   }

   // Execute the callback.
   //cduration.inMilliseconds); //getStartMillis());
   requestId.getCallback().execute(new DateTime.now().millisecondsSinceEpoch);
 }

 // Reschedule the timer if there are more animation requests.
 if (animationRequests.length > 0) {
   /*
    * In order to achieve as close to 60fps as possible, we calculate the new
    * delay based on the execution time of this method. The delay will be
    * less than 16ms, assuming this method takes more than 1ms to complete.
    */
   timer.schedule(dart_math.max(MIN_FRAME_DELAY, DEFAULT_FRAME_DELAY - duration.inMilliseconds)); //elapsedMillis()));
 }
}