Method: Object#fit

Defined in:
lib/atome/extensions/atome.rb

#fit(params) ⇒ Object

def fit(params)

unless params.instance_of?(Hash)
  params = { value: params }
end
target_size = params[:value]
axis = params[:axis]
objet_atome = self
atomes_found = objet_atome.dig
total_width = found_area_used(atomes_found)[:max][:x] - found_area_used(atomes_found)[:min][:x]
total_height = found_area_used(atomes_found)[:max][:y] - found_area_used(atomes_found)[:min][:y]
if axis == :x
  ratio = target_size / total_width
  # now we resize and re-position all atomes
    atomes_found.each do |atome_id|
      current_atome = grab(atome_id)
      current_atome.left(current_atome.left * ratio)
    current_atome.top(current_atome.top * ratio)
    new_width = current_atome.to_px(:width) * ratio
    new_height = current_atome.to_px(:height) * ratio
    current_atome.width(new_width)
    current_atome.height(new_height)
  end
else
  ratio = target_size / total_height
  # now we resize and re-position all atomes

  atomes_found.each do |atome_id|

    current_atome = grab(atome_id)
    # puts "#{current_atome}, #{atome_id}, left:  #{current_atome.left}, ratio: #{ratio}"
    current_atome.left(current_atome.left * ratio)
    current_atome.top(current_atome.top * ratio)
    current_atome.width(current_atome.to_px(:width) * ratio)
    current_atome.height(current_atome.to_px(:height) * ratio)
  end
end

end



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
# File 'lib/atome/extensions/atome.rb', line 520

def fit(params)
  unless params.instance_of?(Hash)
    params = { value: params }
  end

  target_size = params[:value]
  axis = params[:axis]
  objet_atome = self
  atomes_found = objet_atome.dig

  found_area_used(atomes_found) do |result|
    total_width = result[:max][:x] - result[:min][:x]
    total_height = result[:max][:y] - result[:min][:y]

    ratio = if axis == :x
              target_size / total_width
            else
              target_size / total_height
            end

    atomes_found.each do |atome_id|
      current_atome = grab(atome_id)

      left = current_atome.left || 0
      top = current_atome.top || 0

      current_atome.left(left * ratio)
      current_atome.top(top * ratio)
      new_width = current_atome.to_px(:width) * ratio
      new_height = current_atome.to_px(:height) * ratio
      current_atome.width(new_width)
      current_atome.height(new_height)
    end
  end
end