/* // get the list of media we want to place var media_data = $(".mediaContainer").toArray().map(function(el){ return $(el).data("media") }) Scenery.randomize( media_data ) */ Scenery.randomize = function (media_data) { var media_list = media_data.map(function(media){ var width, height if (media.width > media.height) { width = Math.min(300, media.width) height = media.height/media.width * width } else { height = Math.min(300, media.height) width = media.width/media.height * height } return { dimensions: new vec2( width, height ), media: media, } }) // get a list of all walls var walls = {} Walls.forEach(function(wall){ walls[wall.id] = wall }) // remove the walls that already have stuff on them Scenery.forEach(function(scenery){ delete walls[scenery.wall.id] }) var wall_ids = _.keys(walls) if (! wall_ids.length) { return } // randomize walls shuffle(wall_ids) // assign each of the media to the walls, until we run out of either media_list.some(function(media){ if (wall_ids.length == 0) { return true } var i, fits = -1 for (i = 0; i < wall_ids.length; i++) { if (walls[wall_ids[i]].surface.fits(media.dimensions)) { // walls[wall_ids[i]] fits = i break } } if (fits != -1) { var wall = walls[wall_ids[fits]] wall_ids.splice(fits, 1) Scenery.add({ media: media.media, wall: wall, index: 0, }) } else { // artwork won't fit anywhere?? } return false }) }