knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(minisvg) library(svganim)
There are at least 5 methods of animating SVG. These methods are detailed on the w3.org pages).
The 5 methods are as follows:
This package (and this vignette) uses SMIL to animate the SVG by including animation specific tags witin objects.
This vignette assumes you are familiar with SVG tags and document structures.
SMIL stands for Synchronized Multimedia Integration Language.
The way it works is to add an animate
, animateTransform
or animateMotion
tag
as a child of the element to be animated.
<animate />
- specifies an attribute on the parent object and how it should change. e.g.
change the radius of a <circle />
element.<animateTransform />
- specifies how the transformation of the parent object should
change e.g. change the rotation of the object from 0 to 90 degrees.<animationMotion />
- species how the object moves along a given path.This package includes a number of pre-baked animation types, and this vignette shows examples of how these animations can be applied to elements.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Create a simple pattern using the `svgpatternsimple` package #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ stripes <- svgpatternsimple::create_pattern_stripe(id = 'stripe') #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Create a pattern rotation animation and add it to the pattern #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ anim1 <- create_anim_pattern_rotation() stripes$append(anim1) stripes$save_full_svg("svg/README-stripe-rotation.svg", height = 100)
cat( "<pre>", "<details><summary style='color: #4169E1;'> Show/hide SVG text </summary>", htmltools::htmlEscape(as.character(stripes)), "</details>", "</pre>", sep='')
if (interactive()) { stripes$show() }
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Create a document in `minisvg` #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ library(minisvg) doc <- minisvg::svg_doc(width = 400, height = 200) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Create the pulse animation #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ pulse <- create_anim_pulse_line(size2 = 20) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Create a rectangle which includes the animation #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ rect <- stag$rect( x = "10%", y = "10%", width = "80%", height = "80%", fill = "blue", stroke = 'black', pulse ) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Add the animated rectangle to the svg document and save #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ doc$append(rect) # doc$show() doc$save("svg/README-example-pulse.svg")
cat( "<pre>", "<details><summary style='color: #4169E1;'> Show/hide SVG text </summary>", htmltools::htmlEscape(as.character(doc)), "</details>", "</pre>", sep='')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Create a document in `minisvg` #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ library(minisvg) doc <- minisvg::svg_doc(width = 400, height = 200) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Create the rotation animation around a particular point #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ rotation <- create_anim_rotation(cx = 200, cy = 100) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Create a rectangle which includes the animation #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ rect <- stag$rect( x = "10%", y = "10%", width = "80%", height = "80%", fill = "blue", stroke = 'black', rotation ) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Add the animated rectangle to the svg document and save #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ doc$append(rect) # doc$show() doc$save("svg/README-example-rotation.svg")
cat( "<pre>", "<details><summary style='color: #4169E1;'> Show/hide SVG text </summary>", htmltools::htmlEscape(as.character(doc)), "</details>", "</pre>", sep='')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Create a document in `minisvg` #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ library(minisvg) doc <- minisvg::svg_doc(width = 400, height = 200) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Create the motion animated along the path given by xs, ys #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ path_motion <- create_anim_motion(xs = c(20, 180, 180, 20), ys = c(20, 20, 80, 80)) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Create a rectangle which includes multiple animations #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ rect <- stag$rect( x = "10%", y = "10%", width = "20%", height = "20%", fill = "blue", stroke = 'black', path_motion, pulse ) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Add the animated rectangle to the svg document and save #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ doc$append(rect) # doc$show() doc$save("svg/README-example-pathmotion.svg")
cat( "<pre>", "<details><summary style='color: #4169E1;'> Show/hide SVG text </summary>", htmltools::htmlEscape(as.character(doc)), "</details>", "</pre>", sep='')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.