var dom = fw.getDocumentDOM(); var sel = [].concat(fw.selection); function TileSelection(){ if(!dom) return; var MAX_WIDTH = dom.width; var elems = [].concat(sel), newElems = []; // use all elements in document if none are selected if(!sel.length){ elems = []; for(var l in dom.layers){ for(var e in dom.layers[l].frames[dom.currentFrameNum].elements){ if(dom.layers[l].frames[dom.currentFrameNum].elements[e].visible) elems.push(dom.layers[l].frames[dom.currentFrameNum].elements[e]); } } } // tile algorithm - create a grid and find grid spots that elements fit it var GRID_SIZE = 10/*MAX_WIDTH/100*/, COLUMNS = Math.floor(MAX_WIDTH/GRID_SIZE), rows = [], r, c; var elem, gridW, gridH, grid; for(var e=0; e COLUMNS) continue; // find the first open grid slot grid = findGrid(gridW,gridH); fw.selection = elem; dom.moveSelectionTo({x:grid.c*GRID_SIZE,y:grid.r*GRID_SIZE},false,false); newElems.push(fw.selection[0]); } fw.selection = newElems; return true; // find an open spot in the grid function findGrid(w,h){ var r = 0, c = 0, currW, currH; // looping through rows, no limit while(true){ currW = currH = 0; // create new blank grid row if necessary if(!rows[r]) rows[r] = new Array(COLUMNS); // loop through columns in current row for(c=0; c= w){ // enough empty columns foind in row (width) // check for available columns [height] rows above currH = 0; while(currH < h){ currH++; if(!rows[r+currH]) return returnGrid('new row'); for(var i=0; i100) return {r:-99,c:-99} // dev - prevent endless loop } // function to mark the current grid slots as taken and return grid location function returnGrid(msg){ //elem.name = msg; for(var i=0; i