class Castaway::Timeline

Attributes

fps[R]
resolution[R]

Public Class Methods

new(resolution, fps) click to toggle source
# File lib/castaway/timeline.rb, line 9
def initialize(resolution, fps)
  @resolution = resolution
  @elements = []
  @fps = fps

  @cached_command = nil
  @cached_file = nil
end

Public Instance Methods

_log(frame, t, msg) click to toggle source
# File lib/castaway/timeline.rb, line 75
def _log(frame, t, msg)
  _logger.info { format('[%d:%.2fs] %s', frame, t, msg) }
end
_logger() click to toggle source
# File lib/castaway/timeline.rb, line 67
def _logger
  @_logger ||= Logger.new('build.log').tap do |logger|
    logger.formatter = lambda do |severity, _datetime, _progname, msg|
      "#{severity}: #{msg}\n"
    end
  end
end
add(element) click to toggle source
# File lib/castaway/timeline.rb, line 18
def add(element)
  @elements << element
end
duration() click to toggle source
# File lib/castaway/timeline.rb, line 22
def duration
  @elements.map(&:t2).max
end
render_frame(frame, name: 'frame') click to toggle source
# File lib/castaway/timeline.rb, line 26
def render_frame(frame, name: 'frame')
  t = frame / fps.to_f

  signature = nil
  tool = MiniMagick::Tool::Convert.new.tap do |convert|
    convert << '-size' << resolution.to_geometry
    convert.xc 'black'

    @elements.sort_by(&:t1).each do |element|
      next unless element.alive_at?(t)
      element.render_at(t, convert)
    end

    convert.colorspace 'sRGB'
    convert.type 'TrueColor'
    convert.depth '16'

    signature = convert.command
    convert << "PNG48:#{name}.png"
  end

  if signature != @cached_command
    _log frame, t, tool.command.join(' ')
    @cached_command = signature
    @cached_file = name
    tool.call
  else
    old = "#{@cached_file}.png"
    new = "#{name}.png"
    _log frame, t, "duplicate #{old} as #{new}"
    _link old, new
  end
end