-
Notifications
You must be signed in to change notification settings - Fork 3
/
Animator.j
326 lines (253 loc) · 10.4 KB
/
Animator.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Animator.j
* Pixity
*
* Created by Nicholas Small on April 1, 2010.
* Copyright Perfect Studios, 2010. All rights reserved.
*/
@import <AppKit/CPView.j>
CPAnimationEase = 4;
var CurveFunctionMap = [];
CurveFunctionMap[CPAnimationEaseInOut] = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
CurveFunctionMap[CPAnimationEaseIn] = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
CurveFunctionMap[CPAnimationEaseOut] = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
CurveFunctionMap[CPAnimationLinear] = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
CurveFunctionMap[CPAnimationEase] = [CAMediaTimingFunction functionWithControlPoints:0.25 :0.1 :0.25 :1.0];
var optional = function(options, key, defaultValue) {
if (typeof(options[key]) == 'undefined')
options[key] = defaultValue;
return options[key];
}
@implementation CPView (AnimatorHelpers)
- (void)fadeIn
{
[self fade:{
from: 0,
to: 1
}];
}
- (void)fadeOut
{
[self fade:{
from: [self alphaValue],
to: 0
}];
}
@end
@implementation CPView (Animators)
- (void)fade:(JSObject)options
{
var alpha = [self alphaValue],
from = optional(options, 'from', alpha > 0 ? alpha : 0),
to = optional(options, 'to', alpha > 0 ? 0 : 1),
duration = optional(options, 'duration', 1),
curve = optional(options, 'curve', CPAnimationEase),
delay = optional(options, 'delay', NO),
privateDelay = optional(options, '_delay', NO);
[self removeTransitionForProperty:@"opacity"];
if (duration > 0)
{
[self setAlphaValue:from];
[self addTransitionForProperty:@"opacity" duration:duration animationCurve:curve];
[self _applyTransitions];
setTimeout(function(){
[self removeTransitionForProperty:@"opacity"];
[self _applyTransitions];
}, duration * 1000);
}
[self setAlphaValue:to];
}
- (void)flip:(JSObject)options
{
var modalView = options.modalView;
if (![modalView isKindOfClass:CPView])
return;
var lightboxWindow = [[CPWindow alloc] initWithContentRect:[[self window] frame] styleMask:CPBorderlessWindowMask];
[lightboxWindow setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
[[lightboxWindow contentView] setBackgroundColor:[CPColor colorWithCalibratedWhite:0.1 alpha:0.6]];
[[lightboxWindow contentView] setAlphaValue:0.0];
[lightboxWindow contentView]._DOMElement.style['-webkit-perspective'] = @"1200px";
[lightboxWindow orderFront:self];
setTimeout(function(){[[lightboxWindow contentView] fadeIn];},0);
var oldSize = [modalView frame].size;
[modalView setFrame:[self frame]];
[modalView setAffineTransform:@"rotateY(-170deg)"];
[[lightboxWindow contentView] addSubview:modalView];
var currentAlpha = [self alphaValue],
currentTransform = [self affineTransform],
duration = optional(options, 'duration', 1),
curve = optional(options, 'curve', CPAnimationEaseInOut);
options._currentAlpha = currentAlpha;
options._currentTransform = currentTransform;
options._lightboxWindow = lightboxWindow;
self._flipOptions = options;
if (duration > 0)
{
[self superview]._DOMElement.style['-webkit-perspective'] = @"1200px";
[self addTransitionForProperty:@"-webkit-transform" duration:duration animationCurve:curve];
_DOMElement.style['-webkit-transform-style'] = @"preserve-3d";
_DOMElement.style['-webkit-backface-visibility'] = @"hidden";
[self _applyTransitions];
[modalView addTransitionForProperty:@"left" duration:duration * 0.4 animationCurve:curve];
[modalView addTransitionForProperty:@"top" duration:duration * 0.4 animationCurve:curve];
[modalView addTransitionForProperty:@"width" duration:duration * 0.4 animationCurve:curve];
[modalView addTransitionForProperty:@"height" duration:duration * 0.4 animationCurve:curve];
[modalView addTransitionForProperty:@"-webkit-transform" duration:duration animationCurve:curve];
modalView._DOMElement.style['-webkit-transform-style'] = @"preserve-3d";
modalView._DOMElement.style['-webkit-backface-visibility'] = @"hidden";
[modalView _applyTransitions];
}
// var transform = CGAffineTransformScale(currentTransform, 1.5, 1.5);
// CGAffineTransform3DRotateY(transform, 170 * PI / 180);
// var transform = CGAffineTransformMake([-1.47721, 0, 0, 0], [0, 1.5, 0, 0], [0.260472, 0, 0, 0], [0, 0], [0], [1]);
var transform = @"rotateY(170deg)";
[self setAffineTransform:transform];
setTimeout(function(){
[modalView setAffineTransform:CGAffineTransformMakeIdentity()];
}, 0);
setTimeout(function(){
var contentView = [[self window] contentView],
bounds = [contentView bounds];
[modalView setFrame:CGRectMake(CGRectGetMidX(bounds) - oldSize.width * 0.5, CGRectGetMidY(bounds) - oldSize.height * 0.5, oldSize.width, oldSize.height)];
}, duration * 0.5 * 1000);
}
- (void)unflip
{
var options = self._flipOptions;
if (!options)
return;
delete self._flipOptions;
var currentAlpha = options._currentAlpha,
currentTransform = options._currentTransform,
lightboxWindow = options._lightboxWindow,
modalView = options.modalView,
duration = options.duration;
if (duration > 0)
{
setTimeout(function(){
[self removeTransitionForProperty:@"-webkit-transform"];
[self superview]._DOMElement.style['-webkit-perspective'] = null;
_DOMElement.style['-webkit-transform-style'] = null;
[self _applyTransitions];
[modalView removeTransitionForProperty:@"-webkit-transform"];
[modalView removeTransitionForProperty:@"left"];
[modalView removeTransitionForProperty:@"top"];
[modalView removeTransitionForProperty:@"width"];
[modalView removeTransitionForProperty:@"height"];
[modalView _applyTransitions];
}, duration * 1000);
}
[self setAffineTransform:currentTransform];
[modalView setAffineTransform:@"rotateY(-170deg)"];
[modalView setFrame:[self frame]];
setTimeout(function(){
[[lightboxWindow contentView] fadeOut];
},duration * 0.5 * 1000);
setTimeout(function(){
[lightboxWindow orderOut:self];
},duration * 1000);
}
@end
@implementation CPView (AnimatorProperties)
- (void)addTransitionForProperty:(CPString)property duration:(float)duration animationCurve:(CAMediaTimingFunction)curve
{
var properties = self._transitionProperties;
if (!properties)
{
properties = {};
self._transitionProperties = properties;
}
var thisProperty = properties[property];
if (!thisProperty)
{
thisProperty = {};
properties[property] = thisProperty;
}
thisProperty.duration = duration + @"s";
if (![curve isKindOfClass:CAMediaTimingFunction])
curve = CurveFunctionMap[curve] || [CurveFunctionMap lastObject];
thisProperty.curve = @"cubic-bezier(" + [curve._c1x, curve._c1y, curve._c2x, curve._c2y].join(@", ") + @")";
}
- (void)removeTransitionForProperty:(CPString)property
{
var properties = self._transitionProperties;
if (!properties)
return;
delete properties[property];
}
- (void)_applyTransitions
{
var properties = self._transitionProperties;
if (!properties)
properties = {};
var propStyle = [],
durationStyle = [],
curveStyle = [];
for (property in properties)
{
var options = properties[property];
propStyle.push(property);
durationStyle.push(options.duration);
curveStyle.push(options.curve);
}
var style = _DOMElement.style,
prefixes = [@"webkit", @"moz", @"o"];
for (var i = 0, count = prefixes.length; i < count; i++)
{
var prefix = prefixes[i];
style[prefix + @"TransitionProperty"] = propStyle.join(@", ");
style[prefix + @"TransitionDuration"] = durationStyle.join(@", ");
style[prefix + @"TransitionTimingFunction"] = curveStyle.join(@", ");
}
[[CPRunLoop currentRunLoop] performSelectors];
}
- (void)setAffineTransform:(CGAffineTransform)anAffineTransform
{
if (!anAffineTransform)
anAffineTransform = CGAffineTransformMakeIdentity();
if (!anAffineTransform.isa)
{
if (_transform && !_transform.isa && CGAffineTransformEqualToTransform(_transform, anAffineTransform))
return;
_transform = CGAffineTransformMakeCopy(anAffineTransform);
if (CGAffineTransformIsIdentity(_transform))
_DOMElement.style["-webkit-transform"] = null;
else
_DOMElement.style["-webkit-transform"] = "matrix(" +
_transform.a + ", " +
_transform.b + ", " +
_transform.c + ", " +
_transform.d + ", " +
_transform.tx + ", " +
_transform.ty + ")";
}
else
{
_transform = anAffineTransform;
if ([_transform isKindOfClass:CPString])
_DOMElement.style["-webkit-transform"] = _transform;
}
}
@end
function CGAffineTransform3DFrom2D(aTransform) {
return CGAffineTransformMake(
[aTransform.a, aTransform.b, 0, 0],
[aTransform.c, aTransform.d, 0, 0],
[0, 0, 1, 0],
[aTransform.e, aTransform.f], [0], [1]
);
}
function CGAffineTransform3DConcat(lhs, rhs) {
}
function CGAffineTransform3DMakeRotationY(angle) {
var sin = SIN(angle),
cos = COS(angle);
return CGAffineTransformMake(
[cos, 0, -sin, 0],
[0, 1, 0, 0],
[sin, 0, cos, 0],
[0, 0], [0], [1]
);
}
function CGAffineTransform3DRotateY(aTransform, angle) {
}