Move the FrameUpdater function to CPViewAnimator

This commit is contained in:
cacaodev committed 2017-03-02 10:30:07 +01:00
1 parent 14ebd58c96
commit 184d63f7eb
2 files changed
+120 -109

No files matched your search

-109
View File
@@ -585,112 +585,3 @@ var _animationFlushingObserverCallback = function()
_animationFlushingObserver = nil;
}
};
var FrameUpdater = function(anIdentifier)
{
this._identifier = anIdentifier;
this._requestId = null;
this._duration = 0;
this._stop = false;
this._targets = [];
this._callbacks = [];
var frameUpdater = this;
this._updateFunction = function(timestamp)
{
if (frameUpdater._startDate == null)
frameUpdater._startDate = timestamp;
if (frameUpdater._stop)
return;
for (var i = 0; i < frameUpdater._callbacks.length; i++)
frameUpdater._callbacks[i]();
if (timestamp - frameUpdater._startDate < frameUpdater._duration * 1000)
window.requestAnimationFrame(frameUpdater._updateFunction);
};
};
FrameUpdater.prototype.start = function()
{
this._requestId = window.requestAnimationFrame(this._updateFunction);
};
FrameUpdater.prototype.stop = function()
{
// window.cancelAnimationFrame support is Chrome 24, Firefox 23, IE 10, Opera 15, Safari 6.1
if (window.cancelAnimationFrame)
window.cancelAnimationFrame(this._requestId);
this._stop = true;
};
FrameUpdater.prototype.updateFunction = function()
{
return this._updateFunction;
};
FrameUpdater.prototype.identifier = function()
{
return this._identifier;
};
FrameUpdater.prototype.addTarget = function(target, keyPath, duration)
{
var callback = createUpdateFrame(target, keyPath);
if (callback)
{
this._duration = MAX(this._duration, duration);
this._targets.push(target);
this._callbacks.push(callback);
}
};
var createUpdateFrame = function(aView, aKeyPath)
{
if (aKeyPath !== "frame" && aKeyPath !== "frameSize" && aKeyPath !== "frameOrigin")
return nil;
var style = getComputedStyle(aView._DOMElement),
getCSSPropertyValue = function(prop) {
return ROUND(parseFloat(style.getPropertyValue(prop)));
},
initialOrigin = CGPointMakeCopy([aView frameOrigin]),
transformProperty = CPBrowserCSSProperty("transform"),
updateFrame = function(timestamp)
{
var width = getCSSPropertyValue("width"),
height = getCSSPropertyValue("height");
if (aKeyPath === "frameSize")
{
[aView setFrameSize:CGSizeMake(width, height)];
}
else
{
[aView _setInhibitDOMUpdates:YES];
var matrix = style[transformProperty].split('(')[1].split(')')[0].split(','),
x = ROUND(initialOrigin.x + parseFloat(matrix[4])),
y = ROUND(initialOrigin.y + parseFloat(matrix[5]));
if (aKeyPath === "frame")
{
[aView setFrame:CGRectMake(x, y, width, height)];
}
else
{
[aView setFrameOrigin:CGPointMake(x, y)];
}
[aView _setInhibitDOMUpdates:NO];
}
[[CPRunLoop currentRunLoop] performSelectors];
};
return updateFrame;
};
+120
View File
@@ -223,3 +223,123 @@ var DEFAULT_CSS_PROPERTIES = nil;
}
@end
var FrameUpdater = function(anIdentifier)
{
this._identifier = anIdentifier;
this._requestId = null;
this._duration = 0;
this._stop = false;
this._targets = [];
this._callbacks = [];
var frameUpdater = this;
this._updateFunction = function(timestamp)
{
if (frameUpdater._startDate == null)
frameUpdater._startDate = timestamp;
if (frameUpdater._stop)
return;
for (var i = 0; i < frameUpdater._callbacks.length; i++)
frameUpdater._callbacks[i]();
if (timestamp - frameUpdater._startDate < frameUpdater._duration * 1000)
window.requestAnimationFrame(frameUpdater._updateFunction);
};
};
FrameUpdater.prototype.start = function()
{
this._requestId = window.requestAnimationFrame(this._updateFunction);
};
FrameUpdater.prototype.stop = function()
{
CPLog.warn("STOP FrameUpdater" + this._identifier);
// window.cancelAnimationFrame support is Chrome 24, Firefox 23, IE 10, Opera 15, Safari 6.1
if (window.cancelAnimationFrame)
window.cancelAnimationFrame(this._requestId);
this._stop = true;
};
FrameUpdater.prototype.updateFunction = function()
{
return this._updateFunction;
};
FrameUpdater.prototype.identifier = function()
{
return this._identifier;
};
FrameUpdater.prototype.description = function()
{
return "<timer " + this._identifier + " " + this._targets.map(function(t){return [t debug_description];}) + ">";
};
FrameUpdater.prototype.addTarget = function(target, keyPath, duration)
{
var callback = createUpdateFrame(target, keyPath);
if (callback)
{
this._duration = MAX(this._duration, duration);
this._targets.push(target);
this._callbacks.push(callback);
}
};
var createUpdateFrame = function(aView, aKeyPath)
{
if (aKeyPath !== "frame" && aKeyPath !== "frameSize" && aKeyPath !== "frameOrigin")
return nil;
var style = getComputedStyle([aView _DOMElement]),
getCSSPropertyValue = function(prop) {
return ROUND(parseFloat(style.getPropertyValue(prop)));
},
initialOrigin = CGPointMakeCopy([aView frameOrigin]),
transformProperty = CPBrowserStyleProperty("transform"),
updateFrame = function(timestamp)
{
if (aKeyPath === "frameSize")
{
var width = getCSSPropertyValue("width"),
height = getCSSPropertyValue("height");
[aView setFrameSize:CGSizeMake(width, height)];
}
else
{
[aView _setInhibitDOMUpdates:YES];
var matrix = style[transformProperty].split('(')[1].split(')')[0].split(','),
x = ROUND(initialOrigin.x + parseFloat(matrix[4])),
y = ROUND(initialOrigin.y + parseFloat(matrix[5]));
if (aKeyPath === "frame")
{
var width = getCSSPropertyValue("width"),
height = getCSSPropertyValue("height");
[aView setFrame:CGRectMake(x, y, width, height)];
}
else
{
[aView setFrameOrigin:CGPointMake(x, y)];
}
[aView _setInhibitDOMUpdates:NO];
}
[[CPRunLoop currentRunLoop] performSelectors];
// CPLog.debug("update " + [aView debug_description]);
};
return updateFrame;
};