/
filter-effects.Rmd
279 lines (202 loc) · 6.05 KB
/
filter-effects.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
---
title: "filter-effects"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{filter-effects}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r setup}
library(minisvg)
```
## Example images
Using the example image from the [MDN site](https://developer.mozilla.org). For
these vignettes, I'm going to use an inline version of the image for future-proofing.
<img src="images/mdn_logo.png" width="25%">
```{r}
inline_img <- paste0("data:image/png;base64,", base64enc::base64encode("./images/mdn_logo.png"))
```
## `feBlend`
The `<feBlend>` SVG filter primitive composes two objects together ruled by a certain blending mode. This is similar to what is known from image editing software when blending two layers. The mode is defined by the mode attribute.
Example from [MDN docs](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/feBlend).
```{r}
doc <- svg_doc(
width = 200, height = 200,
stag$defs(
stag$filter(
id="spotlight",
stag$feFlood(
x=0, y=0,
result = "floodFill",
width = "100%",
height = "100%",
flood_color = "green",
flood_opacity = "1"
),
stag$feBlend(
in_ = "SourceGraphic", in2="floodFill", mode="multiply"
)
)
),
stag$image(
xlink_href = inline_img,
x="10%", y="10%", width="80%", height="80%",
style="filter:url(#spotlight);"
)
)
```
<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
}
```
## `feColorMatrix`
The `<feColorMatrix>` SVG filter element changes colors based on a transformation matrix. Every pixel's color value (represented by an [R,G,B,A] vector) is matrix multiplied to create a new color.
Example from [MDN docs](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/feColorMatrix).
```{r}
doc <- svg_doc(
width="40%", height="40%", viewBox="0 0 150 250",
preserveAspectRatio = "xMidYMid meet",
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 3 overlapping circles
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
stag$defs(
stag$g(
id="circles",
stag$circle(cx=30, cy=30, r=20, fill="blue" , fill_opacity="0.5"),
stag$circle(cx=20, cy=50, r=20, fill="green", fill_opacity="0.5"),
stag$circle(cx=40, cy=50, r=20, fill="red" , fill_opacity="0.5")
)
),
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Reference rendering of the circles
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
stag$use(href='#circles'),
stag$text(x=70, y=50, "Reference"),
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Applying a colour transformation matrix to these circles
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
stag$filter(
id="colorMeMatrix",
stag$feColorMatrix(
in_ = "SourceGraphic",
type = "matrix", # matrix | saturate | hueRotate | luminanceToAlpha
values = "0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 0"
)
),
stag$use(href='#circles', transform="translate(0 70)", filter="url(#colorMeMatrix)"),
stag$text(x=70, y=120, "Matrix"),
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Desaturate the colour of the circles
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
stag$filter(
id = "colorMeSaturate",
stag$feColorMatrix(
in_ = "SourceGraphic",
type = "saturate",
values = "0.2"
)
),
stag$use(href='#circles', transform="translate(0 140)", filter="url(#colorMeSaturate)"),
stag$text(x=70, y=190, "Saturate")
)
```
<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
}
```
## `feTurbulence`
The `<feTurbulence>` SVG filter primitive creates an image using the Perlin turbulence function. It allows the synthesis of artificial textures like clouds or marble. The resulting image will fill the entire filter primitive subregion.
Example from [MDN docs](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/feTurbulence).
```{r}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Define filter with turbulence driving the displacmenet
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
my_filter <- stag$filter(
id = "displacementFilter",
stag$feTurbulence(
type = "turbulence",
baseFrequency = 0.05,
numOctaves = 2,
result = "turbulence"),
stag$feDisplacementMap(
in_ = "SourceGraphic",
in2 = "turbulence",
scale = 50,
xChannelSelector = 'R',
yChannelSelector = 'G')
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Apply this displacement filter to a circle
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
doc <- svg_doc(
width = 200, height = 200,
my_filter,
stag$circle(cx=100, cy=100, r=80, filter = my_filter)
)
```
<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
}
```
## `feTurbulence` - simple example
```{r}
doc <- svg_doc(
width = 250, height = 250,
stag$filter(
id = "noise",
stag$feTurbulence(baseFrequency = 0.05)
),
stag$rect(
x = 0,
y = 0,
height = 250,
width = 250,
filter = "url(#noise)",
fill = 'none')
)
```
<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
}