Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Bricks
- constructor: (@parent_id, @col_width = 300) ->
- @_ctx = $("#" + parent_id)
- @_ctx.css "position", "relative"
- @_items = []
- @rebuild()
- appendElement: (element, width, height) ->
- item = {e:element, w:width, h:height}
- @_items.push item
- @_addItem item
- prependElement: (element, width, height) ->
- item = {e:element, w:width, h:height}
- @_items.unshift item
- @rebuild()
- removeElement: (index) ->
- @_items.splice index, 1
- @rebuild()
- clear: ->
- @_ctx.html ""
- @_items = []
- rebuild: ->
- ctx_w = @_ctx.width()
- cols = Math.ceil(ctx_w / @col_width)
- @_col_w = Math.floor(ctx_w / cols)
- @_col_chl = []
- for n in [0..(cols - 1)]
- @_col_chl[n] = 0
- @_ctx.html ""
- for n in @_items
- @_addItem n
- _addItem: (item) ->
- rh = Math.ceil(@_col_w * (item.h/item.w))
- x = 0
- y = 100000000
- xi = 0
- for c in @_col_chl
- if c < y
- y = c
- xi = x
- x++
- x = @_col_w * xi
- @_col_chl[xi] += rh
- item.e.css "position", "absolute"
- item.e.css "width", @_col_w + "px"
- item.e.css "height", rh + "px"
- item.e.css "top", y + "px"
- item.e.css "left", x + "px"
- @_ctx.append item.e
Add Comment
Please, Sign In to add comment