Class Animation

java.lang.Object
com.oorian.css.animations.Animation
Direct Known Subclasses:
Beat, BeatFade, Bounce, Fade, Flip, Shake, Spin, SpinPulse

public abstract class Animation extends Object
Abstract base class for CSS keyframe animations.

Animation provides a high-level, object-oriented API for CSS @keyframes animations. Each concrete subclass represents a specific visual effect (beat, bounce, spin, etc.) and encapsulates its own @keyframes definition, timing function, and default configuration. Developers configure animations using type-safe setters instead of raw CSS strings.

Speed Scale: The setSpeed(int) method accepts values 0-10, where 0 is slowest (1200ms) and 10 is fastest (50ms). For precise control, use setDuration(float) to override the speed scale with an exact duration in seconds.

Usage:


 // Apply a spinning animation
 icon.animate(new Spin());

 // Bounce with custom speed
 button.animate(new Bounce().setSpeed(7));

 // Shake 3 times then stop
 panel.animate(new Shake().setIterationCount(3));

 // Stop any animation
 icon.stopAnimation();
 
Since:
2026
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • Animation

      protected Animation(int defaultSpeed)
      Constructs an Animation with the specified default speed.
      Parameters:
      defaultSpeed - the default speed on a 0-10 scale
  • Method Details

    • setSpeed

      public Animation setSpeed(int speed)
      Sets the animation speed on a 0-10 scale.

      Speed values map to durations: 0 = 1200ms (slowest), 5 = 300ms (default), 10 = 50ms (fastest). Values outside the 0-10 range are clamped.

      Parameters:
      speed - the speed value (0-10)
      Returns:
      this animation for method chaining
    • setDuration

      public Animation setDuration(float seconds)
      Sets an exact duration in seconds, overriding the speed scale.

      Use this for precise timing control when the 0-10 speed scale is too coarse. For example, setDuration(0.5f) sets a 500ms animation.

      Parameters:
      seconds - the duration in seconds
      Returns:
      this animation for method chaining
    • setIterationCount

      public Animation setIterationCount(int count)
      Sets the number of times the animation plays.

      Use -1 for infinite looping (the default for most animations). Use a positive integer for a specific number of repetitions.

      Parameters:
      count - the iteration count (-1 for infinite)
      Returns:
      this animation for method chaining
    • setDirection

      public Animation setDirection(AnimationDirection direction)
      Sets the animation direction.
      Parameters:
      direction - the direction (normal, reverse, alternate, alternate-reverse)
      Returns:
      this animation for method chaining
    • setFillMode

      public Animation setFillMode(AnimationFillMode fillMode)
      Sets the animation fill mode.
      Parameters:
      fillMode - the fill mode (none, forwards, backwards, both)
      Returns:
      this animation for method chaining
    • setDelay

      public Animation setDelay(long ms)
      Sets the delay before the animation starts.
      Parameters:
      ms - the delay in milliseconds
      Returns:
      this animation for method chaining
    • getDurationMs

      public long getDurationMs()
      Returns the animation duration in milliseconds.

      If a duration override has been set via setDuration(float), that value is used. Otherwise, the duration is derived from the speed scale.

      Returns:
      the duration in milliseconds
    • getKeyFramesId

      public abstract String getKeyFramesId()
      Returns the unique ID for this animation's keyframes style element.

      This ID is used to deduplicate keyframe injection into the page head. Subclasses with configurable keyframes (e.g., Beat with a custom scale) should return a unique ID per configuration to avoid collisions.

      Returns:
      the keyframes style element ID
    • createKeyFrames

      public abstract KeyFrames createKeyFrames()
      Creates the @keyframes definition for this animation.
      Returns:
      a KeyFrames object defining the animation sequence
    • getDefaultTimingFunction

      protected abstract AnimationTimingFunction getDefaultTimingFunction()
      Returns the default timing function for this animation.

      Each animation subclass provides its own default timing function. For custom cubic-bezier or steps timing, override getTimingFunctionValue() instead.

      Returns:
      the default timing function, or null if getTimingFunctionValue() provides a custom value
    • getTimingFunctionValue

      protected String getTimingFunctionValue()
      Returns the CSS timing function value as a string.

      By default, delegates to getDefaultTimingFunction(). Override this method to provide custom timing function values like cubic-bezier(...) or steps(...) that are not represented by the enum.

      Returns:
      the CSS timing function value
    • getStyleSheet

      public CssStyleSheet getStyleSheet()
      Returns a CssStyleSheet containing this animation's keyframes.

      Use this method for manual keyframe injection in createHead() when the element is not yet attached to the page tree:

      
       head.addCssStyleSheet(animation.getStyleSheet());
       
      Returns:
      a stylesheet containing the keyframes definition
    • apply

      public final void apply(StyledElement<?> element)
      Applies this animation's CSS properties to the given style attributes.

      Sets animation-name, animation-duration, animation-timing-function, animation-iteration-count, animation-direction, animation-fill-mode, and animation-delay on the element.

      Parameters:
      element - the element to animate