animation.Rmd
Animation using css consists of
The following CSS code defines 2 keyframes - the first keyframe defines the colour as red, and the second keyframe defines the colour as green. This set of keyframes is given the name ‘example’.
The .ugly
class contains the animation
declaration which says the animation should take 1second and should alternate back-and-forth between the two given states forever (i.e. infinite
loops)
@keyframes example1 {
from { color: #ff0000; }
to { color: #00ff00; }
}
.ugly { animation: example1 1s infinite alternate; }
minicss
Create a keyframe by initialising an R6 object of class Keyframe
#> from { color: #000; }
Create a keyframe by using the css_keyframe()
function.
#> to { color: #00f; }
Create a keyframe linked to a particular instant in time i.e. 10% through the animation cycle.
#> 10% { color: #123456; }
A Keyframe
object behaves in the same way as Style
object, individual declarations can be updated.
#> to {
#> color: #00f;
#> margin: 10px;
#> }
Keyframes
objectframe1 <- css_keyframe(time = "from", color = '#123456')
frame2 <- css_keyframe(time = "to" , color = '#0000ff')
keyframes <- Keyframes$new(name = 'example', frame1, frame2)
keyframes
#> @keyframes example {
#> from { color: #123456; }
#> to { color: #0000ff; }
#> }
keyframes <- Keyframes$new(
name = 'example',
css_keyframe(time = "from", color = '#123456'),
css_keyframe(time = "to" , color = '#0000ff')
)
keyframes
#> @keyframes example {
#> from { color: #123456; }
#> to { color: #0000ff; }
#> }
keyframes <- Keyframes$new(name = 'example')
keyframes$add(time = "from", color = '#123456')
keyframes$add(time = "to" , color = '#0000ff')
keyframes
#> @keyframes example {
#> from { color: #123456; }
#> to { color: #0000ff; }
#> }
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Construct individual frames
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
k1 <- css_keyframe('from', color = '#ffffff')
k2 <- css_keyframe('to' , color = '#123456')
k2$update(css_prop$transform$translateX(40, 'px'))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Combine frames into @keyframes
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
kf <- css_keyframes('ex1', k1, k2)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create style to attach @keyframes to the 'h1' type
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
my_style <- css_style(".swish", font_size = "50px")
my_style$update(
css_prop$animation('ex1', duration = 1, direction = 'alternate')
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create a style sheet which includes
# - the style for the swish element (including an animation declaration)
# - the keyframes definition
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
stylesheet <- css_stylesheet(my_style, kf)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create a small HTML snippet which uses this style sheet
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
html <- glue::glue("<html><head>
<style>{stylesheet}</style>
</head>
<body>
<p class='swish'> Hello #RStats </p>
</body></html>")
#> <html><head>
#> <style>.swish {
#> font-size: 50px;
#> animation: ex1 1s infinite alternate;
#> }
#> @keyframes ex1 {
#> from { color: #ffffff; }
#> to {
#> color: #123456;
#> transform: translateX(40px);
#> }
#> }</style>
#> </head>
#> <body>
#> <p class='swish'> Hello #RStats </p>
#> </body></html>
Hello #RStats