-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollage.js
303 lines (260 loc) · 8.89 KB
/
collage.js
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* @file
* @author andre-st
* @since 2018-11-02
*
*/
"use strict";
/**
* Collage Generators
* @namespace
*
*/
const collages =
{
_MSG_ENOINLINESVG: "Sorry, your browser does not support inline SVG.",
/**
* Helper-function to reduce repetitive code / improve overall readability:
* creates an NS element and sets attributes with their corresponding namespace.
*
* @return {Element} new element from the SVG namespace with the given attributes
* @param {String} element name (lowercase)
* @param {Object} attributes
* @private
*/
_createSvg: function( theElementName, theProps )
{
const el = document.createElementNS( "http://www.w3.org/2000/svg", theElementName );
const ns = { "xlink:href": "http://www.w3.org/1999/xlink" };
for( var key in theProps )
el.setAttributeNS( ns[key] || null, key, theProps[key] );
if( theElementName == "svg" )
el.appendChild( document.createTextNode( collages._MSG_ENOINLINESVG ));
return el;
},
/**
* Generates source-material for a collage
* @namespace
*
*/
materials:
{
/**
* @example new collages.materials.Color( "#AABBCC" )
* @param {String} color hex-string
* @return {collages.materials.Color} instance
* @public
*/
Color: function( theColorHex )
{
this.color = theColorHex;
this.toPattern = function( theId, theWidth, theHeight )
{
const bpat = collages._createSvg( "pattern", {
id : theId,
class : "bpat bpatColor",
patternUnits : "userSpaceOnUse", // Absolute coords
width : theWidth,
height : theHeight
});
return bpat;
};
},
/**
* @example new collages.materials.Image( "myimage.jpg" )
* @param {String} the image url
* @return {collages.materials.Image} instance
* @public
*/
Image: function( theImageUrl )
{
this.imageUrl = theImageUrl;
this.toPattern = function( theId, theWidth, theHeight )
{
const bpat = collages._createSvg( "pattern", {
id : theId,
class : "bpat bpatImage",
patternUnits : "userSpaceOnUse", // Absolute coords
width : theWidth,
height : theHeight
});
const bimg = collages._createSvg( "image", {
class : "bimg",
x : 0,
y : 0,
width : theWidth,
height : theHeight,
"xlink:href" : this.imageUrl,
preserveAspectRatio : "xMidYMid slice"
});
bpat.appendChild( bimg );
return bpat;
};
},
},
/**
* Collage inspired by Jelle Marten's series "In The Quivering Forest":
*
* "A typical photograph from Jelle Martens would normally
* include a geometrical triangle pattern with
* one line of triangles being of a very vibrant scene such as a forest and the
* other lines of triangles being a gradient in an opposite colour of the opposing triangles.
* This creates contrast within the image and it makes the image eye-catching.
*
* The original photographs look very natural with the exception of some
* although this doesn't matter too much as they are then transformed in
* to almost a collage of images to create Jelle Marten's desired effect.
*
* This image in particular uses multiple images instead of just two and
* the images are of all different colours and I believe some are even
* rotated."
*
* - no white-tile points to the left:
* being the heaviest tile it gets everything a reading direction/flow to the right,
* also emphasises the triangle geometries even more;
* tells the brain how to read this thing;
* to both sides it would give it another character, not so triangular anymore;
* i recognized this as i did it wrong the first time (pointing to both directions);
* these triangles are anchor points; although it seems not a rule
* in other collages of Martens, it's the catchy thing with QuiveringForest imho
*
* - white-tiles become fewer to the right side,
* constituting some kind of triangle again
*
* - white-tiles use a (horizontally defined) cardboard paper texture
* (material, not just color)
*
* - triangles = stability, energy, aggression;
* rhombus (2 triangles) = vibrant and contemporary
*
* - equilateral triangles
*
* - photos: paintings, structured, old teared out worne out photos,
* all colors of all(!) photos harmonize with eachother:
* brown, green, blue, sand, ... (earth/nature colors)
*
* - if sth looks unexpected check load-states of your image material (borwser's network view)
*
*
*
* The algorithm:
*
* The primitive consists of two triangles A and B,
* where A and B can potentially have same fill and thus
* appear as a larger tile among the others;
* not just random triangles but some relation between
* A and B, and B and A'
*
* /|\
* /A|B\ -----> repeated along x-axis
* \ | /|\
* \|/ | \ --> repeated along x-axis
* 1\ | /
* \|/
* 2
*
* next row: x and y moved by +1/2
*
* Triangles are filled with "tile patterns" (tpat),
* which are views into a randomly chosen "base pattern" (bpat),
* which are primarily large images (bimg).
* The white-tile pattern applies to A-triangles only.
* Tile-pattern position is not random but successive.
*
* ____________________________________
* | .-------. |
* | --------------> | tpat3 | bpat2 |
* | '-------' image |
* |____________________________________|
* | .-------.-------. |
* | | tpat1 | tpat2 | ------> bpat1 |
* | '-------'-------' image |
* |____________________________________|
* _ _
* /|\ /|\
* / | \ / | \
* / | \ / |
* / | \ / |
* /tpat|tpat\/tpat| --->
* \ 1 | 2 /\ 3 |
* \ | / \ |
* \ | / \ |
* \ | / \ | /
* \|/ \|/
*
*
*
*
* @param {DOMElement} container block-element
* @param {Array} source materials for the collage:
* same-sized instances of `collages.materials.Image`
* or `collages.materials.Color` (3-5 instances prefered, 4 best?);
* style the white-tiles via `.collage { background-color... }`
* @return {void}
* @pre theMaterials.length > 0
* @public
*/
drawMartens: function( theContainer, theMaterials )
{
if( !theContainer )
throw "drawMartens: Invalid argument for theContainer";
if( !theMaterials || theMaterials.length == 0 )
throw "drawMartens: Invalid argument for theMaterials";
const w = theContainer.clientWidth;
const h = theContainer.clientHeight;
const numTriPerRow = 4;
const numTriPerCol = 4;
const triW = w / numTriPerRow;
const triH = h / numTriPerCol;
var tpatIdx = 0;
const svg = collages._createSvg( "svg", {
class : "collage martens",
width : w,
height : h
});
const def = collages._createSvg( "defs" );
svg.appendChild( def );
for( var i = 0; i < theMaterials.length; i++ ) // Define base patterns:
def.appendChild( theMaterials[i].toPattern( "bpat"+i, w, h ));
for( var y = -triH; y < h; y += triH/2 ) // All the triangles:
{
const xoff = ((y*2)/triH) % 2 ? triW : 0;
for( var x = -triW; x < w; x += triW )
{
const isTriA = x/triW % 2;
var points;
if( isTriA )
{
points = [[ xoff + x, y ],
[ xoff + x + triW, y + triH / 2 ],
[ xoff + x, y + triH ]];
}
else // TriB:
{
points = [[ xoff + x + triW, y + 0 ],
[ xoff + x , y + triH / 2 ],
[ xoff + x + triW, y + triH ]];
}
// Choose a fill:
if( isTriA && Math.random()*(w-x) > w/2 ) continue; // Fewer white-tiles to the right
const bpatIdx = Math.floor( Math.random() * theMaterials.length ); // Random tile-pattern
const tpat = collages._createSvg( "pattern", {
id : "tpat" + tpatIdx++,
"xlink:href" : "#bpat" + bpatIdx, // Base pattern
patternUnits : "userSpaceOnUse", // Absolute coords
viewport : x + "," + y + "," + triW + "," + triH // Window over base pattern
});
const tile = collages._createSvg( "polygon", {
class : "tile",
fill : "url(#" + tpat.id + ")",
points : points[0][0] + "," + points[0][1] + " " + // "x,y x,y ..."
points[1][0] + "," + points[1][1] + " " +
points[2][0] + "," + points[2][1]
});
def.appendChild( tpat );
svg.appendChild( tile );
}
}
theContainer.appendChild( svg );
}
}