1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 * @extends {WebInspector.SDKModel}
9 * @param {!WebInspector.Target} target
11 WebInspector
.AnimationModel = function(target
)
13 WebInspector
.SDKModel
.call(this, WebInspector
.AnimationModel
, target
);
14 this._agent
= target
.animationAgent();
15 target
.registerAnimationDispatcher(new WebInspector
.AnimationDispatcher(this));
16 /** @type {!Map.<string, !WebInspector.AnimationModel.Animation>} */
17 this._animationsById
= new Map();
18 /** @type {!Map.<string, !WebInspector.AnimationModel.AnimationGroup>} */
19 this._animationGroups
= new Map();
20 /** @type {!Array.<string>} */
21 this._pendingAnimations
= [];
24 WebInspector
.AnimationModel
.Events
= {
25 AnimationCreated
: "AnimationCreated",
26 AnimationCanceled
: "AnimationCanceled"
29 WebInspector
.AnimationModel
.prototype = {
33 animationCreated: function(id
)
35 this._pendingAnimations
.push(id
);
39 * @param {!AnimationAgent.Animation} payload
41 animationStarted: function(payload
)
43 var animation
= WebInspector
.AnimationModel
.Animation
.parsePayload(this.target(), payload
);
44 this._animationsById
.set(animation
.id(), animation
);
46 for (var id
of this._pendingAnimations
) {
47 if (!this._animationsById
.get(id
))
51 while (this._pendingAnimations
.length
) {
52 var group
= this._createGroupFromPendingAnimations();
53 this._animationGroups
.set(group
.id(), group
);
54 // TODO(samli): Dispatch single group event.
55 for (var anim
of group
.animations())
56 this.dispatchEventToListeners(WebInspector
.AnimationModel
.Events
.AnimationCreated
, { "player": anim
, "resetTimeline": anim
.id() === group
.id() });
61 * @return {!WebInspector.AnimationModel.AnimationGroup}
63 _createGroupFromPendingAnimations: function()
65 console
.assert(this._pendingAnimations
.length
);
66 var groupedAnimations
= [this._animationsById
.get(this._pendingAnimations
.shift())];
67 var remainingAnimations
= [];
68 for (var id
of this._pendingAnimations
) {
69 var anim
= this._animationsById
.get(id
);
70 if (anim
.startTime() === groupedAnimations
[0].startTime())
71 groupedAnimations
.push(anim
);
73 remainingAnimations
.push(id
);
75 this._pendingAnimations
= remainingAnimations
;
76 return new WebInspector
.AnimationModel
.AnimationGroup(this.target(), groupedAnimations
[0].id(), groupedAnimations
);
82 animationCanceled: function(id
)
84 this.dispatchEventToListeners(WebInspector
.AnimationModel
.Events
.AnimationCanceled
, { "id": id
});
88 * @param {number} playbackRate
90 setPlaybackRate: function(playbackRate
)
92 this._agent
.setPlaybackRate(playbackRate
);
95 ensureEnabled: function()
100 this._enabled
= true;
103 __proto__
: WebInspector
.SDKModel
.prototype
106 WebInspector
.AnimationModel
._symbol
= Symbol("AnimationModel");
109 * @param {!WebInspector.Target} target
110 * @return {!WebInspector.AnimationModel}
112 WebInspector
.AnimationModel
.fromTarget = function(target
)
114 if (!target
[WebInspector
.AnimationModel
._symbol
])
115 target
[WebInspector
.AnimationModel
._symbol
] = new WebInspector
.AnimationModel(target
);
117 return target
[WebInspector
.AnimationModel
._symbol
];
122 * @extends {WebInspector.SDKObject}
123 * @param {!WebInspector.Target} target
124 * @param {!AnimationAgent.Animation} payload
126 WebInspector
.AnimationModel
.Animation = function(target
, payload
)
128 WebInspector
.SDKObject
.call(this, target
);
129 this._payload
= payload
;
130 this._source
= new WebInspector
.AnimationModel
.AnimationEffect(this.target(), this._payload
.source
);
134 * @param {!WebInspector.Target} target
135 * @param {!AnimationAgent.Animation} payload
136 * @return {!WebInspector.AnimationModel.Animation}
138 WebInspector
.AnimationModel
.Animation
.parsePayload = function(target
, payload
)
140 return new WebInspector
.AnimationModel
.Animation(target
, payload
);
143 WebInspector
.AnimationModel
.Animation
.prototype = {
145 * @return {!AnimationAgent.Animation}
149 return this._payload
;
157 return this._payload
.id
;
165 return this.source().name();
173 return this._payload
.pausedState
;
179 playState: function()
181 return this._playState
|| this._payload
.playState
;
185 * @param {string} playState
187 setPlayState: function(playState
)
189 this._playState
= playState
;
195 playbackRate: function()
197 return this._payload
.playbackRate
;
203 startTime: function()
205 return this._payload
.startTime
;
213 if (!this.source().iterations
)
215 return this.startTime() + this.source().delay() + this.source().duration() * this.source().iterations() + this.source().endDelay();
221 currentTime: function()
223 return this._payload
.currentTime
;
227 * @return {!WebInspector.AnimationModel.AnimationEffect}
239 return this._payload
.type
;
243 * @param {!WebInspector.AnimationModel.Animation} animation
246 overlaps: function(animation
)
248 // Infinite animations
249 if (!this.source().iterations() || !animation
.source().iterations())
252 var firstAnimation
= this.startTime() < animation
.startTime() ? this : animation
;
253 var secondAnimation
= firstAnimation
=== this ? animation
: this;
254 return firstAnimation
.endTime() >= secondAnimation
.startTime();
257 __proto__
: WebInspector
.SDKObject
.prototype
262 * @extends {WebInspector.SDKObject}
263 * @param {!WebInspector.Target} target
264 * @param {!AnimationAgent.AnimationEffect} payload
266 WebInspector
.AnimationModel
.AnimationEffect = function(target
, payload
)
268 WebInspector
.SDKObject
.call(this, target
);
269 this._payload
= payload
;
270 if (payload
.keyframesRule
)
271 this._keyframesRule
= new WebInspector
.AnimationModel
.KeyframesRule(target
, payload
.keyframesRule
);
272 this._delay
= this._payload
.delay
;
273 this._duration
= this._payload
.duration
;
276 WebInspector
.AnimationModel
.AnimationEffect
.prototype = {
286 * @param {number} delay
288 setDelay: function(delay
)
298 return this._payload
.endDelay
;
304 playbackRate: function()
306 return this._payload
.playbackRate
;
312 iterationStart: function()
314 return this._payload
.iterationStart
;
320 iterations: function()
322 return this._payload
.iterations
|| Infinity
;
330 return this._duration
;
333 setDuration: function(duration
)
335 this._duration
= duration
;
341 direction: function()
343 return this._payload
.direction
;
351 return this._payload
.fill
;
359 return this._payload
.name
;
363 * @return {!WebInspector.DeferredDOMNode}
365 deferredNode: function()
367 return new WebInspector
.DeferredDOMNode(this.target(), this.backendNodeId());
373 backendNodeId: function()
375 return this._payload
.backendNodeId
;
379 * @return {?WebInspector.AnimationModel.KeyframesRule}
381 keyframesRule: function()
383 return this._keyframesRule
;
391 return this._payload
.easing
;
394 __proto__
: WebInspector
.SDKObject
.prototype
399 * @extends {WebInspector.SDKObject}
400 * @param {!WebInspector.Target} target
401 * @param {!AnimationAgent.KeyframesRule} payload
403 WebInspector
.AnimationModel
.KeyframesRule = function(target
, payload
)
405 WebInspector
.SDKObject
.call(this, target
);
406 this._payload
= payload
;
407 this._keyframes
= this._payload
.keyframes
.map(function (keyframeStyle
) {
408 return new WebInspector
.AnimationModel
.KeyframeStyle(target
, keyframeStyle
);
412 WebInspector
.AnimationModel
.KeyframesRule
.prototype = {
414 * @param {!Array.<!AnimationAgent.KeyframeStyle>} payload
416 _setKeyframesPayload: function(payload
)
418 this._keyframes
= payload
.map(function (keyframeStyle
) {
419 return new WebInspector
.AnimationModel
.KeyframeStyle(this._target
, keyframeStyle
);
424 * @return {string|undefined}
428 return this._payload
.name
;
432 * @return {!Array.<!WebInspector.AnimationModel.KeyframeStyle>}
434 keyframes: function()
436 return this._keyframes
;
439 __proto__
: WebInspector
.SDKObject
.prototype
444 * @extends {WebInspector.SDKObject}
445 * @param {!WebInspector.Target} target
446 * @param {!AnimationAgent.KeyframeStyle} payload
448 WebInspector
.AnimationModel
.KeyframeStyle = function(target
, payload
)
450 WebInspector
.SDKObject
.call(this, target
);
451 this._payload
= payload
;
452 this._offset
= this._payload
.offset
;
455 WebInspector
.AnimationModel
.KeyframeStyle
.prototype = {
465 * @param {number} offset
467 setOffset: function(offset
)
469 this._offset
= offset
* 100 + "%";
475 offsetAsNumber: function()
477 return parseFloat(this._offset
) / 100;
485 return this._payload
.easing
;
488 __proto__
: WebInspector
.SDKObject
.prototype
493 * @extends {WebInspector.SDKObject}
494 * @param {!WebInspector.Target} target
496 * @param {!Array.<!WebInspector.AnimationModel.Animation>} animations
498 WebInspector
.AnimationModel
.AnimationGroup = function(target
, id
, animations
)
500 WebInspector
.SDKObject
.call(this, target
);
502 this._animations
= animations
;
505 WebInspector
.AnimationModel
.AnimationGroup
.prototype = {
515 * @return {!Array.<!WebInspector.AnimationModel.Animation>}
517 animations: function()
519 return this._animations
;
522 __proto__
: WebInspector
.SDKObject
.prototype
528 * @implements {AnimationAgent.Dispatcher}
530 WebInspector
.AnimationDispatcher = function(animationModel
)
532 this._animationModel
= animationModel
;
535 WebInspector
.AnimationDispatcher
.prototype = {
540 animationCreated: function(id
)
542 this._animationModel
.animationCreated(id
);
547 * @param {!AnimationAgent.Animation} payload
549 animationStarted: function(payload
)
551 this._animationModel
.animationStarted(payload
);
558 animationCanceled: function(id
)
560 this._animationModel
.animationCanceled(id
);