@thi.ng/imago
Version:
JSON & API-based declarative and extensible image processing trees/pipelines
54 lines (53 loc) • 1.36 kB
JavaScript
import { isNumber } from "@thi.ng/checks";
import { illegalArgs } from "@thi.ng/errors";
import {
computeMargins,
computeSize,
computeSizeWithAspect,
gravityPosition,
positionOrGravity
} from "../utils.js";
const cropProc = async (spec, input, ctx) => {
const { aspect, border, gravity, pos, size, ref, unit } = spec;
if (border == null && size == null)
illegalArgs("require `border` or `size` option");
if (border != null) {
const sides = computeMargins(border, ctx.size, ref, unit);
const [left2, right, top2, bottom] = sides;
return [
input.extract({
left: left2,
top: top2,
width: ctx.size[0] - left2 - right,
height: ctx.size[1] - top2 - bottom
}),
true
];
}
let $size;
if (aspect != void 0) {
if (!isNumber(size))
illegalArgs("size must be numeric if aspect is used");
$size = computeSizeWithAspect(size, ctx.size, aspect, unit);
} else {
$size = computeSize(size, ctx.size, ref, unit);
}
let left = 0, top = 0;
if (pos) {
({ left = 0, top = 0 } = positionOrGravity($size, ctx.size, spec) || {});
} else {
[left, top] = gravityPosition(gravity || "c", $size, ctx.size);
}
return [
input.extract({
left,
top,
width: $size[0],
height: $size[1]
}),
true
];
};
export {
cropProc
};