/
javascript-in-svg.Rmd
112 lines (91 loc) · 2.56 KB
/
javascript-in-svg.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
---
title: "javascript-in-svg"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{javascript-in-svg}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = " "
)
```
```{r setup}
library(minisvg)
```
## Add Javascript code for a `<script>` block
```{r}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create a document with a single blue rectangle
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
doc <- svg_doc(width = 200, height = 200)
doc$rect(
id = "myrect",
x = 10,
y = 10,
width = 180,
height = 180,
fill = 'blue'
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Add javascript to edit the DOM and change the fill colour to blue
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
doc$add_js_code("
var element = document.getElementById('myrect');
element.setAttributeNS(null, 'fill', 'green');
console.log('hi {minisvg}');
")
doc$save("svg/javascript-01.svg")
```
```{r echo=FALSE}
print(doc)
```
```{r, echo=FALSE}
htmltools::includeHTML("svg/javascript-01.svg")
```
## Link to external Javascript
```{r}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create a document with a single blue rectangle
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
doc2 <- svg_doc(width = 200, height = 200, id='svg2')
doc2$rect(
id = "myrect2",
x = 10,
y = 10,
width = 180,
height = 180,
fill = 'blue'
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Link to the D3.js library
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
doc2$add_js_url("https://d3js.org/d3.v5.min.js")
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Add d3.js code to change the fill colour to red
# and add a new rectangle
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
doc2$add_js_code("
console.log('start {minisvg}');
var rect = d3.select('#myrect2');
rect.attr('fill', 'red');
var doc = d3.select('#svg2');
doc.append('rect')
.attr('x', 0)
.attr('y', 0)
.attr('width', 50)
.attr('height', 50)
.attr('fill', 'blue')
.attr('stroke', 'black');
console.log('end {minisvg}');
")
doc2$save("svg/javascript-02.svg")
```
```{r echo=FALSE}
print(doc2)
```
```{r, echo=FALSE}
htmltools::includeHTML("svg/javascript-02.svg")
```