Class: Inkcite::Animation

Inherits:
Object
  • Object
show all
Defined in:
lib/inkcite/animation.rb

Defined Under Namespace

Classes: Keyframe

Constant Summary collapse

INFINITE =

Infinite iteration count

'infinite'
LINEAR =

Timing functions

'linear'
EASE =
'ease'
EASE_IN_OUT =
'ease-in-out'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ctx) ⇒ Animation

Returns a new instance of Animation.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/inkcite/animation.rb', line 86

def initialize name, ctx
  @name = name
  @ctx = ctx

  # Default values for the animation's properties
  @duration = 1
  @delay = 0
  @iteration_count = INFINITE
  @timing_function = LINEAR

  # Initialize the keyframes
  @keyframes = []

end

Instance Attribute Details

#ctxObject (readonly)

Animation name, view context and array of keyframes



82
83
84
# File 'lib/inkcite/animation.rb', line 82

def ctx
  @ctx
end

#delayObject

Returns the value of attribute delay.



84
85
86
# File 'lib/inkcite/animation.rb', line 84

def delay
  @delay
end

#durationObject

Returns the value of attribute duration.



84
85
86
# File 'lib/inkcite/animation.rb', line 84

def duration
  @duration
end

#iteration_countObject

Returns the value of attribute iteration_count.



84
85
86
# File 'lib/inkcite/animation.rb', line 84

def iteration_count
  @iteration_count
end

#nameObject (readonly)

Animation name, view context and array of keyframes



82
83
84
# File 'lib/inkcite/animation.rb', line 82

def name
  @name
end

#timing_functionObject

Returns the value of attribute timing_function.



84
85
86
# File 'lib/inkcite/animation.rb', line 84

def timing_function
  @timing_function
end

Instance Method Details

#add_keyframe(percent, styles = {}) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/inkcite/animation.rb', line 101

def add_keyframe percent, styles={}
  keyframe = Keyframe.new(percent, @ctx, styles)

  @keyframes << keyframe

  keyframe
end

#to_keyframe_cssObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/inkcite/animation.rb', line 109

def to_keyframe_css

  css = ''

  # Sort the keyframes by percent in ascending order.
  sorted_keyframes = @keyframes.sort { |kf1, kf2| kf1.percent <=> kf2.percent }

  # Iterate through each prefix and render a set of keyframes
  # for each.
  @ctx.prefixes.each do |prefix|
    css << "@#{prefix}keyframes #{@name} {\n"
    css << sorted_keyframes.collect { |kf| kf.to_css(prefix) }.join("\n")
    css << "\n}\n"
  end

  css

end

#to_sObject

Renders this Animation declaration in the syntax defined here developer.mozilla.org/en-US/docs/Web/CSS/animation e.g. “3s ease-in 1s 2 reverse both paused slidein”



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/inkcite/animation.rb', line 131

def to_s

  # The desired format is: duration | timing-function | delay |
  # iteration-count | direction | fill-mode | play-state | name
  # Although currently not all attributes are supported.
  css = [
      seconds(@duration),
      @timing_function
  ]

  css << seconds(@delay) if @delay > 0

  css << @iteration_count

  css << @name

  css.join(' ')
end