Class: AnimateIt::Tracks::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/animate_it/tracks/document.rb

Overview

Serializable track document consumed by the client runtime.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fps:, duration:) ⇒ Document

Returns a new instance of Document.



9
10
11
12
13
14
15
16
17
18
# File 'lib/animate_it/tracks/document.rb', line 9

def initialize(fps:, duration:)
  @fps = fps
  @duration = duration
  @samples = Hash.new { |hash, group| hash[group] = {} }
  @keyframes = Hash.new { |hash, group| hash[group] = {} }
  @text_samples = {}
  @group_selectors = {}
  @text_selectors = {}
  @layers = []
end

Instance Attribute Details

#durationObject (readonly)

Returns the value of attribute duration.



7
8
9
# File 'lib/animate_it/tracks/document.rb', line 7

def duration
  @duration
end

#fpsObject (readonly)

Returns the value of attribute fps.



7
8
9
# File 'lib/animate_it/tracks/document.rb', line 7

def fps
  @fps
end

Instance Method Details

#add_keyframe_track(group, var, frames:, values:, easing: :ease_out, unit: nil, selector: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/animate_it/tracks/document.rb', line 26

def add_keyframe_track(group, var, frames:, values:, easing: :ease_out, unit: nil, selector: nil)
  @group_selectors[group.to_s] = selector if selector
  @keyframes[group.to_s][normalize_var(var)] = {
    "t" => "kf",
    "k" => frames.map(&:to_i).zip(values),
    "e" => easing.to_s,
    "u" => unit.to_s
  }
end

#add_layer(key, from_frame, to_frame, origin_frame:) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/animate_it/tracks/document.rb', line 42

def add_layer(key, from_frame, to_frame, origin_frame:)
  @layers << {
    "sel" => %([data-animate-layer="#{key}"]),
    "from" => from_frame,
    "to" => to_frame,
    "origin" => origin_frame
  }
end

#as_jsonObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/animate_it/tracks/document.rb', line 51

def as_json
  groups = @samples.transform_values { |vars| vars.transform_values { |track| rle(track) } }
  @keyframes.each { |group, vars| (groups[group] ||= {}).merge!(vars) }

  {
    "v" => 2,
    "fps" => fps,
    "duration" => duration,
    "groups" => groups,
    "groupSelectors" => @group_selectors,
    "texts" => @text_samples.transform_values { |track| rle(fill(track)) },
    "textSelectors" => @text_selectors,
    "layers" => @layers
  }
end

#record_sample(group, var, frame, value, selector: nil) ⇒ Object



20
21
22
23
24
# File 'lib/animate_it/tracks/document.rb', line 20

def record_sample(group, var, frame, value, selector: nil)
  @group_selectors[group.to_s] = selector if selector
  track = @samples[group.to_s][normalize_var(var)] ||= Array.new(duration)
  track[frame] = value&.to_s
end

#record_text(key, frame, value, selector: nil) ⇒ Object



36
37
38
39
40
# File 'lib/animate_it/tracks/document.rb', line 36

def record_text(key, frame, value, selector: nil)
  @text_selectors[key.to_s] = selector if selector
  track = @text_samples[key.to_s] ||= Array.new(duration)
  track[frame] = value.to_s
end

#to_jsonObject



67
68
69
# File 'lib/animate_it/tracks/document.rb', line 67

def to_json(*)
  JSON.generate(as_json)
end