fixed bugs, env variable configuration

- 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
This commit is contained in:
skybldev
2022-01-29 11:09:30 -05:00
parent f3402ebd73
commit 1ee8ab8e74

View File

@@ -3,8 +3,11 @@ const { program } = require("commander");
const KEYFRAME_BASE = { value: "" }; 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 program
.requiredOption("-c, --curve <ax,ay,bx,by>", "the cubic bezier curve to use, in the format of CSS curves", "0.23, 1, 0.32, 1") .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("-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("-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") .requiredOption("-p, --property <p>", "the property to apply the ease to; can be x, y, w, or h")
@@ -13,8 +16,8 @@ program
.option("-rv, --reverse-vertical", "reverse the curve vertically") .option("-rv, --reverse-vertical", "reverse the curve vertically")
.option("-o, --output-plain", "output the values in this format: frameNumber:value") .option("-o, --output-plain", "output the values in this format: frameNumber:value")
.option("-i, --stdin", "read the base keyframe data from stdin") .option("-i, --stdin", "read the base keyframe data from stdin")
.option("-r, --round", "round all generated values") .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\n") .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."); .addHelpText("after", "you can use -i to read keyframe data from stdin and overwrite the selected values to the generated values.");
function decodeKeyframes(kfString) { function decodeKeyframes(kfString) {
@@ -30,8 +33,8 @@ function decodeKeyframes(kfString) {
function encodeKeyframes(kfs) { function encodeKeyframes(kfs) {
let out = []; let out = [];
for (let e in kfs) { for (let f in kfs) {
out.push(`${e}=${kfs[e].x} ${kfs[e].y} ${kfs[e].w} ${kfs[e].h}`); out.push(`${f}=${kfs[f].x} ${kfs[f].y} ${kfs[f].w} ${kfs[f].h}`);
} }
return out.join(";"); return out.join(";");
} }
@@ -51,8 +54,8 @@ function calculate(
let out = {}; let out = {};
const ease = BezierEasing(ax, ay, bx, by); const ease = BezierEasing(ax, ay, bx, by);
for (let frame = frameStart; frame <= frameEnd; frame += skip + 1) { for (let frame = frameStart; frame <= frameEnd; frame += skip + 1) {
const scaled = (frame - frameStart) / (frameEnd - frameStart); const interval = (frame - frameStart) / (frameEnd - frameStart);
const newVal = valueStart + ((valueEnd - valueStart) * ease(scaled)); const newVal = valueStart + ((valueEnd - valueStart) * ease(interval));
out[frame] = round ? Math.round(newVal) : newVal; out[frame] = round ? Math.round(newVal) : newVal;
} }
return out; return out;
@@ -118,7 +121,6 @@ async function main() {
if (o.stdin) { if (o.stdin) {
baseJSON = JSON.parse(await waitForStdin())[0]; baseJSON = JSON.parse(await waitForStdin())[0];
baseJSON.value = decodeKeyframes(baseJSON.value); baseJSON.value = decodeKeyframes(baseJSON.value);
console.dir(baseJSON);
[firstFrame, lastFrame] = getFirstLastFrames(baseJSON.value); [firstFrame, lastFrame] = getFirstLastFrames(baseJSON.value);
} }
@@ -136,18 +138,14 @@ async function main() {
if (!o.stdin) { if (!o.stdin) {
baseJSON.value = {}; baseJSON.value = {};
for ( for (let frame = frameStart; frame <= frameEnd; frame += o.skip + 1) {
let frame = parseInt(frameStart);
frame <= frameEnd;
frame += o.skip + 1
) {
baseJSON.value[frame] = { x: 0, y: 0, w: 0, h: 0 }; baseJSON.value[frame] = { x: 0, y: 0, w: 0, h: 0 };
} }
[firstFrame, lastFrame] = getFirstLastFrames(baseJSON.value); [firstFrame, lastFrame] = getFirstLastFrames(baseJSON.value);
} }
const [ax, ay, bx, by] = reverseCurve( const [ax, ay, bx, by] = reverseCurve(
o.curve.split(",").map((e) => parseInt(e)), o.curve.split(",").map((e) => parseFloat(e)),
o.reverseHorizontal, o.reverseHorizontal,
o.reverseVertical o.reverseVertical
); );
@@ -165,11 +163,13 @@ async function main() {
) )
]; ];
})(); })();
/*
console.dir({ console.dir({
valueRange: [valueStart, valueEnd], valueRange: [valueStart, valueEnd],
frameRange: [frameStart, frameEnd] frameRange: [frameStart, frameEnd]
}); });
*/
const newValues = calculate( const newValues = calculate(
ax, ax,
@@ -184,16 +184,19 @@ async function main() {
o.round o.round
); );
console.dir(newValues);
if (o.outputPlain) { if (o.outputPlain) {
for (let frame in newValues) { for (let frame in newValues) {
console.log(`${frame}:${newValues[frame]}`); console.log(`${frame}:${newValues[frame]}`);
} }
} else { } else {
for (let frame in newValues) { for (let frame in newValues) {
//baseJSON.value[frame][o.property] = newValues[frame]; if (!baseJSON.value.hasOwnProperty(frame)) {
console.dir(baseJSON.value[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); baseJSON.value = encodeKeyframes(baseJSON.value);