diff --git a/index.js b/index.js index 69fd0a7..f1b1f48 100644 --- a/index.js +++ b/index.js @@ -3,8 +3,11 @@ 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 ", "the cubic bezier curve to use, in the format of CSS curves", "0.23, 1, 0.32, 1") + .requiredOption("-c, --curve ", "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 ", "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 ", "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

", "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("-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") - .addHelpText("beforeAll", "kdenease.js - generate eases for kdenlive using a cubic bezier curve\n") + .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) { @@ -30,8 +33,8 @@ function decodeKeyframes(kfString) { function encodeKeyframes(kfs) { let out = []; - for (let e in kfs) { - out.push(`${e}=${kfs[e].x} ${kfs[e].y} ${kfs[e].w} ${kfs[e].h}`); + for (let f in kfs) { + out.push(`${f}=${kfs[f].x} ${kfs[f].y} ${kfs[f].w} ${kfs[f].h}`); } return out.join(";"); } @@ -51,8 +54,8 @@ function calculate( let out = {}; const ease = BezierEasing(ax, ay, bx, by); for (let frame = frameStart; frame <= frameEnd; frame += skip + 1) { - const scaled = (frame - frameStart) / (frameEnd - frameStart); - const newVal = valueStart + ((valueEnd - valueStart) * ease(scaled)); + const interval = (frame - frameStart) / (frameEnd - frameStart); + const newVal = valueStart + ((valueEnd - valueStart) * ease(interval)); out[frame] = round ? Math.round(newVal) : newVal; } return out; @@ -118,7 +121,6 @@ async function main() { if (o.stdin) { baseJSON = JSON.parse(await waitForStdin())[0]; baseJSON.value = decodeKeyframes(baseJSON.value); - console.dir(baseJSON); [firstFrame, lastFrame] = getFirstLastFrames(baseJSON.value); } @@ -136,18 +138,14 @@ async function main() { if (!o.stdin) { baseJSON.value = {}; - for ( - let frame = parseInt(frameStart); - frame <= frameEnd; - frame += o.skip + 1 - ) { + 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) => parseInt(e)), + o.curve.split(",").map((e) => parseFloat(e)), o.reverseHorizontal, o.reverseVertical ); @@ -165,11 +163,13 @@ async function main() { ) ]; })(); - + + /* console.dir({ valueRange: [valueStart, valueEnd], frameRange: [frameStart, frameEnd] }); + */ const newValues = calculate( ax, @@ -184,16 +184,19 @@ async function main() { o.round ); - console.dir(newValues); - if (o.outputPlain) { for (let frame in newValues) { console.log(`${frame}:${newValues[frame]}`); } } else { for (let frame in newValues) { - //baseJSON.value[frame][o.property] = newValues[frame]; - console.dir(baseJSON.value[frame]); + 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);