| 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: | 0.1.1 |
| Built: | 2026-06-01 09:42:05 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 vectors (from/to), or |
offset |
Timeline offset. |
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 )
A family of constructors for Anime.js v4 easing specifications. Each
returns an anime_easing object that serialises to the correct JS string
inside anime_timeline(), anime_add(), 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) 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) 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 and oscillation period. |
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. |
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. |
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_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_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 = "")anime_from_to(from, to, unit = "")
from |
Numeric. Starting value. |
to |
Numeric. Ending value. |
unit |
Character. Optional CSS unit suffix, e.g. |
An anime_from_to object.
anime_from_to(0, 1) anime_from_to(0, 360, unit = "deg")anime_from_to(0, 1) anime_from_to(0, 360, unit = "deg")
Constructs a keyframes object for use in the props argument of
anime_add(). Each positional argument is one keyframe.
anime_keyframes(...)anime_keyframes(...)
... |
Keyframe values. Either bare numeric values, or lists each with
a |
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 timeline hook.
anime_on( timeline, event = c("onBegin", "onUpdate", "onComplete", "onLoop"), callback )anime_on( timeline, event = c("onBegin", "onUpdate", "onComplete", "onLoop"), callback )
timeline |
An |
event |
One of |
callback |
Character scalar. Name of the global JS function to invoke. |
The modified anime_timeline 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, loop, direction, and optional controls UI on an
anime_timeline. Calling this function overwrites any playback settings
already on the timeline (including the loop value set in
anime_timeline()).
anime_playback( timeline, autoplay = TRUE, loop = NULL, reversed = FALSE, alternate = FALSE, controls = FALSE )anime_playback( timeline, autoplay = TRUE, loop = NULL, reversed = FALSE, alternate = FALSE, controls = FALSE )
timeline |
An |
autoplay |
Logical. Start playing immediately on load. |
loop |
Logical or integer. |
reversed |
Logical. Play the timeline 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 object.
Selialises an anime_timeline() object to JSON and wraps it together with
an SVG payload in an htmlwidget.
anime_render( timeline, svg = NULL, width = NULL, height = NULL, elementId = NULL )anime_render( timeline, svg = NULL, width = NULL, height = NULL, elementId = NULL )
timeline |
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", grid = NULL, axis = NULL, ease = NULL)anime_stagger(value, from = "first", grid = NULL, axis = NULL, ease = NULL)
value |
Numeric. Base delay in milliseconds between each element. |
from |
One of |
grid |
Integer vector of length 2 ( |
axis |
One of |
ease |
Easing applied to the stagger distribution itself. |
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 by
svg_annotate() (in gganime) or by hand.
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 the data-layer attribute injected
by gganime's SVG annotation pipeline. Exposed for power users who compose
animejs timelines against annotated ggplot2 SVG output directly.
anime_target_layer(layer_index)anime_target_layer(layer_index)
layer_index |
Integer scalar. 1-based index of the ggplot2 layer. |
A CSS selector string of the form "[data-layer='<layer_index>']".
anime_target_layer(1L)anime_target_layer(1L)
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. |
delay |
Default delay in milliseconds between segments. |
loop |
Logical or 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, timeline_config, width = NULL, height = NULL, elementId = NULL )animejs_widget( svg, timeline_config, width = NULL, height = NULL, elementId = NULL )
svg |
Character. Raw SVG markup to embed in the widget. If |
timeline_config |
List. A serialisable timeline 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)