mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-12-06 04:35:32 +00:00
new: Implement missing CAKeyframeAnimation properties
This commit is contained in:
1 parent
4cb0a87c68
commit
41035a5b10
3 files changed
+155
-18
No files matched your search
@@ -378,3 +378,42 @@ var DefaultLineWidth = 1.0;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@implementation CPBezierPath (AnimationAdditions)
|
||||
|
||||
- (CPString)SVGString
|
||||
{
|
||||
var pathString = "",
|
||||
points = _points,
|
||||
i = 0,
|
||||
count = [self elementCount];
|
||||
|
||||
for (var elementIndex = 0; elementIndex < count; ++elementIndex)
|
||||
{
|
||||
var type = [self elementAtIndex:elementIndex associatedPoints:points];
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case CPMoveToBezierPathElement:
|
||||
pathString += "M" + points[0].x + " " + points[0].y + " ";
|
||||
break;
|
||||
|
||||
case CPLineToBezierPathElement:
|
||||
pathString += "L" + points[0].x + " " + points[0].y + " ";
|
||||
break;
|
||||
|
||||
case CPCurveToBezierPathElement:
|
||||
pathString += "C" + points[0].x + " " + points[0].y + " " + points[1].x + " " + points[1].y + " " + points[2].x + " " + points[2].y + " ";
|
||||
break;
|
||||
|
||||
case CPClosePathBezierPathElement:
|
||||
pathString += "Z";
|
||||
break;
|
||||
}
|
||||
}
|
||||
return pathString;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -128,13 +128,13 @@ var _CPAnimationContextStack = nil,
|
||||
- (Object)_actionForObject:(id)anObject keyPath:(CPString)aKeyPath targetValue:(id)aTargetValue animationCompletion:(Function)animationCompletion
|
||||
{
|
||||
var animation,
|
||||
duration,
|
||||
animatedKeyPath,
|
||||
values,
|
||||
keyTimes,
|
||||
timingFunctions,
|
||||
needsPeriodicFrameUpdates,
|
||||
objectId = [anObject UID];
|
||||
duration,
|
||||
animatedKeyPath,
|
||||
values,
|
||||
keyTimes,
|
||||
timingFunctions,
|
||||
needsPeriodicFrameUpdates,
|
||||
objectId = [anObject UID];
|
||||
|
||||
if (!aKeyPath || !anObject || !(animation = [anObject animationForKey:aKeyPath]) || ![animation isKindOfClass:[CAAnimation class]])
|
||||
return nil;
|
||||
@@ -164,9 +164,36 @@ var _CPAnimationContextStack = nil,
|
||||
|
||||
if ([animation isKindOfClass:[CAKeyframeAnimation class]])
|
||||
{
|
||||
|
||||
values = [animation values];
|
||||
keyTimes = [animation keyTimes];
|
||||
timingFunctions = [animation timingFunctionsControlPoints];
|
||||
|
||||
var path = [animation path];
|
||||
var calculationMode = [animation calculationMode] || kCAAnimationLinear; // Default to linear
|
||||
var rotationMode = [animation rotationMode];
|
||||
var tensionValues = [animation tensionValues];
|
||||
var continuityValues = [animation continuityValues];
|
||||
var biasValues = [animation biasValues];
|
||||
|
||||
return {
|
||||
object: anObject,
|
||||
root: anObject,
|
||||
keypath: animatedKeyPath,
|
||||
duration: duration,
|
||||
completion: completionFunction,
|
||||
|
||||
// Keyframe-specific properties
|
||||
values: values,
|
||||
keytimes: keyTimes,
|
||||
timingfunctions: timingFunctions,
|
||||
path: path,
|
||||
calculationMode: calculationMode,
|
||||
rotationMode: rotationMode,
|
||||
tensionValues: tensionValues,
|
||||
continuityValues: continuityValues,
|
||||
biasValues: biasValues
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -183,18 +210,19 @@ var _CPAnimationContextStack = nil,
|
||||
values = [fromValue, toValue];
|
||||
keyTimes = [0, 1];
|
||||
timingFunctions = isBasicAnimation ? [animation timingFunctionControlPoints] : [_timingFunction controlPoints];
|
||||
}
|
||||
|
||||
return {
|
||||
object:anObject,
|
||||
root:anObject,
|
||||
keypath:animatedKeyPath,
|
||||
values:values,
|
||||
keytimes:keyTimes,
|
||||
duration:duration,
|
||||
timingfunctions:timingFunctions,
|
||||
completion:completionFunction
|
||||
};
|
||||
// Return a basic animation action
|
||||
return {
|
||||
object:anObject,
|
||||
root:anObject,
|
||||
keypath:animatedKeyPath,
|
||||
values:values,
|
||||
keytimes:keyTimes,
|
||||
duration:duration,
|
||||
timingfunctions:timingFunctions,
|
||||
completion:completionFunction
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
- (void)_flushAnimations
|
||||
|
||||
@@ -116,6 +116,13 @@ var DEFAULT_CSS_PROPERTIES = nil,
|
||||
{
|
||||
var target = anAction.object;
|
||||
|
||||
if (anAction.path)
|
||||
{
|
||||
// Path animations are handled differently. They don't map to standard properties like width/height.
|
||||
// They use CSS offset-path.
|
||||
return [self _addPathAnimation:animations forAction:anAction domElement:[target _DOMElement] identifier:[target UID]];
|
||||
}
|
||||
|
||||
return [self _addAnimations:animations forAction:anAction domElement:[target _DOMElement] identifier:[target UID]];
|
||||
}
|
||||
|
||||
@@ -132,6 +139,19 @@ var DEFAULT_CSS_PROPERTIES = nil,
|
||||
[animations addObject:animation];
|
||||
}
|
||||
|
||||
var calculationMode = anAction.calculationMode;
|
||||
|
||||
if (calculationMode === kCAAnimationDiscrete)
|
||||
{
|
||||
// For discrete animations, we use the steps() timing function.
|
||||
// This makes the property jump between values.
|
||||
animation.setTimingFunction("steps(1, end)");
|
||||
}
|
||||
// For other calculation modes like 'paced' or 'cubic', more complex logic would be needed here.
|
||||
// 'paced' would require pre-calculating keyTimes based on distance.
|
||||
// 'cubic' would require generating many keyframes to simulate a spline.
|
||||
// For now, we let them fall through to the default behavior.
|
||||
|
||||
var css_mapping = [self _cssPropertiesForKeyPath:anAction.keypath];
|
||||
|
||||
[css_mapping enumerateObjectsUsingBlock:function(aDict, anIndex, stop)
|
||||
@@ -144,6 +164,56 @@ var DEFAULT_CSS_PROPERTIES = nil,
|
||||
}];
|
||||
}
|
||||
|
||||
+ (void)_addPathAnimation:(CPArray)animations forAction:(id)anAction domElement:(Object)aDomElement identifier:(CPString)anIdentifier
|
||||
{
|
||||
var animation = [animations objectPassingTest:function(anim, idx, stop)
|
||||
{
|
||||
return anim.identifier == anIdentifier;
|
||||
}];
|
||||
|
||||
if (animation == nil)
|
||||
{
|
||||
animation = new CSSAnimation(aDomElement, anIdentifier, [anAction.object debug_description]);
|
||||
[animations addObject:animation];
|
||||
}
|
||||
|
||||
// 1. Get the SVG path string from the CPBezierPath object.
|
||||
// This requires the helper method added in Step 1.
|
||||
var svgPath = [anAction.path SVGString];
|
||||
|
||||
// 2. Set the offset-path property.
|
||||
animation.setCSSProperty("offset-path", "path('" + svgPath + "')");
|
||||
|
||||
// 3. Set the rotation mode.
|
||||
var rotationMode = anAction.rotationMode;
|
||||
if (rotationMode === kCAAnimationRotateAuto)
|
||||
{
|
||||
animation.setCSSProperty("offset-rotate", "auto");
|
||||
}
|
||||
else if (rotationMode === kCAAnimationRotateAutoReverse)
|
||||
{
|
||||
animation.setCSSProperty("offset-rotate", "auto 180deg");
|
||||
}
|
||||
else
|
||||
{
|
||||
animation.setCSSProperty("offset-rotate", "0deg");
|
||||
}
|
||||
|
||||
// 4. Create the @keyframes rule to animate along the path.
|
||||
// The animated property is 'offset-distance'.
|
||||
var getter = function(start, current) { return current; };
|
||||
var values = ["0%", "100%"]; // Animate from the start to the end of the path
|
||||
var keytimes = [0, 1];
|
||||
var timingfunctions = [[0,0,1,1]]; // Linear timing is standard for path distance
|
||||
|
||||
// Use a 'paced' calculation mode to ensure constant speed, which is a common expectation for path animations.
|
||||
if (anAction.calculationMode === kCAAnimationPaced) {
|
||||
animation.setTimingFunction("linear");
|
||||
}
|
||||
|
||||
animation.addPropertyAnimation("offset-distance", getter, anAction.duration, keytimes, values, timingfunctions, anAction.completion);
|
||||
}
|
||||
|
||||
+ (CPArray)_cssPropertiesForKeyPath:(CPString)aKeyPath
|
||||
{
|
||||
return [[self _defaultCSSProperties] objectForKey:aKeyPath];
|
||||
|
||||
Reference in new issue
Block a user