-
Notifications
You must be signed in to change notification settings - Fork 3
/
ae-ease-to-gsap-customease.jsx
364 lines (302 loc) · 10.3 KB
/
ae-ease-to-gsap-customease.jsx
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
(function () {
'use strict';
// Set to true to turn on logging in ExtendScript Toolkit
var debug = false;
/**
* An SVG path comprised of multiple path drawing commands.
* https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths
* @param startX {number} - If provided, defines the start point for the initial "move" (M) command.
* @param startY {number} - If provided, defines the start point for the initial "move" (M) command.
* @constructor
*/
function Path(startX, startY) {
this.commands = [];
if (typeof startX === 'number' && typeof startY === 'number') {
var moveCommand = new PathCommand('M', startX, startY);
this.commands = [moveCommand];
}
}
/**
* Inverts the Y axis of all points of all commands in this path.
* Used to help get paths into a format that GSAP's CustomEase plugin expects.
*/
Path.prototype.invertYAxis = function () {
var numCommands = this.commands.length;
for (var i = 0; i < numCommands; i++) {
var command = this.commands[i];
var numPoints = command.points.length;
for (var j = 0; j < numPoints; j++) {
var point = command.points[j];
point.y *= -1;
}
}
};
/**
* Returns the end point of the path.
* @returns {Point}
*/
Path.prototype.getEndPoint = function () {
var numCommands = this.commands.length;
var lastCommand = this.commands[numCommands - 1];
var numPoints = lastCommand.points.length;
return lastCommand.points[numPoints - 1];
};
Path.prototype.toString = function () {
var string = '';
for (var i = 0; i < this.commands.length; i++) {
string += this.commands[i].toString();
}
return string;
};
/**
* A single SVG path command.
* https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths
* @param command {string} - A single-character SVG path command, see above MDN link for more details.
* @constructor
*/
function PathCommand(command) {
this.command = command;
this.points = [];
var coordinates = Array.prototype.slice.call(arguments, 1);
var hasEvenNumberOfCoordinates = !(coordinates.length % 2);
if (hasEvenNumberOfCoordinates) {
for (var i = 0; i < coordinates.length; i += 2) {
var point = new Point(coordinates[i], coordinates[i + 1]);
this.points.push(point);
}
} else {
throw new Error('Must provide an even number of coordinates when instantiating a PathCommand.');
}
}
PathCommand.prototype.toString = function () {
var string = this.command;
for (var i = 0; i < this.points.length; i++) {
if (i >= 1) {
string += ',';
}
string += this.points[i].toString();
}
return string;
};
/**
* A 2D point in space.
* @param x {number}
* @param y {number}
* @constructor
*/
function Point(x, y) {
this.x = x;
this.y = y;
}
Point.prototype.toString = function () {
return cleanNumber(this.x) + ',' + cleanNumber(this.y);
};
// Above this line are the class definitions used in the script.
/* ---------------------------------------------------------------------------- */
// Below this line is the main logic of the script.
var curItem = app.project.activeItem;
if (curItem === null || !(curItem instanceof CompItem)) {
alert('Please Select a Comp');
return;
}
var framerate = curItem.frameRate;
var selectedProperties = curItem.selectedProperties;
if (selectedProperties.length === 0) {
alert('Please Select at least one Property (Scale, Opacity, etc)');
return;
}
for (var f = 0; f < selectedProperties.length; f++) {
var currentProperty = selectedProperties[f];
// Ignore properties that have one or fewer keyframes.
if (currentProperty.numKeys <= 1) {
continue;
}
if (currentProperty.value instanceof Array) {
// Handle multi-dimensional properties.
for (var d = 0; d < currentProperty.value.length; d++) {
processProperty(currentProperty, d);
}
} else {
// Handle single-dimensional properties.
processProperty(currentProperty);
}
}
function processProperty(property, dimension) {
var path = getPath(property, dimension);
if (path === null) {
return;
}
var pathString = path.toString();
log(pathString);
log();
log();
copyTextToClipboard(pathString);
var alertStart = 'Copied ease for property "' + property.name + '"';
if (typeof dimension === 'number') {
alertStart += ' (dimension ' + (dimension + 1) + '/' + property.value.length + ')';
}
alertStart += ' to clipboard:\n';
alert(alertStart + pathString +
'\n\nPaste directly into a GSAP CustomEase, like:\n' +
'CustomEase.create(\'myCustomEase\', \'' + pathString + '\');' +
'\n\nMore info: https://greensock.com/docs/#/HTML5/GSAP/Easing/CustomEase/');
}
function getPath(property, dimension) {
var curveStartFrame = property.keyTime(1) * framerate;
var curveStartValue;
if (typeof dimension === 'number') {
curveStartValue = property.keyValue(1)[dimension];
} else {
curveStartValue = property.keyValue(1);
}
// The path data we output is in SVG path format: https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths
// Moves the drawing pen to the start point of the path.
var path = new Path(curveStartFrame, curveStartValue);
for (var i = 1; i < property.numKeys; i++) {
var command = getCommand(property, i, dimension);
// Some invocations of getCommand will actually return an Array of commands.
if (command instanceof Array) {
path.commands = path.commands.concat(command);
} else {
path.commands.push(command);
}
log();
}
var endPoint = path.getEndPoint();
if (endPoint.y > curveStartValue) {
path.invertYAxis();
}
return path;
}
function getCommand(property, keyIndex, dimension) {
var command;
var tweenData = calcTweenData(property, keyIndex, keyIndex + 1, dimension);
var easeType = calcEaseType(property, keyIndex, keyIndex + 1);
log('easeType: ' + easeType);
// For linear eases, just draw a line. (L x y)
if (easeType === 'linear-linear') {
command = new PathCommand('L', tweenData.endFrame, tweenData.endValue);
return command;
}
if (easeType === 'hold-hold' || easeType === 'linear-hold' || easeType === 'hold-linear') {
return [
new PathCommand('L', tweenData.endFrame, tweenData.startValue),
new PathCommand('L', tweenData.endFrame, tweenData.endValue)
];
}
if (easeType === 'unsupported') {
log('UNSUPPORTED EASING PAIR!');
alert('This keyframe pair uses an unsupported pair of ease types, results may be inaccurate.');
}
command = new PathCommand('C');
command.points.push(
calcOutgoingControlPoint(tweenData, property, keyIndex),
calcIncomingControlPoint(tweenData, property, keyIndex),
new Point(tweenData.endFrame, tweenData.endValue) // End anchor point.
);
return command;
}
// Above this line is the main logic of the script.
/* ---------------------------------------------------------------------------- */
// Below this line are the helper functions that the main logic uses.
function calcTweenData(property, startIndex, endIndex, dimension) {
var startTime = property.keyTime(startIndex);
var endTime = property.keyTime(endIndex);
var durationTime = endTime - startTime;
var startFrame = startTime * framerate;
var endFrame = endTime * framerate;
var durationFrames = endFrame - startFrame;
var startValue;
var endValue;
if (property.value instanceof Array) {
startValue = property.keyValue(startIndex)[dimension];
endValue = property.keyValue(endIndex)[dimension];
} else {
startValue = property.keyValue(startIndex);
endValue = property.keyValue(endIndex);
}
return {
startTime: startTime,
endTime: endTime,
durationTime: durationTime,
startFrame: startFrame,
endFrame: endFrame,
durationFrames: durationFrames,
startValue: startValue,
endValue: endValue
};
}
function cleanNumber(num) {
return parseFloat(num.toFixed(4));
}
// From https://forums.adobe.com/message/9157695#9157695
function copyTextToClipboard(str) {
var cmdString;
if ($.os.indexOf('Windows') === -1) {
cmdString = 'echo "' + str + '" | pbcopy';
} else {
cmdString = 'cmd.exe /c cmd.exe /c "echo ' + str + ' | clip"';
}
system.callSystem(cmdString);
}
function calcEaseType(property, startIndex, endIndex) {
var startInterpolation = property.keyOutInterpolationType(startIndex);
var endInterpolation = property.keyInInterpolationType(endIndex);
if (startInterpolation === KeyframeInterpolationType.LINEAR &&
endInterpolation === KeyframeInterpolationType.LINEAR) {
return 'linear-linear';
}
if (startInterpolation === KeyframeInterpolationType.LINEAR &&
endInterpolation === KeyframeInterpolationType.BEZIER) {
return 'linear-bezier';
}
if (startInterpolation === KeyframeInterpolationType.BEZIER &&
endInterpolation === KeyframeInterpolationType.LINEAR) {
return 'bezier-linear';
}
if (startInterpolation === KeyframeInterpolationType.BEZIER &&
endInterpolation === KeyframeInterpolationType.BEZIER) {
return 'bezier-bezier';
}
if (startInterpolation === KeyframeInterpolationType.HOLD &&
endInterpolation === KeyframeInterpolationType.HOLD) {
return 'hold-hold';
}
if (startInterpolation === KeyframeInterpolationType.HOLD &&
endInterpolation === KeyframeInterpolationType.LINEAR) {
return 'hold-linear';
}
if (startInterpolation === KeyframeInterpolationType.LINEAR &&
endInterpolation === KeyframeInterpolationType.HOLD) {
return 'linear-hold';
}
return 'unsupported';
}
function calcOutgoingControlPoint(tweenData, property, keyIndex) {
var outgoingEase = property.keyOutTemporalEase(keyIndex);
var outgoingSpeed = outgoingEase[0].speed;
var outgoingInfluence = outgoingEase[0].influence / 100;
var m = outgoingSpeed / framerate; // Slope
var x = tweenData.durationFrames * outgoingInfluence;
var b = tweenData.startValue; // Y-intercept
var y = (m * x) + b;
var correctedX = tweenData.startFrame + x;
return new Point(correctedX, y);
}
function calcIncomingControlPoint(tweenData, property, keyIndex) {
var incomingEase = property.keyInTemporalEase(keyIndex + 1);
var incomingSpeed = incomingEase[0].speed;
var incomingInfluence = incomingEase[0].influence / 100;
var m = -incomingSpeed / framerate; // Slope
var x = tweenData.durationFrames * incomingInfluence;
var b = tweenData.endValue; // Y-intercept
var y = (m * x) + b;
var correctedX = tweenData.endFrame - x;
return new Point(correctedX, y);
}
function log(string) {
if (debug) {
$.writeln(string || '');
}
}
})();