| Title: | R Bindings to the 'Anime.js' Animation Library |
|---|---|
| Description: | Provides low-level R bindings to the 'Anime.js' library (<https://animejs.com>), enabling the creation of browser-native SVG and HTML animations via the 'htmlwidgets' framework. |
| Authors: | Long Nguyen [aut, cre] (ORCID: <https://orcid.org/0000-0001-8878-7386>) |
| Maintainer: | Long Nguyen <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.1.0 |
| Built: | 2026-10-13 12:50:13 UTC |
| Source: | https://github.com/long39ng/animejs |
Pipe-friendly. Each call appends one segment: a set of CSS property animations applied to a target selector.
anime_add( timeline, selector, props, offset = "+=0", duration = NULL, ease = NULL, delay = NULL, stagger = NULL )anime_add( timeline, selector, props, offset = "+=0", duration = NULL, ease = NULL, delay = NULL, stagger = NULL )
timeline |
An |
selector |
CSS selector string identifying the SVG/HTML elements to
animate. Use |
props |
A named list of property animations. Values may be scalars,
two-element numeric vectors (from/to), |
offset |
Timeline position. |
duration |
Overrides the timeline default for this segment. |
ease |
Overrides the timeline default for this segment. |
delay |
Overrides the timeline default for this segment. |
stagger |
An |
The modified anime_timeline object.
anime_timeline() |> anime_add( selector = anime_target_class("circle"), props = list(opacity = anime_from_to(0, 1)), duration = 600 )anime_timeline() |> anime_add( selector = anime_target_class("circle"), props = list(opacity = anime_from_to(0, 1)), duration = 600 )
The R equivalent of Anime.js v4's animate(targets, parameters): one set
of property animations applied to one target selector, without a timeline.
Use anime_timeline() and anime_add() instead when several segments must
be sequenced.
anime_animate( selector, props, duration = NULL, ease = NULL, delay = NULL, loop = FALSE, alternate = FALSE, reversed = FALSE, autoplay = TRUE, stagger = NULL )anime_animate( selector, props, duration = NULL, ease = NULL, delay = NULL, loop = FALSE, alternate = FALSE, reversed = FALSE, autoplay = TRUE, stagger = NULL )
selector |
CSS selector string identifying the SVG/HTML elements to
animate. Use |
props |
A named list of property animations. Values may be scalars,
two-element numeric vectors (from/to), |
duration |
Duration in milliseconds. |
ease |
Easing, an |
delay |
Delay in milliseconds before the animation starts. |
loop |
Logical or positive integer. |
alternate |
Logical. Alternate direction on each iteration. |
reversed |
Logical. Play in reverse from the end. |
autoplay |
Logical. Start playing immediately on load. |
stagger |
An |
Like a timeline, an anime_animation can be modified with
anime_playback() and anime_on(), and is rendered with anime_render().
An anime_animation object.
anime_animate( selector = anime_target_class("circle"), props = list( translateY = anime_from_to(-40, 0), opacity = anime_from_to(0, 1) ), duration = 600, ease = anime_easing_spring() )anime_animate( selector = anime_target_class("circle"), props = list( translateY = anime_from_to(-40, 0), opacity = anime_from_to(0, 1) ), duration = 600, ease = anime_easing_spring() )
A family of constructors for Anime.js v4 easing specifications. Each
returns an anime_easing object that serialises to the corresponding
Anime.js v4 easing inside anime_timeline(), anime_add(),
anime_animate(), or anime_playback().
anime_easing(family = "Quad", direction = "out") anime_easing_elastic(direction = "out", amplitude = 1, period = 0.3) anime_easing_back(direction = "out", overshoot = 1.70158) anime_easing_bezier(x1, y1, x2, y2) anime_easing_steps(count, from_start = FALSE) anime_easing_spring(bounce = 0.5, duration = 628)anime_easing(family = "Quad", direction = "out") anime_easing_elastic(direction = "out", amplitude = 1, period = 0.3) anime_easing_back(direction = "out", overshoot = 1.70158) anime_easing_bezier(x1, y1, x2, y2) anime_easing_steps(count, from_start = FALSE) anime_easing_spring(bounce = 0.5, duration = 628)
family |
Character. One of "linear", "Quad", "Cubic", "Quart", "Quint", "Sine", "Expo", "Circ", "Bounce". |
direction |
Character. One of "in", "out", "inOut", "outIn". |
amplitude, period
|
(Elastic easing) Numeric. Overshoot amplitude in [1, 10] and oscillation period in (0, 2]. |
overshoot |
(Back easing) Numeric. Overshoot amount. |
x1, y1, x2, y2
|
(Cubic bezier easing) Coordinates of the first and
second control point. |
count |
(Steps easing) Positive integer. Number of discrete steps. |
from_start |
(Steps easing) Logical. If |
bounce |
(Spring easing) Number in [-1, 1]. Controls bounciness. Values from 0 to 1 produce bouncy curves; values below 0 produce over-damped curves. Keep within [-0.5, 0.5] for predictable behaviour. |
duration |
(Spring easing) Number in [10, 10000]. The perceived duration in milliseconds at which the animation feels visually complete. |
Plain Anime.js v4 easing name strings (e.g. "inOutSine",
"outElastic(1,0.3)") are also accepted wherever an anime_easing object
is expected. Note that Anime.js v4 has removed the string syntax for
cubicBezier(), steps(), and spring easings; use the constructors below
for those, and the widget reconstructs the corresponding function calls in
JavaScript.
An anime_easing object.
anime_easing("linear") anime_easing("Quad", "outIn") anime_easing_elastic() anime_easing_elastic("in", amplitude = 1.5, period = 0.3) anime_easing_back() anime_easing_back("in", overshoot = 2.5) anime_easing_bezier(0.4, 0, 0.2, 1) anime_easing_bezier(0.68, -0.55, 0.265, 1.55) anime_easing_steps(10) anime_easing_steps(5, from_start = TRUE) anime_easing_spring() anime_easing_spring(bounce = 0.65, duration = 350)anime_easing("linear") anime_easing("Quad", "outIn") anime_easing_elastic() anime_easing_elastic("in", amplitude = 1.5, period = 0.3) anime_easing_back() anime_easing_back("in", overshoot = 2.5) anime_easing_bezier(0.4, 0, 0.2, 1) anime_easing_bezier(0.68, -0.55, 0.265, 1.55) anime_easing_steps(10) anime_easing_steps(5, from_start = TRUE) anime_easing_spring() anime_easing_spring(bounce = 0.65, duration = 350)
Convenience constructor for a two-value property animation that runs from
from to to. An optional CSS unit suffix is concatenated into both
values during serialisation (e.g. 100 with unit = "px" becomes
"100px").
anime_from_to(from, to, unit = "", ease = NULL)anime_from_to(from, to, unit = "", ease = NULL)
from |
Numeric. Starting value. |
to |
Numeric. Ending value. |
unit |
Character. Optional CSS unit suffix, e.g. |
ease |
Optional easing override for this property alone, an
|
An anime_from_to object.
anime_from_to(0, 1) anime_from_to(0, 360, unit = "deg") anime_from_to(0, 1, ease = anime_easing_spring())anime_from_to(0, 1) anime_from_to(0, 360, unit = "deg") anime_from_to(0, 1, ease = anime_easing_spring())
Constructs a keyframes object for use in the props argument of
anime_add() or anime_animate(). Each positional argument is one
keyframe.
anime_keyframes(...)anime_keyframes(...)
... |
< |
An anime_keyframes object.
# Bare numeric keyframe values anime_add( anime_timeline(), selector = ".circle", props = list( opacity = anime_keyframes(0, 1, 0.5), translateY = anime_keyframes(-20, 0, 10) ) ) # Per-keyframe lists with optional ease and duration overrides anime_add( anime_timeline(), selector = ".circle", props = list( opacity = anime_keyframes( list(to = 0), list(to = 1, ease = anime_easing("Cubic"), duration = 400), list(to = 0.5, ease = "linear", duration = 200) ) ) )# Bare numeric keyframe values anime_add( anime_timeline(), selector = ".circle", props = list( opacity = anime_keyframes(0, 1, 0.5), translateY = anime_keyframes(-20, 0, 10) ) ) # Per-keyframe lists with optional ease and duration overrides anime_add( anime_timeline(), selector = ".circle", props = list( opacity = anime_keyframes( list(to = 0), list(to = 1, ease = anime_easing("Cubic"), duration = 400), list(to = 0.5, ease = "linear", duration = 200) ) ) )
The callback must be the name of a globally scoped JavaScript function
already present on the page, for example one injected via
htmltools::tags$script(). At render time the JavaScript binding resolves
the name to window[callback] and attaches it to the corresponding
Anime.js callback.
anime_on(x, event, callback)anime_on(x, event, callback)
x |
An |
event |
One of "onBegin", "onBeforeUpdate", "onUpdate", "onRender", "onLoop", "onPause", "onComplete", matching the Anime.js v4 callback API. |
callback |
Character scalar. Name of the global JS function to invoke. |
The modified object.
if (interactive() && rlang::is_installed("htmltools")) { svg_src <- '<svg viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg"> <circle class="circle" cx="100" cy="50" r="20" fill="#4e79a7"/> </svg>' widget <- anime_timeline(duration = 800) |> anime_add(selector = ".circle", props = list(opacity = c(0, 1))) |> anime_on("onComplete", "handleAnimationDone") |> anime_render(svg = svg_src) callback_js <- htmltools::tags$script( "function handleAnimationDone() { console.log('Animation complete.'); }" ) htmltools::browsable(htmltools::tagList(callback_js, widget)) }if (interactive() && rlang::is_installed("htmltools")) { svg_src <- '<svg viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg"> <circle class="circle" cx="100" cy="50" r="20" fill="#4e79a7"/> </svg>' widget <- anime_timeline(duration = 800) |> anime_add(selector = ".circle", props = list(opacity = c(0, 1))) |> anime_on("onComplete", "handleAnimationDone") |> anime_render(svg = svg_src) callback_js <- htmltools::tags$script( "function handleAnimationDone() { console.log('Animation complete.'); }" ) htmltools::browsable(htmltools::tagList(callback_js, widget)) }
Sets autoplay, looping, direction, speed, and optional controls UI on an
anime_timeline or anime_animation. Arguments left as NULL keep the
settings already stored on the object.
anime_playback( x, autoplay = NULL, loop = NULL, loop_delay = NULL, playback_rate = NULL, reversed = NULL, alternate = NULL, controls = NULL )anime_playback( x, autoplay = NULL, loop = NULL, loop_delay = NULL, playback_rate = NULL, reversed = NULL, alternate = NULL, controls = NULL )
x |
An |
autoplay |
Logical. Start playing immediately on load. |
loop |
Logical or positive integer. |
loop_delay |
Numeric. Delay in milliseconds between iterations. |
playback_rate |
Numeric. Playback speed multiplier (1 is normal speed). |
reversed |
Logical. Play in reverse from the end. |
alternate |
Logical. Alternate direction on each iteration (requires
|
controls |
Logical. Inject a play/pause/seek control bar into the widget container. |
The modified anime_timeline or anime_animation object.
anime_timeline() |> anime_playback(loop = TRUE, alternate = TRUE, controls = TRUE)anime_timeline() |> anime_playback(loop = TRUE, alternate = TRUE, controls = TRUE)
Serialises an anime_timeline() or anime_animate() object to JSON and
wraps it together with an SVG payload in an htmlwidget.
anime_render(x, svg = NULL, width = NULL, height = NULL, elementId = NULL)anime_render(x, svg = NULL, width = NULL, height = NULL, elementId = NULL)
x |
An |
svg |
Character. Raw SVG markup to embed in the widget. If |
width |
Fixed width for widget (in css units). The default is
|
height |
Fixed height for widget (in css units). The default is
|
elementId |
Use an explicit element ID for the widget (rather than an automatically generated one). Useful if you have other JavaScript that needs to explicitly discover and interact with a specific widget instance. |
An object of class c("animejs", "htmlwidget").
tl <- anime_timeline(duration = 800) |> anime_add( selector = anime_target_class("dot"), props = list(opacity = anime_from_to(0, 1)) ) svg <- '<svg viewBox="0 0 100 100"><circle class="dot" cx="50" cy="50" r="10"/></svg>' if (interactive()) { anime_render(tl, svg) }tl <- anime_timeline(duration = 800) |> anime_add( selector = anime_target_class("dot"), props = list(opacity = anime_from_to(0, 1)) ) svg <- '<svg viewBox="0 0 100 100"><circle class="dot" cx="50" cy="50" r="10"/></svg>' if (interactive()) { anime_render(tl, svg) }
When applied to a multi-element selector, Anime.js distributes animation start times across elements according to the stagger value.
anime_stagger( value, from = "first", start = NULL, reversed = FALSE, grid = NULL, axis = NULL, ease = NULL )anime_stagger( value, from = "first", start = NULL, reversed = FALSE, grid = NULL, axis = NULL, ease = NULL )
value |
Numeric. Base delay in milliseconds between each element. |
from |
One of |
start |
Numeric. Starting value added to every staggered delay. |
reversed |
Logical. Reverse the stagger order. |
grid |
Integer vector of length 2 ( |
axis |
One of |
ease |
Easing applied to the stagger distribution itself, an
|
An anime_stagger object.
# Simple linear stagger, 100 ms between elements anime_stagger(100) # Stagger from centre outward anime_stagger(200, from = "center") # 2-D grid stagger along the x axis anime_stagger(50, grid = c(3, 4), axis = "x")# Simple linear stagger, 100 ms between elements anime_stagger(100) # Stagger from centre outward anime_stagger(200, from = "center") # 2-D grid stagger along the x axis anime_stagger(50, grid = c(3, 4), axis = "x")
Target elements by CSS class
anime_target_class(cls)anime_target_class(cls)
cls |
Character scalar. Class name without a leading dot. |
A CSS selector string of the form ".<cls>".
anime_target_class("circle")anime_target_class("circle")
A pass-through for selectors not covered by the other anime_target_*()
helpers.
anime_target_css(selector)anime_target_css(selector)
selector |
Character scalar. A valid CSS selector string. |
selector unchanged.
anime_target_css(".panel > circle")anime_target_css(".panel > circle")
The primary mechanism for targeting individual elements annotated with a
data-animejs-id attribute, by hand or by an upstream SVG annotation tool.
anime_target_id(id)anime_target_id(id)
id |
Character scalar. Value of the |
A CSS selector string of the form "[data-animejs-id='<id>']".
anime_target_id("c1")anime_target_id("c1")
Produces an attribute selector matching a data-layer attribute, a
convention used by SVG annotation pipelines that tag all data elements
belonging to one plot layer (e.g. ggplot2 layers) with their layer index.
anime_target_layer(layer_index)anime_target_layer(layer_index)
layer_index |
Integer scalar. 1-based index of the layer. |
A CSS selector string of the form "[data-layer='<layer_index>']".
anime_target_layer(1L)anime_target_layer(1L)
Constructs a text-swap specification for use in the props argument of
anime_add(). Unlike a numeric tween, a text prop does not interpolate:
the values are spread evenly across the segment's duration and the target
element's textContent is swapped to the value for the current position as
the timeline plays or is scrubbed. Useful for animated titles, counters,
and tickers.
anime_text(values)anime_text(values)
values |
An atomic vector of the successive text values. Non-character
vectors are coerced with |
The segment's selector picks the text element(s) to update, so the prop
name you file this under is only a label; use something descriptive such as
text or label.
An anime_text object.
# A counter that steps through four values across the segment anime_add( anime_timeline(), selector = anime_target_id("counter"), props = list(text = anime_text(c("0", "25", "50", "100"))) ) # Compose a text swap with an ordinary tween on the same segment anime_add( anime_timeline(), selector = anime_target_id("title"), props = list( opacity = anime_keyframes(0, 1), label = anime_text(c("Loading", "Ready")) ) )# A counter that steps through four values across the segment anime_add( anime_timeline(), selector = anime_target_id("counter"), props = list(text = anime_text(c("0", "25", "50", "100"))) ) # Compose a text swap with an ordinary tween on the same segment anime_add( anime_timeline(), selector = anime_target_id("title"), props = list( opacity = anime_keyframes(0, 1), label = anime_text(c("Loading", "Ready")) ) )
Initialise an Anime.js timeline
anime_timeline(duration = 1000, ease = anime_easing(), delay = 0, loop = FALSE)anime_timeline(duration = 1000, ease = anime_easing(), delay = 0, loop = FALSE)
duration |
Default duration in milliseconds for all segments. |
ease |
Default easing for all segments, an |
delay |
Default delay in milliseconds between segments. |
loop |
Logical or positive integer. |
An anime_timeline object.
anime_timeline(duration = 800, ease = anime_easing())anime_timeline(duration = 800, ease = anime_easing())
This is the low-level constructor. Most users will not call this directly;
it is the final rendering step called by anime_render().
animejs_widget(svg, config, width = NULL, height = NULL, elementId = NULL)animejs_widget(svg, config, width = NULL, height = NULL, elementId = NULL)
svg |
Character. Raw SVG markup to embed in the widget. If |
config |
List. A serialisable animation specification produced by
|
width |
Fixed width for widget (in css units). The default is
|
height |
Fixed height for widget (in css units). The default is
|
elementId |
Use an explicit element ID for the widget (rather than an automatically generated one). Useful if you have other JavaScript that needs to explicitly discover and interact with a specific widget instance. |
An object of class c("animejs", "htmlwidget").
Output and render functions for using animejs widgets within Shiny applications and interactive R Markdown documents.
animejsOutput(outputId, width = "100%", height = "400px") renderAnimejs(expr, env = parent.frame(), quoted = FALSE)animejsOutput(outputId, width = "100%", height = "400px") renderAnimejs(expr, env = parent.frame(), quoted = FALSE)
outputId |
Output variable to read from. |
width, height
|
Must be a valid CSS unit (like |
expr |
An expression that generates an animejs widget, typically a
call to |
env |
The environment in which to evaluate |
quoted |
Is |
animejsOutput() returns a Shiny output function that can be used
in a UI definition; renderAnimejs() returns a Shiny render function
that can be assigned to an output slot.
if (interactive() && rlang::is_installed("shiny")) { library(shiny) svg_src <- '<svg viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg"> <circle class="dot" cx="100" cy="50" r="20" fill="#4e79a7"/> </svg>' ui <- fluidPage( sliderInput("duration", "Duration (ms)", 200, 2000, 800), animejsOutput("anim", height = "200px") ) server <- function(input, output, session) { output$anim <- renderAnimejs({ anime_animate( selector = anime_target_class("dot"), props = list(opacity = anime_from_to(0, 1)), duration = input$duration ) |> anime_render(svg = svg_src) }) } shinyApp(ui, server) }if (interactive() && rlang::is_installed("shiny")) { library(shiny) svg_src <- '<svg viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg"> <circle class="dot" cx="100" cy="50" r="20" fill="#4e79a7"/> </svg>' ui <- fluidPage( sliderInput("duration", "Duration (ms)", 200, 2000, 800), animejsOutput("anim", height = "200px") ) server <- function(input, output, session) { output$anim <- renderAnimejs({ anime_animate( selector = anime_target_class("dot"), props = list(opacity = anime_from_to(0, 1)), duration = input$duration ) |> anime_render(svg = svg_src) }) } shinyApp(ui, server) }