/
imageCloud.js
568 lines (527 loc) · 16.4 KB
/
imageCloud.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/**
* Copyright (c) 2011 Mike Kent
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function($) {
$.fn.imageCloud = function(options) {
// some default settings
var settings = {
width : 300, // pixel width of display box
height : 300, // pixel height of display box
rows : 7, // rows of images (at most)
cols : 7, // cols of images (at most)
spacing : 5, // pixel spacing between images
maxSize : 5, // maximum block an image can take
imgcls : 'cloudImg', // class to apply to image divs
el : this, // this element
fadeDuration: 1000, // effect duration
imageDelay : 500, // delay between add/remove image
url : '/imglist.json' // url of list of images
};
var images = []; // default images list, expecting:
// [url] or [{url,weight}]
var curBlocks = [], // current layouts of images
nextBlocks = [], // next layout of images
displayField = [], // 2d boolean array (cell occupied?)
queue = [];
// override default settings
if(options) {
if(typeof options === "object") {
$.extend(settings, options);
} else if(typeof options === "string") {
settings.url = options;
}
}
// setup `this` container
this.css({
width : settings.width,
height: settings.height
});
// setup block dimensions
settings.blockWidth =
Math.floor(
settings.width -
((settings.rows + 1) * settings.spacing)
) / settings.rows;
settings.blockHeight =
Math.floor(
settings.height -
((settings.cols + 1) * settings.spacing)
) / settings.cols;
// get images list, then start everything
$.get(
settings.url,
function(data, status) {
images = data;
run();
},
'json'
);
/**
* runs everything once image list is gotten.
* Inits display field, runs first add of all images,
* then cycles add/remove of images
*/
function run() {
// init displayField
for(var i=0;i<settings.rows;i++) {
displayField[i] = [];
for(var j=0;j<settings.cols;j++) {
displayField[i][j] =
{
// upper right coordinate
ur:
{
x: (settings.blockWidth + settings.spacing) * j
+ settings.spacing,
y: (settings.blockHeight + settings.spacing) * i
+ settings.spacing
},
// lower left coordinate
ll:
{
x: (settings.blockWidth + settings.spacing) * j
+ settings.spacing + settings.blockWidth,
y: (settings.blockHeight + settings.spacing) * i
+ settings.spacing + settings.blockHeight
},
occupied: false
};
}
}
// get first arragement
curBlocks = genRandomBlocks();
// iterations
enqueue(0,nextIter);
// perform iterations
function nextIter() {
nextBlocks = genRandomBlocks();
// start add/remove process
enqueue(0,addRemove);
/**
* First remove an image if there are any to remove,
* then add an image if there is space and continue until
* all of nextBlocks have been used, then run nextIter() again.
*/
function addRemove() {
// remove images if possible
if(curBlocks.length > 0 && anyRemovableBlocks()) {
enqueue(
settings.imageDelay,
function() {
removeImage(curBlocks, displayField);
}
);
}
// add images if possible
var nextBlock = getLargestAddableBlock(nextBlocks, displayField);
if(nextBlock !== null) {
enqueue(
settings.imageDelay,
function() {
addImage(nextBlock, displayField);
}
);
}
// continue addRemove() or start another iteration
if(anyAddableBlocks()) {
enqueue(0,addRemove);
} else {
curBlocks = nextBlocks;
enqueue(0,nextIter);
}
// search for removeable blocks
function anyRemovableBlocks() {
return testAny(
curBlocks, function(block) {
return block.visible;
}
);
}
// search for addable blocks
function anyAddableBlocks() {
return testAny(
nextBlocks, function(block) {
return !block.visible;
}
);
}
}
}
}
/**
* adds an image if possible
*
* returns true if possible
* false otherwise
*/
function addImage(block, displayField) {
// return if we cannot find a valid block to add
if(block === null) {
return;
}
var coords = block.coords;
block.visible = true;
// make div accessable in block
var urcell = coords[0],
llcell = coords[1];
urcell = displayField[urcell.r][urcell.c].ur;
llcell = displayField[llcell.r][llcell.c].ll;
var width = llcell.x - urcell.x,
height = llcell.y - urcell.y;
// create image element
var img = $("<image>",
{
src: chooseRandom(images)
}
);
// get dimensions of the image
var imgWidth = img.attr("width"),
imgHeight = img.attr("height");
// change the smaller value to fit unless the block size (width or
// height) is more than 1.6 times that of the other.
if(imgWidth < imgHeight || width/height >= 1.6) {
var y =
img.css(
{
width: width
}
);
} else {
img.css(
{
height: height
}
);
}
// make element accessable in block
block.el =
$("<div>",
{
"class" : settings.imgcls
}
).css(
{
left : urcell.x,
top : urcell.y,
width : llcell.x - urcell.x,
height : llcell.y - urcell.y,
overflow: "hidden",
display : "none"
}
)
.append(img)
.appendTo(settings.el)
.fadeIn(settings.fadeDuration);
// set display field as occupied
for(var r = coords[0].r; r <= coords[1].r; r++) {
for(var c = coords[0].c; c <= coords[1].c; c++) {
displayField[r][c].occupied = true;
}
}
}
/**
* get the largest addable block
*/
function getLargestAddableBlock(blocks, displayField) {
var maxArea = 0, maxIndex = -1, area, coords;
for(var i=0;i<blocks.length;i++) {
if(!blocks[i].visible &&
isBlockAddable( blocks[i], displayField )
)
{
coords = blocks[i].coords;
area = (coords[1].r - coords[0].r + 1) *
(coords[1].c - coords[0].c + 1);
if(area > maxArea) {
maxArea = area;
maxIndex = i;
}
}
}
if(maxIndex >= 0)
return blocks[maxIndex];
return null;
// check that the all cells the block would occupy are empty
function isBlockAddable(block, displayField) {
var coords = block.coords;
for(var r = coords[0].r; r <= coords[1].r; r++) {
for(var c = coords[0].c; c <= coords[1].c; c++) {
if(displayField[r][c].occupied)
return false;
}
}
return true;
}
}
/**
* get the first block that is addable
*/
function getFirstAddableBlock(blocks, displayField) {
for(var i=0;i<blocks.length;i++) {
if(!blocks[i].visible &&
isBlockAddable( blocks[i], displayField )
)
{
return blocks[i];
}
}
return null;
// check that the all cells the block would occupy are empty
function isBlockAddable(block, displayField) {
var coords = block.coords;
for(var r = coords[0].r; r <= coords[1].r; r++) {
for(var c = coords[0].c; c <= coords[1].c; c++) {
if(displayField[r][c].occupied)
return false;
}
}
return true;
}
}
/**
* removes an image
*/
function removeImage(blocks, displayField) {
var block = getFirstRemovableBlock(blocks),
coords = block.coords;
// set block as not visible
block.visible = false;
// fade out coord and remove from DOM
block.el.fadeOut(settings.fadeDuration, function() {
$(this).remove();
});
// remove from displayField
for(var r = coords[0].r; r <= coords[1].r; r++) {
for(var c = coords[0].c; c <= coords[1].c; c++) {
displayField[r][c].occupied = false;
}
}
// returns the first block which is visible
function getFirstRemovableBlock(blocks) {
for(var i=0;i<blocks.length;i++) {
if(blocks[i].visible) {
return blocks[i];
}
}
}
}
/**
* adds to the running queue
*
* uses running variable to determine if the queue needs to be
* started or is already running.
*/
var running = false;
function enqueue(delay,fn) {
queue.push({delay:delay,fn:fn});
startQueue();
// starts the queue if it is not already running
function startQueue() {
if(running)
return;
running = true;
setTimeout(doNext,queue[0].delay);
function doNext() {
queue[0].fn();
removeItem(queue,0);
if(queue.length>0) {
setTimeout(doNext,queue[0].delay);
} else
running = false;
}
}
}
/**
* generates a new sequence of blocks to place
*
* returns blocks in the following form:
* [
* {
* coords,
* visible:false,
* }
* ]
*/
function genRandomBlocks() {
var newDisplayField = [], blocks = [];
for(var i=0;i<settings.rows;i++) {
newDisplayField[i] = [];
for(var j=0;j<settings.cols;j++) {
newDisplayField[i][j] = false;
}
}
var openPositions = getOpenPositions(newDisplayField);
while(openPositions.length > 0) {
// pick a random cell to start in
var position = chooseRandom(openPositions);
var coords = [
$.extend({},position), //clone position to new object
$.extend({},position)
];
// directions to expand the block
var directions = ['up','up','down','down','left','left,',
'right','right'];
// then expand it randomly
var iters = randInt(3,8); // random number of modifications
for(;iters>0;iters--) {
expand(
coords,
newDisplayField,
chooseRandom(directions,true)
);
}
// occupy the field based on the block
for(var r = coords[0].r; r <= coords[1].r; r++) {
for(var c = coords[0].c; c <= coords[1].c; c++) {
newDisplayField[r][c] = true;
}
}
// add the new block to the list of blocks
blocks.push({coords:coords,visible:false});
// refresh the list of open positions
openPositions = getOpenPositions(newDisplayField);
}
return blocks;
// get a list of open positions
function getOpenPositions(displayField) {
positions = [];
for(var i=0;i<settings.rows;i++) {
for(var j=0;j<settings.cols;j++) {
if(!displayField[i][j])
positions.push({r:i,c:j});
}
}
return positions;
}
// only expands a block if it possible
function expand(coords,displayField,direction) {
if(
canExpand(
getExpansionCells(coords,direction),
displayField)
)
{
if(direction == 'up') {
coords[0].r -= 1;
} else if(direction == 'down') {
coords[1].r += 1;
} else if(direction == 'left') {
coords[0].c -= 1;
} else if(direction == 'right') {
coords[1].c += 1;
}
}
}
// returns true if none of `cells` are occupied in `displayField`
function canExpand(coords,displayField) {
var coord;
for(var i=0;i<coords.length;i++) {
coord = coords[i];
if(coord.r < 0 || coord.r >= settings.rows ||
coord.c < 0 || coord.c >= settings.cols)
{
return false; //cannot expand to there
}
if(displayField[coord.r][coord.c]) {
return false; // that coord is occupied
}
}
return true; // there are no coords occupied
}
// get the new cells that an expansion would include
function getExpansionCells(coord,direction) {
var rowStart, rowEnd, colStart, colEnd, cells = [];
if(direction == 'up') {
rowStart = rowEnd = coord[0].r - 1;
colStart = coord[0].c;
colEnd = coord[1].c;
} else if(direction == 'down') {
rowStart = rowEnd = coord[1].r + 1;
colStart = coord[0].c;
colEnd = coord[1].c;
} else if(direction == 'left') {
colStart = colEnd = coord[0].c - 1;
rowStart = coord[0].r;
rowEnd = coord[1].r;
} else if(direction == 'right') {
colStart = colEnd = coord[1].c + 1;
rowStart = coord[0].r;
rowEnd = coord[1].r;
}
for(;rowStart <= rowEnd;rowStart++) {
for(var c = colStart;c <= colEnd;c++) {
cells.push({r:rowStart,c:c});
}
}
return cells;
}
}
/**
* tests to see if any element in array is true via fn
*/
function testAny(arr, fn) {
for(var i=0;i<arr.length;i++) {
if(fn(arr[i])) {
return true;
}
}
return false;
}
/**
* implements reduce as specified in most map/reduce implementations
*/
function reduce(arr,fn) {
var newArr = [];
for(var i=0; i < arr.length; i++) {
if(fn(arr[i])) {
newArr.push(arr[i]);
}
}
return newArr;
}
/**
* removes an item from an array.
* source: www.ejohn.org/blog/javascript-array-remove
*/
function removeItem(arr, from, to) {
var rest = arr.slice((to || from) + 1 || arr.length);
arr.length = from < 0 ? arr.length + from : from;
return arr.push.apply(arr, rest);
}
/**
* choose a randome element from a list
*/
function chooseRandom(list,deleteItem) {
var index = randInt(0,list.length-1),
item = list[index];
if(deleteItem === true)
removeItem(list,index);
return item;
}
/**
* get a random int in range
*/
function randInt(low,high) {
return Math.floor(Math.random()*(high-low+1)+low);
}
};
})(jQuery);