Class Transition

java.lang.Object
com.oorian.css.transitions.Transition
Direct Known Subclasses:
Collapse, ColorShift, Fade, Grow, Morph, Move, Reshape, Slide

public abstract class Transition extends Object
Abstract base class for CSS transitions.

Transition provides a high-level, object-oriented API for CSS transitions. Each concrete subclass represents a specific visual effect (fade, slide, grow, etc.) and encapsulates all CSS property details. Developers configure transitions 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). The default speed of 5 maps to 300ms. For precise control, use setDuration(float) to override the speed scale with an exact duration in seconds.

Usage:


 Fade fade = new Fade();
 fade.setSpeed(7);       // Fast fade
 fade.setTo(0f);         // Fade to invisible

 Slide slide = new Slide();
 slide.setDirection(Direction.LEFT);
 slide.setDuration(0.5f);  // Exact 500ms duration
 
Since:
2026
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • Transition

      public Transition()
      Constructs a Transition with default speed (5).
  • Method Details

    • setSpeed

      public Transition setSpeed(int speed)
      Sets the transition 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 transition for method chaining
    • setDuration

      public Transition 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 transition.

      Parameters:
      seconds - the duration in seconds
      Returns:
      this transition for method chaining
    • getDurationMs

      public long getDurationMs()
      Returns the transition 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
    • getTransitionProperties

      protected abstract String[] getTransitionProperties()
      Returns the CSS property names that this transition animates.

      Subclasses return the CSS properties they modify (e.g., "opacity", "transform", "max-height"). These are used to build the CSS transition shorthand.

      Returns:
      an array of CSS property names
    • applyEndState

      public abstract void applyEndState(CssStyle style)
      Applies the transition's end state to the given style.

      This method sets the CSS property values that represent the final state of the transition. For example, a fade-out sets opacity: 0.

      Parameters:
      style - the CssStyle to apply end-state values to
    • snapshotCurrentState

      public abstract void snapshotCurrentState(CssStyle current, CssStyle snapshot)
      Saves the current values of the transition's properties from the element's style.

      Before a transition plays forward, the current property values are saved so they can be restored when playing in reverse. If a property has no current value, the subclass should save a sensible default (e.g., "1" for opacity).

      Parameters:
      current - the element's current CssStyle to read values from
      snapshot - the CssStyle to save values into
    • restoreOriginalState

      public abstract void restoreOriginalState(CssStyle style, CssStyle snapshot)
      Restores previously saved property values to the given style.

      This method copies values from the snapshot back to the element's style, causing the browser to animate back to the original state.

      Parameters:
      style - the CssStyle to restore values to
      snapshot - the CssStyle containing previously saved values
    • getTimingFunction

      protected TransitionTimingFunction getTimingFunction()
      Returns the timing function for this transition.

      The default is TransitionTimingFunction.EASE. Subclasses may override to use a different timing function (e.g., Collapse uses TransitionTimingFunction.EASE_IN_OUT).

      Returns:
      the timing function
    • buildTransitionValue

      public final String buildTransitionValue()
      Builds the CSS transition shorthand value for this transition.

      Generates a comma-separated list of transition declarations, one per animated property. For example: "opacity 300ms ease, transform 300ms ease".

      Returns:
      the CSS transition shorthand value
    • buildTransitionValue

      public final String buildTransitionValue(long delayMs)
      Builds the CSS transition shorthand value with a delay.

      Generates a comma-separated list of transition declarations with the specified delay appended. For example: "opacity 300ms ease 150ms, transform 300ms ease 150ms".

      Parameters:
      delayMs - the delay in milliseconds before the transition starts
      Returns:
      the CSS transition shorthand value