Class: Dommy::Animation

Inherits:
Object
  • Object
show all
Includes:
Bridge::Methods, 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 Bridge::Methods

included

Methods included from EventTarget

#__internal_deliver_event__, #add_event_listener, capture_flag, #deliver_at, #dispatch_event, js_truthy?, #remove_event_listener

Constructor Details

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

Returns a new instance of Animation.



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

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.



91
92
93
# File 'lib/dommy/animation.rb', line 91

def effect
  @effect
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#play_stateObject (readonly)

Returns the value of attribute play_state.



91
92
93
# File 'lib/dommy/animation.rb', line 91

def play_state
  @play_state
end

#timelineObject (readonly)

Returns the value of attribute timeline.



91
92
93
# File 'lib/dommy/animation.rb', line 91

def timeline
  @timeline
end

Instance Method Details

#__internal_event_parent__Object

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



255
256
257
# File 'lib/dommy/animation.rb', line 255

def __internal_event_parent__
  nil
end

#__js_call__(method, args) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/dommy/animation.rb', line 233

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], args[2])
  when "dispatchEvent"
    dispatch_event(args[0])
  end
end

#__js_get__(key) ⇒ Object



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

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



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/dommy/animation.rb', line 212

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
  else
    return Bridge::UNHANDLED
  end

  nil
end

#cancelObject



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

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

#current_timeObject



107
108
109
# File 'lib/dommy/animation.rb', line 107

def current_time
  @current_time
end

#current_time=(value) ⇒ Object



111
112
113
# File 'lib/dommy/animation.rb', line 111

def current_time=(value)
  @current_time = value
end

#finishObject



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

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.



175
176
177
# File 'lib/dommy/animation.rb', line 175

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

#pauseObject



143
144
145
146
147
# File 'lib/dommy/animation.rb', line 143

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

#playObject

Start (or resume) the animation. Returns self.



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

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



123
124
125
# File 'lib/dommy/animation.rb', line 123

def playback_rate
  @playback_rate
end

#playback_rate=(value) ⇒ Object



127
128
129
# File 'lib/dommy/animation.rb', line 127

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).



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

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

#reverseObject



167
168
169
170
171
# File 'lib/dommy/animation.rb', line 167

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

#start_timeObject



115
116
117
# File 'lib/dommy/animation.rb', line 115

def start_time
  @start_time
end

#start_time=(value) ⇒ Object



119
120
121
# File 'lib/dommy/animation.rb', line 119

def start_time=(value)
  @start_time = value
end