Class Transition
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract voidapplyEndState(CssStyle style) Applies the transition's end state to the given style.final StringBuilds the CSStransitionshorthand value for this transition.final StringbuildTransitionValue(long delayMs) Builds the CSStransitionshorthand value with a delay.longReturns the transition duration in milliseconds.protected TransitionTimingFunctionReturns the timing function for this transition.protected abstract String[]Returns the CSS property names that this transition animates.abstract voidrestoreOriginalState(CssStyle style, CssStyle snapshot) Restores previously saved property values to the given style.setDuration(float seconds) Sets an exact duration in seconds, overriding the speed scale.setSpeed(int speed) Sets the transition speed on a 0-10 scale.abstract voidsnapshotCurrentState(CssStyle current, CssStyle snapshot) Saves the current values of the transition's properties from the element's style.
-
Constructor Details
-
Transition
public Transition()Constructs a Transition with default speed (5).
-
-
Method Details
-
setSpeed
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
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
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 CSStransitionshorthand.- Returns:
- an array of CSS property names
-
applyEndState
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
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 fromsnapshot- the CssStyle to save values into
-
restoreOriginalState
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 tosnapshot- the CssStyle containing previously saved values
-
getTimingFunction
Returns the timing function for this transition.The default is
TransitionTimingFunction.EASE. Subclasses may override to use a different timing function (e.g.,CollapseusesTransitionTimingFunction.EASE_IN_OUT).- Returns:
- the timing function
-
buildTransitionValue
Builds the CSStransitionshorthand 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
Builds the CSStransitionshorthand 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
-