Sha256: 48701b39b2b49c53574c0fbf4b1eeeccbf0bf1bbb7fd44af2a59a60d1ea94679

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module ImageMosaic
  module Image
    module Layout
      # Takes an array of URLs, returns a sliced array of Child images with sizes based on columns
      class Grid
        STARTING_Y = 0

        def initialize(items, width: WIDTH)
          @items = items
          @width = width
        end

        def items(y: STARTING_Y)
          [].tap do |images|
            rows.each do |row|
              x = 0
              row.each do |cell|
                images << cell.merge(y: y, x: x, h: child_dimension, w: child_dimension)
                x += child_dimension
              end
              y += child_dimension
            end
          end
        end

        private

        def rows
          @rows ||= valid_items.each_slice(columns).to_a
        end

        def columns
          Math.sqrt(valid_items.count).floor
        end

        def child_dimension
          @width / columns
        end

        def valid_items
          @valid_items ||= Data::Validator.new(@items).items
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
image_mosaic-0.1.3 lib/image_mosaic/image/layout/grid.rb