| Title: | R Bindings to the 'Anime.js' Animation Library |
| Version: | 1.0.0 |
| 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. |
| License: | MIT + file LICENSE |
| URL: | https://github.com/long39ng/animejs, https://long39ng.github.io/animejs/ |
| BugReports: | https://github.com/long39ng/animejs/issues |
| Depends: | R (≥ 4.1.0) |
| Imports: | cli, htmlwidgets, rlang |
| Suggests: | htmltools, jsonlite, knitr, rmarkdown, shiny, testthat (≥ 3.0.0) |
| VignetteBuilder: | knitr |
| Config/Needs/website: | rmarkdown |
| Config/testthat/edition: | 3 |
| Encoding: | UTF-8 |
| Config/roxygen2/version: | 8.0.0 |
| NeedsCompilation: | no |
| Packaged: | 2026-07-20 09:43:20 UTC; long |
| Author: | Long Nguyen |
| Maintainer: | Long Nguyen <nguyen@dezim-institut.de> |
| Repository: | CRAN |
| Date/Publication: | 2026-07-20 10:10:02 UTC |
animejs: R Bindings to the Anime.js Animation Library
Description
animejs provides a pipe-friendly R interface to
Anime.js v4, a JavaScript animation library.
Animations are authored in R and serialised to JSON; the htmlwidgets
infrastructure renders them in a browser environment.
The central workflow is:
Create a single animation with
anime_animate(), or a timeline withanime_timeline()and add segments to it withanime_add().Configure playback with
anime_playback()and attach event callbacks withanime_on().Render to an htmlwidget with
anime_render().
Property values are specified via anime_from_to() and anime_keyframes().
Per-element delay offsets are specified via anime_stagger().
Easing functions are specified via the anime_easing_*() family.
Target selectors are constructed via the anime_target_*() family.
In Shiny applications, use animejsOutput() and renderAnimejs().
Package options
None currently. All configuration is per-animation.
Author(s)
Maintainer: Long Nguyen nguyen@dezim-institut.de (ORCID)
Authors:
Long Nguyen nguyen@dezim-institut.de (ORCID)
See Also
Useful links:
Report bugs at https://github.com/long39ng/animejs/issues
Add an animation segment to a timeline
Description
Pipe-friendly. Each call appends one segment: a set of CSS property animations applied to a target selector.
Usage
anime_add(
timeline,
selector,
props,
offset = "+=0",
duration = NULL,
ease = NULL,
delay = NULL,
stagger = NULL
)
Arguments
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 |
Value
The modified anime_timeline object.
Examples
anime_timeline() |>
anime_add(
selector = anime_target_class("circle"),
props = list(opacity = anime_from_to(0, 1)),
duration = 600
)
Create a single Anime.js animation
Description
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.
Usage
anime_animate(
selector,
props,
duration = NULL,
ease = NULL,
delay = NULL,
loop = FALSE,
alternate = FALSE,
reversed = FALSE,
autoplay = TRUE,
stagger = NULL
)
Arguments
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 |
Details
Like a timeline, an anime_animation can be modified with
anime_playback() and anime_on(), and is rendered with anime_render().
Value
An anime_animation object.
Examples
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()
)
Easing constructors
Description
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().
Usage
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)
Arguments
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. |
Details
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.
Value
An anime_easing object.
Examples
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)
Specify a from/to property range
Description
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").
Usage
anime_from_to(from, to, unit = "", ease = NULL)
Arguments
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
|
Value
An anime_from_to object.
Examples
anime_from_to(0, 1)
anime_from_to(0, 360, unit = "deg")
anime_from_to(0, 1, ease = anime_easing_spring())
Specify per-property keyframes for an animation
Description
Constructs a keyframes object for use in the props argument of
anime_add() or anime_animate(). Each positional argument is one
keyframe.
Usage
anime_keyframes(...)
Arguments
... |
< |
Value
An anime_keyframes object.
Examples
# 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)
)
)
)
Attach a JavaScript callback to an animation event
Description
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.
Usage
anime_on(x, event, callback)
Arguments
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. |
Value
The modified object.
Examples
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))
}
Configure animation playback
Description
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.
Usage
anime_playback(
x,
autoplay = NULL,
loop = NULL,
loop_delay = NULL,
playback_rate = NULL,
reversed = NULL,
alternate = NULL,
controls = NULL
)
Arguments
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. |
Value
The modified anime_timeline or anime_animation object.
Examples
anime_timeline() |>
anime_playback(loop = TRUE, alternate = TRUE, controls = TRUE)
Render an animation or timeline as an htmlwidget
Description
Serialises an anime_timeline() or anime_animate() object to JSON and
wraps it together with an SVG payload in an htmlwidget.
Usage
anime_render(x, svg = NULL, width = NULL, height = NULL, elementId = NULL)
Arguments
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. |
Value
An object of class c("animejs", "htmlwidget").
Examples
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)
}
Create a stagger configuration for per-element delay offsets
Description
When applied to a multi-element selector, Anime.js distributes animation start times across elements according to the stagger value.
Usage
anime_stagger(
value,
from = "first",
start = NULL,
reversed = FALSE,
grid = NULL,
axis = NULL,
ease = NULL
)
Arguments
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
|
Value
An anime_stagger object.
Examples
# 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
Description
Target elements by CSS class
Usage
anime_target_class(cls)
Arguments
cls |
Character scalar. Class name without a leading dot. |
Value
A CSS selector string of the form ".<cls>".
Examples
anime_target_class("circle")
Target elements by an arbitrary CSS selector
Description
A pass-through for selectors not covered by the other anime_target_*()
helpers.
Usage
anime_target_css(selector)
Arguments
selector |
Character scalar. A valid CSS selector string. |
Value
selector unchanged.
Examples
anime_target_css(".panel > circle")
Target SVG or HTML elements by a data-animejs-id attribute
Description
The primary mechanism for targeting individual elements annotated with a
data-animejs-id attribute, by hand or by an upstream SVG annotation tool.
Usage
anime_target_id(id)
Arguments
id |
Character scalar. Value of the |
Value
A CSS selector string of the form "[data-animejs-id='<id>']".
Examples
anime_target_id("c1")
Target elements by a data-layer attribute
Description
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.
Usage
anime_target_layer(layer_index)
Arguments
layer_index |
Integer scalar. 1-based index of the layer. |
Value
A CSS selector string of the form "[data-layer='<layer_index>']".
Examples
anime_target_layer(1L)
Initialise an Anime.js timeline
Description
Initialise an Anime.js timeline
Usage
anime_timeline(duration = 1000, ease = anime_easing(), delay = 0, loop = FALSE)
Arguments
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. |
Value
An anime_timeline object.
Examples
anime_timeline(duration = 800, ease = anime_easing())
Shiny bindings for animejs
Description
Output and render functions for using animejs widgets within Shiny applications and interactive R Markdown documents.
Usage
animejsOutput(outputId, width = "100%", height = "400px")
renderAnimejs(expr, env = parent.frame(), quoted = FALSE)
Arguments
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 |
Value
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.
Examples
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)
}
Create a bare animejs htmlwidget
Description
This is the low-level constructor. Most users will not call this directly;
it is the final rendering step called by anime_render().
Usage
animejs_widget(svg, config, width = NULL, height = NULL, elementId = NULL)
Arguments
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. |
Value
An object of class c("animejs", "htmlwidget").