-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFill.Rmd
More file actions
executable file
·245 lines (199 loc) · 6.25 KB
/
Fill.Rmd
File metadata and controls
executable file
·245 lines (199 loc) · 6.25 KB
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
---
title: "Fill"
output:
html_document:
toc: true
theme: united
---
```{r, echo=FALSE}
stopifnot(require(svgR, quietly=TRUE))
```
*Fill'er up*
At this point, we have seen several shapes, with the fill attribute specified either as
- "none"
- a color, such as 'red', or '#FF0000' or "rgb(255,0,0)"
However, we can also fill with
- a linear gradient
- a radial gradient
- a pattern
## Fill with a Linear Gradient
As first example, we take an ellipse to fill with a linear gradient.
```{r, echo=T, results="asis"}
colors=c('blue','green','white','orange','red')
svgR(
wh=c(600, 200),
ellipse(cxy=c(250,100), rxy=c(200,80),
fill=linearGradient( xy1=c(0,0), xy2=c(1,0), colors=colors)
)
)
```
The above example invokes the svgR quick approach with fill=lineargradient(...), but this is not standard svg. Standard svg requires a reference to a gradient to be assigned to the fill attribute, i.e. fill="url(#id)" and usually the
gradient definition is placed inside a defs element, as illustrated in the following.
```{r, echo=T, results="asis"}
colors<-c('green','white','orange')
svgR(
wh=c(600, 200),
defs(
linearGradient(id='grad1', xy1=c(0,0), xy2=c(1,0), colors=colors)
),
ellipse(cxy=c(250,100), rxy=c(200,80), fill="url( #grad1 )")
)
```
In the second approach, we can reuse the gradient definition
multiple times referencing the url for each use
```{r, echo=T, results="asis"}
color<-c('green','white','orange')
svgR(
wh=c(600, 200),
defs(
linearGradient(id='grad1', xy1=c(0,0), xy2=c(1,0),colors=colors)
),
ellipse(cxy=c(50,100), rxy=c(20,80), fill="url( #grad1 )"),
ellipse(cxy=c(100,100), rxy=c(20,80), fill="url( #grad1 )"),
ellipse(cxy=c(150,100), rxy=c(20,80), fill="url( #grad1 )"),
ellipse(cxy=c(200,100), rxy=c(20,80), fill="url( #grad1 )"),
ellipse(cxy=c(250,100), rxy=c(20,80), fill="url( #grad1 )")
)
```
However, if we explicity assign an id to the fill=lineargrad(id=...), for the first ellipse, we can also refer to that gradient in the subsequent ellipses
```{r, echo=T, results="asis"}
colors<-c('blue','green','white','orange','red')
svgR(
wh=c(600, 200),
ellipse(cxy=c(50,100), rxy=c(20,80),
fill=linearGradient( id="grad2", xy1=c(0,0), xy2=c(1,0), colors=colors)),
ellipse(cxy=c(100,100), rxy=c(20,80), fill="url( #grad2 )"),
ellipse(cxy=c(150,100), rxy=c(20,80), fill="url( #grad2 )"),
ellipse(cxy=c(200,100), rxy=c(20,80), fill="url( #grad2 )"),
ellipse(cxy=c(250,100), rxy=c(20,80), fill="url( #grad2 )")
)
```
Of course, an elegant approach is to put the ellipses in a group and iterate
```{r, echo=T, results="asis"}
colors<-c('blue','green','yellow','white','orange','red','purple')
svgR(
wh=c(600, 200),
g(
lapply(seq(50,550,by=50), function(x)ellipse(cxy=c(x,100), rxy=c(20,80))),
fill=linearGradient( xy1=c(0,0), xy2=c(1,0), colors=colors)
)
)
```
## Filling with a radial gradient
Filling radial gradient is essentially the same as with a linear gradient
```{r, echo=T, results="asis"}
colors=c('blue','green','white','orange','red')
svgR(
wh=c(600, 200),
ellipse(cxy=c(250,100), rxy=c(200,80),
fill=radialGradient(
colors=colors)
)
)
```
##### Radial Gradient fill with center offset
```{r, echo=T, results="asis"}
colors=c('white', 'green', 'blue')
WH=c(600, 200)
S=190 # Size of square
svgR(
wh=WH,
rect(cxy=WH/2, wh=c(190,190), stroke='black', stroke.width=3,
fill=radialGradient(
cxy=c(.5,.5),
r=.5,
fxy=c(.6,.6)+c(0,0),
colors=colors)
)
)
```
Here the attributes of the radialGradient are:
- cxy The center of the outer circle
- r The radius of the outer circe
- fxy The center of the gradient focus
- colors, an array of gradient colors
All the above units are in relative to the bounding box, which in this case is the dimensions of the rectangle.
Alternativly, we can set the units via
filterUnits="userSpaceOnUse",
##### Radial Gradient fill with center offset
```{r, echo=T, results="asis"}
colors=c('white', 'green', 'blue')
WH=c(600, 200)
S=190 # Size of square
svgR(
wh=WH,
rect(cxy=WH/2, wh=c(S,S), stroke='black', stroke.width=3,
fill=radialGradient( gradientUnits="userSpaceOnUse",
cxy=WH/2,
r=.5*S,
fxy=WH/2+.1*S,
colors=colors)
)
)
```
## Fill with Patterns
We can also fill with a pattern (Although, depending on the your system, you may need to refresh after scrolling)
```{r, echo=T, results="asis"}
WH=c(600, 200) # window rect
svgR( wh=WH,
ellipse( cxy=c(250,100), rxy=c(200,80),
fill=pattern( xy=c(10,10), wh=c(40,40), patternUnits="userSpaceOnUse",
circle(cxy=c(10,10), r=10, fill='blue')
)
)
)
```
Or using defs for the fill
```{r, echo=T, results="asis"}
WH=c(600, 200) # window rect
patternId<-autoId()
patternUrl<-sprintf("url(#%s)",patternId)
svgR( wh=WH,
defs(
pattern(
id=patternId, xy=c(10,10), wh=c(40,40), patternUnits="userSpaceOnUse" ,
circle(cxy=c(10,10), r=10, fill='red')
)
),
ellipse( cxy=c(250,100), rxy=c(200,80), fill=patternUrl)
)
```
And we can compose pattern with gradients:
```{r, echo=T, results="asis"}
WH=c(600, 200) # window rect
colors=c('white','yellow','orange','green', 'blue','black')
svgR( wh=WH,
ellipse( cxy=c(250,100), rxy=c(200,80),
fill=pattern( xy=c(10,10), wh=c(40,40), patternUnits="userSpaceOnUse",
rect(cxy=c(20,20), wh=c(40,40),
fill=radialGradient( colors=colors)
)
)
)
)
```
## Filling text
We can also fill text with gradients and patterns
```{r, echo=T, results="asis"}
WH=c(600, 300) # window rect
colors1=c('white','yellow','orange','green', 'blue','black')
colors2=c('blue','green','orange','yellow','orange', 'green','blue')
svgR( wh=WH,
text( cxy=c(150,100), font.size=300, "R", stroke='green',
fill=pattern( xy=c(0,0), wh=c(20,20), patternUnits="userSpaceOnUse",
text("R", cxy=c(10,10), stroke='red')
)
)
)
```
```{r, echo=T, results="asis"}
WH=c(600, 300) # window rect
svgR( wh=WH,
text( cxy=c(150,100), font.size=300, "R",
fill=radialGradient( colors=colors1)
),
text( cxy=c(420,50), font.size=60, "Programming",
fill=linearGradient(colors=colors2)
)
)
```