/
animation-02-css.Rmd
131 lines (100 loc) · 3.63 KB
/
animation-02-css.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
---
title: "Animation with CSS"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Animation with CSS}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = " "
)
```
```{r setup}
library(minisvg)
```
Introduction
==============================================================================
There are at least 5 methods of animating SVG. These methods are detailed on the [w3.org pages](https://www.w3.org/TR/SVG/animate.html)).
The 5 methods are as follows:
1. SMIL - Synchronized Multimedia Integration Language. [MDN doc](https://developer.mozilla.org/en-US/docs/Web/SVG/SVG_animation_with_SMIL)
2. CSS Animations [w3 css animation reference](https://www.w3.org/TR/css-animations-1/)
3. CSS Transitions [w3 css transitions reference](https://www.w3.org/TR/css-transitions-1/)
4. SVG DOM manipulation via scripting [w3 reference](https://www.w3.org/TR/SVG/types.html#SVGDOMOverview)
5. Web Animation [w3 web animation reference](https://www.w3.org/TR/web-animations-1/)
This vignette assumes you are familiar with SVG tags and document structures.
This document describes method 2 - CSS animation.
CSS animation - inline CSS
==============================================================================
```{r}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Build a small SVG with a rectangle
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
doc <- svg_doc(width = 240, height = 100)
doc$title("Attribute animation with SMIL")
doc$rect(x=0, y=0, width=240, height = 100, stroke='black', fill='white', stroke_width = 1)
doc$rect(id = 'myrect', x = 80, y=20, width=80, height=60)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Add a CSS style block to the SVG document. CSS definitions will override
# any inline definitions defined above
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
doc$add_css(
"@keyframes moveit {
from { fill: green; }
50% { fill: blue; }
to { fill: yellow; }
}")
doc$add_css(
"#myrect {
fill: green;
animation-name: moveit;
animation-duration: 5s;
animation-iteration-count: infinite;
}")
```
<details closed>
<summary> Show SVG text (click to open) </summary>
```{r echo=FALSE}
print(doc)
```
</details> <br />
```{r echo = FALSE}
if (interactive()) {
doc$show()
} else {
doc
}
```
CSS animation - External CSS StyleSheet
==============================================================================
This example uses [animate.css](https://github.com/daneden/animate.css) which
provided a nice range of useful CSS animations.
```{r}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Build a small SVG with a rectangle
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
doc <- svg_doc(width = 240, height = 100)
doc$title("Attribute animation with SMIL")
doc$rect(x=0, y=0, width=240, height = 100, stroke='black', fill='white', stroke_width = 1)
doc$rect(x = 80, y=20, width=80, height=60)$
update(class = "animated infinite bounce delay-2s")
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Add a reference to a CSS URL
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
doc$add_css_url("https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.2/animate.min.css")
```
<details closed>
<summary> Show SVG text (click to open) </summary>
```{r echo=FALSE}
print(doc)
```
</details> <br />
```{r echo = FALSE}
if (interactive()) {
doc$show()
} else {
doc
}
```