Module: AnimateIt::TextEffects

Included in:
Scene
Defined in:
lib/animate_it/text_effects.rb

Overview

Declarative word-by-word headline reveals that can be recorded as compact keyframe tracks while retaining server-rendered fallback values.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
# File 'lib/animate_it/text_effects.rb', line 5

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#reveal_group(key) ⇒ Object



83
84
85
# File 'lib/animate_it/text_effects.rb', line 83

def reveal_group(key)
  "textfx-#{key.to_s.tr("_", "-")}"
end

#reveal_plain_words(key) ⇒ Object



78
79
80
81
# File 'lib/animate_it/text_effects.rb', line 78

def reveal_plain_words(key)
  spec = self.class.word_reveals_registry.fetch(key.to_sym)
  safe_join(spec[:text].split.map { |word| tag.span(word) }, " ")
end

#reveal_words(key) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/animate_it/text_effects.rb', line 53

def reveal_words(key)
  spec = self.class.word_reveals_registry.fetch(key.to_sym)
  static = word_reveal_tracks(key).to_h do |track|
    value = interpolate(
      local_frame,
      track[:frames],
      track[:values],
      easing: :ease_out,
      extrapolate_left: :clamp,
      extrapolate_right: :clamp
    ).round(4)
    [track[:var].to_sym, "#{value}#{track[:unit]}"]
  end

  spans = spec[:text].split.each_with_index.map do |word, index|
    tag.span(word, style: reveal_word_style(spec, key, index))
  end

  tag.span(
    safe_join(spans, " "),
    data: { animate_vars: reveal_group(key) },
    style: Style.build("display: contents", Style.vars(**static))
  )
end

#word_reveal_tracks(key) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/animate_it/text_effects.rb', line 28

def word_reveal_tracks(key)
  spec = self.class.word_reveals_registry.fetch(key.to_sym)
  base = resolve_reveal_start(spec)
  spec[:text].split.each_with_index.flat_map do |_word, index|
    from = base + (index * spec[:stagger])
    to = from + spec[:dur]
    if spec[:kind] == :punch
      [
        { var: "#{key}-w#{index}-op", frames: [from, to], values: [0, 1], unit: "" },
        {
          var: "#{key}-w#{index}-sc",
          frames: [from, from + (spec[:dur] * 0.6).round, to],
          values: [1.3, 1.06, 1.0],
          unit: ""
        }
      ]
    else
      [
        { var: "#{key}-w#{index}-op", frames: [from, to], values: [0, 1], unit: "" },
        { var: "#{key}-w#{index}-y", frames: [from, to], values: [spec[:rise], 0], unit: "px" }
      ]
    end
  end
end