- fixed curves being parsed improperly due to parseInt() instead of parseFloat() - fixed reverse-referencing when filling in undefined keyframes - made it possible to configure -r and -c in env variables KE_ROUND and KE_CURVE
209 lines
5.8 KiB
JavaScript
209 lines
5.8 KiB
JavaScript
const BezierEasing = require("bezier-easing");
|
|
const { program } = require("commander");
|
|
|
|
const KEYFRAME_BASE = { value: "" };
|
|
|
|
const KE_CURVE = process.env.KE_CURVE || "0.23, 1, 0.32, 1";
|
|
const KE_ROUND = process.env.KE_ROUND === "true" || false;
|
|
|
|
program
|
|
.requiredOption("-c, --curve <ax,ay,bx,by>", "the cubic bezier curve to use, in the format of CSS curves.\ncan also be set by the environment variable KE_CURVE", KE_CURVE)
|
|
.requiredOption("-vr, --value-range <a..b>", "the starting and ending value of the ease.\nan empty value on either side will resolve to the values of the first and last frame, respectively.\nif no stdin is supplied, both of those will be 0")
|
|
.requiredOption("-fr, --frame-range <a..b>", "the start and end frames for the ease.\nlike -vr, an empty value will resolve to the first and last frames from stdin, but will not work without -i")
|
|
.requiredOption("-p, --property <p>", "the property to apply the ease to; can be x, y, w, or h")
|
|
.option("-s, --skip <frames>", "skip every <frames> frames", 0)
|
|
.option("-rh, --reverse-horizontal", "reverse the curve horizontally")
|
|
.option("-rv, --reverse-vertical", "reverse the curve vertically")
|
|
.option("-o, --output-plain", "output the values in this format: frameNumber:value")
|
|
.option("-i, --stdin", "read the base keyframe data from stdin")
|
|
.option("-r, --round", "round all generated values.\nyou can set this in the KE_ROUND environment variable", KE_ROUND)
|
|
.addHelpText("beforeAll", "kdenease.js - generate eases for kdenlive using a cubic bezier curve")
|
|
.addHelpText("after", "you can use -i to read keyframe data from stdin and overwrite the selected values to the generated values.");
|
|
|
|
function decodeKeyframes(kfString) {
|
|
let out = {};
|
|
for (let kf of kfString.split(";")) {
|
|
kf = kf.split("=");
|
|
const frame = parseInt(kf[0]);
|
|
const [x, y, w, h] = kf[1].split(" ");
|
|
out[frame] = { x: x, y: y, w: w, h: h };
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function encodeKeyframes(kfs) {
|
|
let out = [];
|
|
for (let f in kfs) {
|
|
out.push(`${f}=${kfs[f].x} ${kfs[f].y} ${kfs[f].w} ${kfs[f].h}`);
|
|
}
|
|
return out.join(";");
|
|
}
|
|
|
|
function calculate(
|
|
ax,
|
|
ay,
|
|
bx,
|
|
by,
|
|
valueStart,
|
|
valueEnd,
|
|
frameStart,
|
|
frameEnd,
|
|
skip,
|
|
round
|
|
) {
|
|
let out = {};
|
|
const ease = BezierEasing(ax, ay, bx, by);
|
|
for (let frame = frameStart; frame <= frameEnd; frame += skip + 1) {
|
|
const interval = (frame - frameStart) / (frameEnd - frameStart);
|
|
const newVal = valueStart + ((valueEnd - valueStart) * ease(interval));
|
|
out[frame] = round ? Math.round(newVal) : newVal;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function waitForStdin() {
|
|
return new Promise((resolve, reject) => {
|
|
let out = "";
|
|
process.stdin.setEncoding("utf8");
|
|
process.stdin.on("data", (chunk) => { out += chunk });
|
|
process.stdin.on("end", () => { resolve(out) });
|
|
});
|
|
}
|
|
|
|
function reverseCurve([ax, ay, bx, by], horizontal, vertical) {
|
|
if (horizontal) {
|
|
const temp = bx;
|
|
bx = 0.5 + (0.5 - ax);
|
|
ax = 0.5 + (0.5 - temp);
|
|
}
|
|
|
|
if (vertical) {
|
|
const temp = by;
|
|
by = 0.5 + (0.5 - ay);
|
|
ay = 0.5 + (0.5 - by);
|
|
}
|
|
|
|
return [ax, ay, bx, by];
|
|
}
|
|
|
|
function panic(a) {
|
|
console.error(a);
|
|
process.exit(1);
|
|
}
|
|
|
|
function getFirstLastFrames(values) {
|
|
const keys = Object.keys(values);
|
|
return [
|
|
parseInt(firstFrame = keys[0]),
|
|
parseInt(lastFrame = keys[keys.length - 1])
|
|
];
|
|
}
|
|
|
|
async function main() {
|
|
program.parse();
|
|
const o = program.opts();
|
|
|
|
if (!["x", "y", "w", "h"].includes(o.property)) {
|
|
panic("property not in x, y, w, or h");
|
|
}
|
|
|
|
let baseJSON = {
|
|
"in": 0,
|
|
"max": 0,
|
|
"min": 0,
|
|
"name": "transition.geometry",
|
|
"out": 150,
|
|
"type": 6
|
|
};
|
|
|
|
let firstFrame, lastFrame;
|
|
|
|
if (o.stdin) {
|
|
baseJSON = JSON.parse(await waitForStdin())[0];
|
|
baseJSON.value = decodeKeyframes(baseJSON.value);
|
|
[firstFrame, lastFrame] = getFirstLastFrames(baseJSON.value);
|
|
}
|
|
|
|
const [frameStart, frameEnd] = (() => {
|
|
let vals = ` ${o.frameRange} `.split("..");
|
|
if (vals.includes(" ") && !o.stdin) {
|
|
panic("cannot resolve frame from nonexistent input!\nif you meant to resolve to the start or end frame, remember to pipe in keyframe data from Kdenlive and use -i.\notherwise, you made a typo");
|
|
}
|
|
|
|
return [
|
|
parseInt(vals[0] == " " ? firstFrame : vals[0]),
|
|
parseInt(vals[1] == " " ? lastFrame : vals[1])
|
|
];
|
|
})();
|
|
|
|
if (!o.stdin) {
|
|
baseJSON.value = {};
|
|
for (let frame = frameStart; frame <= frameEnd; frame += o.skip + 1) {
|
|
baseJSON.value[frame] = { x: 0, y: 0, w: 0, h: 0 };
|
|
}
|
|
[firstFrame, lastFrame] = getFirstLastFrames(baseJSON.value);
|
|
}
|
|
|
|
const [ax, ay, bx, by] = reverseCurve(
|
|
o.curve.split(",").map((e) => parseFloat(e)),
|
|
o.reverseHorizontal,
|
|
o.reverseVertical
|
|
);
|
|
|
|
const [valueStart, valueEnd] = (() => {
|
|
let vals = ` ${o.valueRange} `.split(".."); // spaces are to allow for empty values
|
|
return [
|
|
parseInt(vals[0] == " "
|
|
? baseJSON.value[firstFrame][o.property]
|
|
: vals[0]
|
|
),
|
|
parseInt(vals[1] == " "
|
|
? baseJSON.value[lastFrame][o.property]
|
|
: vals[1]
|
|
)
|
|
];
|
|
})();
|
|
|
|
/*
|
|
console.dir({
|
|
valueRange: [valueStart, valueEnd],
|
|
frameRange: [frameStart, frameEnd]
|
|
});
|
|
*/
|
|
|
|
const newValues = calculate(
|
|
ax,
|
|
ay,
|
|
bx,
|
|
by,
|
|
valueStart,
|
|
valueEnd,
|
|
frameStart,
|
|
frameEnd,
|
|
o.skip,
|
|
o.round
|
|
);
|
|
|
|
if (o.outputPlain) {
|
|
for (let frame in newValues) {
|
|
console.log(`${frame}:${newValues[frame]}`);
|
|
}
|
|
} else {
|
|
for (let frame in newValues) {
|
|
if (!baseJSON.value.hasOwnProperty(frame)) {
|
|
const lastFrame = (frame - 1).toString();
|
|
const l = baseJSON.value[lastFrame];
|
|
baseJSON.value[frame] = { x: l.x, y: l.y, w: l.w, h: l.h };
|
|
}
|
|
|
|
baseJSON.value[frame][o.property] = newValues[frame];
|
|
}
|
|
|
|
baseJSON.value = encodeKeyframes(baseJSON.value);
|
|
|
|
console.log(JSON.stringify([baseJSON]));
|
|
}
|
|
}
|
|
|
|
main();
|