forked from motiondeveloper/aefunctions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aefunctions.jsx
342 lines (342 loc) · 15.3 KB
/
aefunctions.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
{
getFunctions(time = thisLayer.time) {
function funcError(funcName, ...errors) {
return new Error(`in function ${funcName}.\n\n${errors.join('\n')}`);
}
function attachKeys(inKeys = 2, outKeys = 2) {
if (inKeys >= 1 && outKeys >= 1) {
const outStart = thisLayer.outPoint -
(thisProperty.key(thisProperty.numKeys).time -
thisProperty.key(thisProperty.numKeys - outKeys).time);
const inFinish = thisLayer.inPoint +
(thisProperty.key(inKeys).time - thisProperty.key(1).time);
if (time < thisLayer.inPoint) {
return thisProperty.key(1).value;
}
else if (time < inFinish) {
return thisProperty.valueAtTime(thisProperty.key(1).time + (time - thisLayer.inPoint));
}
else if (time < outStart) {
return thisProperty.key(inKeys).value;
}
else {
return thisProperty.valueAtTime(thisProperty.key(thisProperty.numKeys - outKeys).time +
time -
outStart);
}
}
else if (inKeys == 0 && outKeys >= 2) {
const outStart = thisLayer.outPoint -
(thisProperty.key(outKeys).time - thisProperty.key(1).time);
if (time < outStart) {
return thisProperty.key(1).value;
}
else {
return thisProperty.valueAtTime(thisProperty.key(1).time + time - outStart);
}
}
else if (inKeys >= 2 && outKeys == 0) {
const inFinish = thisLayer.inPoint +
(thisProperty.key(inKeys).time - thisProperty.key(1).time);
if (time < thisLayer.inPoint) {
return thisProperty.key(1).value;
}
else if (time < inFinish) {
return thisProperty.valueAtTime(thisProperty.key(1).time + (time - thisLayer.inPoint));
}
else {
return thisProperty.key(inKeys).value;
}
}
else {
return thisProperty.value;
}
}
function bounceKeys(amp = 0.12, freq = 2.5, decay = 8, keyMin = 1, keyMax = thisProperty.numKeys) {
let curKey = 0;
let t = 0;
if (thisProperty.numKeys > 0) {
curKey = thisProperty.nearestKey(time).index;
if (thisProperty.key(curKey).time > time) {
curKey--;
}
}
if (curKey !== 0) {
t = time - thisProperty.key(curKey).time;
}
if (curKey > 0 && curKey >= keyMin && curKey <= keyMax && t < 3) {
let velocity = thisProperty.velocityAtTime(thisProperty.key(curKey).time - thisComp.frameDuration / 10);
return thisLayer.add(thisProperty.value, thisLayer.mul(velocity, (amp * Math.sin(freq * t * 2 * Math.PI)) / Math.exp(decay * t)));
}
else {
return thisProperty.value;
}
}
function getPathFromPoints(points, closed = true) {
const pathPoints = points.map(item => thisLayer.fromCompToSurface(item));
return thisProperty.createPath(pathPoints, [], [], closed);
}
function gridPoints({ rows = 3, columns = 3, rowNum = 1, columnNum = 1, gridSize = [thisComp.width, thisComp.height], }) {
const columnWidth = gridSize[0] / columns;
const rowHeight = gridSize[1] / rows;
const topLeft = [
columnWidth * (columnNum - 1),
rowHeight * (rowNum - 1),
];
const topRight = thisLayer.add(topLeft, [columnWidth, 0]);
const bottomLeft = thisLayer.add(topLeft, [0, rowHeight]);
const bottomRight = thisLayer.add(topRight, [0, rowHeight]);
return [topLeft, topRight, bottomRight, bottomLeft];
}
function hideLayerWhenBelow(layerIndex = thisLayer.index - 1) {
try {
const aboveLayer = thisComp.layer(layerIndex);
return time < aboveLayer.inPoint ? 100 : 0;
}
catch (err) {
return 100;
}
}
function getIsometricPosition(position, offset = [0, 0]) {
const xGrid = position[0];
const yGrid = position[1];
const x = xGrid * 1.75 - yGrid;
const y = xGrid + yGrid / 1.75;
return thisLayer.add(offset, [x, y]);
}
function getLayerBoundsPath(buffer = 0, sourceLayer = thisLayer, extend = false, sampleTime = time) {
const layerWidth = sourceLayer.sourceRectAtTime(sampleTime, extend).width;
const layerHeight = sourceLayer.sourceRectAtTime(sampleTime, extend).height;
const layerTop = sourceLayer.sourceRectAtTime(sampleTime, extend).top;
const layerLeft = sourceLayer.sourceRectAtTime(sampleTime, extend).left;
const maskPoints = [
[layerLeft - buffer, layerTop - buffer],
[layerLeft + layerWidth + buffer, layerTop - buffer],
[layerLeft + layerWidth + buffer, layerTop + layerHeight + buffer],
[layerLeft - buffer, layerTop + layerHeight + buffer],
];
return thisProperty.createPath(maskPoints, [], [], true);
}
function layerSize(layerIndex = thisLayer.index, sampleTime = time) {
const layerSize = [
thisComp.layer(layerIndex).sourceRectAtTime(sampleTime, false).width,
thisComp.layer(layerIndex).sourceRectAtTime(sampleTime, false).height,
];
return layerSize;
}
function layerRect({ layer = thisLayer, sampleTime = time, anchor = 'center', xHeight = true, }) {
const sourceRect = layer.sourceRectAtTime(sampleTime, false);
let { width, height, top, left } = sourceRect;
let topLeft = [left, top];
if (layer.text && xHeight) {
const { fontSize, leading, autoLeading } = layer.text.sourceText.style;
const lineGap = autoLeading ? fontSize * 1.2 : leading;
const textSize = fontSize / 2;
const numLines = textCount(layer.text.sourceText.value, 'line');
height = lineGap * (numLines - 1) + textSize;
topLeft = [left, -textSize];
}
const positions = {
topLeft: topLeft,
topRight: thisLayer.add(topLeft, [width, 0]),
topCenter: thisLayer.add(topLeft, [width / 2, 0]),
bottomCenter: thisLayer.add(topLeft, [width / 2, height]),
bottomLeft: thisLayer.add(topLeft, [0, height]),
bottomRight: thisLayer.add(topLeft, [width, height]),
center: thisLayer.add(topLeft, [width / 2, height / 2]),
leftCenter: thisLayer.add(topLeft, [0, height / 2]),
rightCenter: thisLayer.add(topLeft, [width, height / 2]),
};
const position = positions[anchor];
const onOwnLayer = layer === thisLayer;
return {
size: [width, height],
position: onOwnLayer ? position : layer.toComp(position),
sourceRect: sourceRect,
};
}
function textCount(sourceText, type = 'word') {
if (typeof sourceText !== 'string') {
const valueHint = typeof sourceText === 'function' &&
`\n\nDid you mean sourceText.value?`;
throw funcError(`textCount`, `Invalid value for sourceText.`, `Value must be a string, received ${typeof sourceText}.${valueHint ||
''}`);
}
const counts = {
word: text => text.split(' ').length,
line: text => Math.max(text.split(/[^\r\n\3]*/gm).length - 1, 0),
char: text => text.length,
};
if (!counts[type]) {
throw funcError(`textCount`, `Invalid type: ${type}.\nValid types are: word, line, char`);
}
return counts[type](sourceText);
}
function commaNum(inputNum) {
let number = '' + Math.round(inputNum);
if (number.length > 3) {
const mod = number.length % 3;
let output = mod > 0 ? number.substring(0, mod) : '';
for (let i = 0; i < Math.floor(number.length / 3); i++) {
if (mod == 0 && i == 0) {
output += number.substring(mod + 3 * i, mod + 3 * i + 3);
}
else {
output += ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
}
}
return output;
}
else {
return number;
}
}
function cleanLines(string, maxLines, maxCharacters) {
const lines = string.split(/[\r\n\3]+/g);
const limitedLines = lines.map(item => {
return item.replace(/^\s+|\s+$/g, '').substring(0, maxCharacters);
});
return limitedLines.slice(0, maxLines + 1).join('\n');
}
function hideDescenders(string = thisProperty.value, hideTime = -500) {
const numLines = textCount(string, 'line');
const descenderFreeLines = 'X\r'.repeat(numLines - 1) + 'X';
return time < hideTime ? descenderFreeLines : string;
}
function getKeyframesAsArray() {
let keys = [];
for (let i = 1; i <= thisProperty.numKeys; i++) {
const thisKey = {
time: thisProperty.key(i).time,
value: thisProperty.key(i).value,
};
keys.push(thisKey);
}
return keys;
}
function circularMotion(radius = 200, revolutionTime = 1, startAngle = -90) {
const startRadians = thisLayer.degreesToRadians(startAngle);
const angularSpeed = (2 * Math.PI) / revolutionTime;
const xt = radius * Math.cos(angularSpeed * time + startRadians);
const yt = radius * Math.sin(angularSpeed * time + startRadians);
return [xt, yt];
}
function circularPosition(radius, angle) {
const startAngle = thisLayer.degreesToRadians(angle - 90);
const xt = radius * Math.cos(startAngle);
const yt = radius * Math.sin(startAngle);
return [xt, yt];
}
function countdown(length = thisLayer.outPoint - thisLayer.inPoint, speed = 1) {
const clockTime = Math.max(length - speed * (time - thisLayer.inPoint), 0);
const clock = Math.floor(clockTime);
const min = Math.floor((clock % 3600) / 60);
const sec = Math.floor(clock % 60);
return `${min}:${sec.toString().padStart(2, '0')}`;
}
function scaleToFit(inputSize, maxSize, toggles = {
onlyScaleDown: false,
onlyScaleUp: false,
uniform: true,
}) {
let scaleFactorWidth = maxSize[0] / inputSize[0];
let scaleFactorHeight = maxSize[1] / inputSize[1];
let scaleFactor = Math.min(scaleFactorWidth, scaleFactorHeight);
if (toggles.onlyScaleDown) {
scaleFactor = Math.min(scaleFactor, 1);
scaleFactorWidth = Math.min(scaleFactorWidth, 1);
scaleFactorHeight = Math.min(scaleFactorHeight, 1);
}
if (toggles.onlyScaleUp) {
scaleFactor = Math.max(scaleFactor, 1);
scaleFactorWidth = Math.max(scaleFactorWidth, 1);
scaleFactorHeight = Math.max(scaleFactorHeight, 1);
}
return toggles.uniform
? [100 * scaleFactor, 100 * scaleFactor]
: [100 * scaleFactorWidth, 100 * scaleFactorHeight];
}
function addLineBreaks(string, maxCharacters) {
const splitRegex = new RegExp('(.{' + maxCharacters + '}[^ ]* )', 'g');
return string.replace(splitRegex, '$1\n');
}
function hasAShortLine(string, minWords) {
const lines = string.split('\n');
if (lines.length == 1) {
return false;
}
for (let index = 0; index < lines.length; index++) {
const line = lines[index];
const words = line.split(' ');
if (words.length <= minWords) {
return true;
}
}
return false;
}
function breakWithoutOrphans(string, maxCharacters, minWords, options = {
minCharacters: 12,
characterStep: 4,
}) {
function smartBreak(string, maxCharacters, minWords, options) {
const brokenString = addLineBreaks(string, maxCharacters);
if (!hasAShortLine(brokenString, minWords) ||
maxCharacters < options.minCharacters) {
return brokenString;
}
return smartBreak(string, maxCharacters - options.characterStep, minWords, options);
}
return smartBreak(string, maxCharacters, minWords, options);
}
function maintainScale(parentLayer = thisLayer.parent) {
if (typeof thisLayer.transform === 'undefined') {
throw funcError('maintainScale', `Current layer (${thisLayer.name}) doesn't have transform values`);
}
if (typeof parentLayer.transform === 'undefined') {
throw funcError('maintainScale', `Parent layer (${thisLayer.name}) doesn't have transform values`);
}
return thisLayer.transform.scale.value.map((scale, index) => scale
? (scale * 100) / (parentLayer.transform.scale.value[index] || 0)
: 0);
}
function offsetFromAnchor(position, [offsetX, offsetY], anchor) {
switch (anchor) {
case 'topLeft':
return thisLayer.add(position, [-offsetX, -offsetY]);
case 'topRight':
return thisLayer.add(position, [offsetX, -offsetY]);
case 'bottomRight':
return thisLayer.add(position, [offsetX, offsetY]);
case 'bottomLeft':
return thisLayer.add(position, [-offsetX, offsetY]);
default:
throw Error('Invalid anchor: ' + anchor);
}
}
return {
attachKeys,
bounceKeys,
getPathFromPoints,
gridPoints,
hideLayerWhenBelow,
getIsometricPosition,
getLayerBoundsPath,
layerSize,
layerRect,
textCount,
commaNum,
cleanLines,
hideDescenders,
getKeyframesAsArray,
circularMotion,
circularPosition,
countdown,
scaleToFit,
breakWithoutOrphans,
maintainScale,
offsetFromAnchor,
addLineBreaks,
};
},
version: '2.0.3',
}