Class: Dommy::Animation

Inherits:
Object
  • Object
show all
Includes:
EventTarget
Defined in:
lib/dommy/animation.rb

Overview

‘Animation` — represents a running animation, mirroring the Web Animations API’s lifecycle and event surface. Dommy doesn’t interpolate any property values; the animation transitions through “idle” → “running” → “finished” by either virtual time (‘scheduler.advance_time`) or by an explicit `finish()` call.

Spec: drafts.csswg.org/web-animations/#animation

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventTarget

#__deliver_event__, #add_event_listener, #dispatch_event, #invoke_listener, #remove_event_listener

Constructor Details

#initialize(effect = nil, timeline = nil, window: nil) ⇒ Animation

Returns a new instance of Animation.



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/dommy/animation.rb', line 91

def initialize(effect = nil, timeline = nil, window: nil)
  @effect = effect
  @timeline = timeline
  @window = window
  @play_state = "idle"
  @playback_rate = 1.0
  @current_time = nil
  @start_time = nil
  @id = ""
  @finished_promise = nil
  @ready_promise = nil
  @scheduled_finish_id = nil
end

Instance Attribute Details

#effectObject (readonly)

Returns the value of attribute effect.



89
90
91
# File 'lib/dommy/animation.rb', line 89

def effect
  @effect
end

#idObject

Returns the value of attribute id.



88
89
90
# File 'lib/dommy/animation.rb', line 88

def id
  @id
end

#play_stateObject (readonly)

Returns the value of attribute play_state.



89
90
91
# File 'lib/dommy/animation.rb', line 89

def play_state
  @play_state
end

#timelineObject (readonly)

Returns the value of attribute timeline.



89
90
91
# File 'lib/dommy/animation.rb', line 89

def timeline
  @timeline
end

Instance Method Details

#__event_parent__Object

Event bubbling stops at Animation — it isn’t part of the DOM tree.



247
248
249
# File 'lib/dommy/animation.rb', line 247

def __event_parent__
  nil
end

#__js_call__(method, args) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/dommy/animation.rb', line 225

def __js_call__(method, args)
  case method
  when "play"
    play
  when "pause"
    pause
  when "cancel"
    cancel
  when "finish"
    finish
  when "reverse"
    reverse
  when "addEventListener"
    add_event_listener(args[0], args[1], args[2])
  when "removeEventListener"
    remove_event_listener(args[0], args[1])
  when "dispatchEvent"
    dispatch_event(args[0])
  end
end

#__js_get__(key) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/dommy/animation.rb', line 187

def __js_get__(key)
  case key
  when "playState"
    @play_state
  when "playbackRate"
    @playback_rate
  when "currentTime"
    @current_time
  when "startTime"
    @start_time
  when "effect"
    @effect
  when "timeline"
    @timeline
  when "finished"
    finished
  when "ready"
    ready
  when "id"
    @id
  end
end

#__js_set__(key, value) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/dommy/animation.rb', line 210

def __js_set__(key, value)
  case key
  when "currentTime"
    @current_time = value
  when "startTime"
    @start_time = value
  when "playbackRate"
    @playback_rate = value.to_f
  when "id"
    @id = value.to_s
  end

  nil
end

#cancelObject



147
148
149
150
151
152
153
154
# File 'lib/dommy/animation.rb', line 147

def cancel
  cancel_scheduled_finish
  @play_state = "idle"
  @current_time = nil
  reject_finished_with_abort
  dispatch_event(Event.new("cancel"))
  self
end

#current_timeObject



105
106
107
# File 'lib/dommy/animation.rb', line 105

def current_time
  @current_time
end

#current_time=(value) ⇒ Object



109
110
111
# File 'lib/dommy/animation.rb', line 109

def current_time=(value)
  @current_time = value
end

#finishObject



156
157
158
159
160
161
162
163
# File 'lib/dommy/animation.rb', line 156

def finish
  cancel_scheduled_finish
  @play_state = "finished"
  @current_time = effect_duration_ms
  resolve_finished
  dispatch_event(Event.new("finish"))
  self
end

#finishedObject

PromiseValue that resolves when the animation finishes. Rejected (with AbortError-style RuntimeError) on cancel.



173
174
175
# File 'lib/dommy/animation.rb', line 173

def finished
  @finished_promise ||= PromiseValue.new(@window)
end

#pauseObject



141
142
143
144
145
# File 'lib/dommy/animation.rb', line 141

def pause
  cancel_scheduled_finish
  @play_state = "paused" unless @play_state == "idle"
  self
end

#playObject

Start (or resume) the animation. Returns self.



130
131
132
133
134
135
136
137
138
139
# File 'lib/dommy/animation.rb', line 130

def play
  return self if @play_state == "running"

  previous = @play_state
  @play_state = "running"
  @start_time ||= @window&.scheduler&.now_ms || 0
  ensure_ready_resolved
  schedule_auto_finish if previous != "paused"
  self
end

#playback_rateObject



121
122
123
# File 'lib/dommy/animation.rb', line 121

def playback_rate
  @playback_rate
end

#playback_rate=(value) ⇒ Object



125
126
127
# File 'lib/dommy/animation.rb', line 125

def playback_rate=(value)
  @playback_rate = value.to_f
end

#readyObject

PromiseValue that resolves once the animation is ready to play (immediately in Dommy — there’s no render-thread handoff).



179
180
181
182
183
184
185
# File 'lib/dommy/animation.rb', line 179

def ready
  @ready_promise ||= if @window
    PromiseValue.resolve(@window, self)
  else
    PromiseValue.new(@window)
  end
end

#reverseObject



165
166
167
168
169
# File 'lib/dommy/animation.rb', line 165

def reverse
  @playback_rate = -@playback_rate
  play if @play_state == "idle"
  self
end

#start_timeObject



113
114
115
# File 'lib/dommy/animation.rb', line 113

def start_time
  @start_time
end

#start_time=(value) ⇒ Object



117
118
119
# File 'lib/dommy/animation.rb', line 117

def start_time=(value)
  @start_time = value
end