@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
251 lines • 9.73 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import { RectangleF } from "@aurigma/design-atoms-model/Math/RectangleF";
import { PointF, Transform, RotatedRectangleF, EqualsOfFloatNumbers } from "@aurigma/design-atoms-model/Math";
import { ArgumentException } from "@aurigma/design-atoms-model";
import { OriginPointType, ResizeRectangleType } from "@aurigma/design-atoms-interfaces";
export var ISize;
(function (ISize) {
function from(avoSize) {
return {
width: avoSize.width,
height: avoSize.height
};
}
ISize.from = from;
})(ISize || (ISize = {}));
export function rectangleEquals(rec1, rec2, tolerance) {
return EqualsOfFloatNumbers(rec1.left, rec2.left, tolerance) &&
EqualsOfFloatNumbers(rec1.top, rec2.top, tolerance) &&
EqualsOfFloatNumbers(rec1.width, rec2.width, tolerance) &&
EqualsOfFloatNumbers(rec1.height, rec2.height, tolerance);
}
export function fitSizeToSize(fittingSize, targetSize, resizeType) {
if (resizeType === void 0) { resizeType = ResizeRectangleType.Fit; }
if (fittingSize.width === targetSize.width && fittingSize.height === targetSize.height)
return fittingSize;
if (resizeType === ResizeRectangleType.Original)
return __assign({}, fittingSize);
if (resizeType === ResizeRectangleType.Arbitrary)
return __assign({}, targetSize);
var multiplier = resizeType === ResizeRectangleType.Fit ?
Math.min(targetSize.width / fittingSize.width, targetSize.height / fittingSize.height) :
Math.max(targetSize.width / fittingSize.width, targetSize.height / fittingSize.height);
return {
width: fittingSize.width * multiplier,
height: fittingSize.height * multiplier
};
}
export function fitRectangleToRectangle(fittingRect, targetRect, resizeType, originX, originY) {
if (resizeType === void 0) { resizeType = ResizeRectangleType.Fit; }
if (originX === void 0) { originX = OriginPointType.Center; }
if (originY === void 0) { originY = OriginPointType.Center; }
if (RectangleF.isEqual(fittingRect, targetRect))
return fittingRect.clone();
var fittingSize = fitSizeToSize({ width: fittingRect.width, height: fittingRect.height }, { width: targetRect.width, height: targetRect.height }, resizeType);
var x;
var y;
switch (originX) {
case OriginPointType.Left:
x = targetRect.left;
break;
case OriginPointType.Right:
x = targetRect.right - fittingSize.width;
break;
default:
x = targetRect.center.x - fittingSize.width / 2;
break;
}
switch (originY) {
case OriginPointType.Top:
y = targetRect.top;
break;
case OriginPointType.Bottom:
y = targetRect.bottom - fittingSize.height;
break;
default:
y = targetRect.center.y - fittingSize.height / 2;
break;
}
var resultRect = new RectangleF(x, y, fittingSize.width, fittingSize.height);
return resultRect;
}
export function alignRectToRect(sourceRect, targetRect, horizontalAlignment, verticalAlignment) {
sourceRect.angle = targetRect.angle;
var anchorPoint = getRectPoint(targetRect, horizontalAlignment, verticalAlignment);
var point = getRectPoint(sourceRect, horizontalAlignment, verticalAlignment);
var result = sourceRect.clone();
result.centerX += anchorPoint.x - point.x;
result.centerY += anchorPoint.y - point.y;
return result;
}
export function getRectPoint(rect, horizontal, vertical) {
var x = 0;
var y = 0;
switch (horizontal) {
case OriginPointType.Left:
x = -rect.width / 2;
break;
case OriginPointType.Right:
x = rect.width / 2;
break;
}
switch (vertical) {
case OriginPointType.Top:
y = -rect.height / 2;
break;
case OriginPointType.Bottom:
y = rect.height / 2;
break;
}
var p = new PointF(x, y);
p = p.rotate(rect.angle);
p = p.translate(rect.centerX, rect.centerY);
return p;
}
export function snapValueToStep(oldVal, newVal, step) {
var direction = Math.sign(newVal - oldVal);
var index = oldVal / step;
if (EqualsOfFloatNumbers(index, Math.round(index))) {
index = Math.round(index);
}
index = Math.floor(index);
if (direction == -1 && !EqualsOfFloatNumbers(index * step, oldVal)) {
return index * step;
}
return (index + direction) * step;
}
/**
* Clamps a value to a range [min, max].
* @param value The value for clamping.
* @param min The minimum values. If the input value is less than this value, the output will be this value.
* @param max The maximum value. If the input value is more than this value, the output will be this value.
*/
export function clamp(value, min, max) {
if (max != null) {
value = Math.min(value, max);
}
if (min != null) {
value = Math.max(min, value);
}
return value;
}
export function getOriginCoordinate(rect, originX, originY) {
var x;
var y;
switch (originX) {
case OriginPointType.Left:
x = rect.left;
break;
case OriginPointType.Right:
x = rect.right;
break;
default:
x = rect.left + rect.width / 2;
break;
}
switch (originY) {
case OriginPointType.Top:
y = rect.top;
break;
case OriginPointType.Bottom:
y = rect.bottom;
break;
default:
y = rect.top + rect.height / 2;
break;
}
return new PointF(x, y);
}
;
export function getTranslationTransform(source, target) {
return new Transform(1, 1, target.x - source.x, target.y - source.y, 0);
}
export function minByAbs() {
var e_1, _a;
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i] = arguments[_i];
}
var result;
var leastAbsValue = Number.POSITIVE_INFINITY;
try {
for (var values_1 = __values(values), values_1_1 = values_1.next(); !values_1_1.done; values_1_1 = values_1.next()) {
var value = values_1_1.value;
var current = Math.abs(value);
if (current < leastAbsValue) {
leastAbsValue = current;
result = value;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (values_1_1 && !values_1_1.done && (_a = values_1.return)) _a.call(values_1);
}
finally { if (e_1) throw e_1.error; }
}
return result;
}
export function getResizedImageAxisDpi(sourceSize, sourceDpi, targetSize) {
return Math.abs(sourceSize * sourceDpi / targetSize);
}
export function getResizedImageMinAxisDpi(sourceWidth, sourceHeight, sourceDpiX, sourceDpiY, targetWidth, targetHeight) {
var dpiX = Math.abs(sourceWidth * sourceDpiX / targetWidth);
var dpiY = Math.abs(sourceHeight * sourceDpiY / targetHeight);
return Math.min(dpiX, dpiY);
}
/**
* Maps the given value from the range [oldMin, oldMax] to the range [newMin, newMax].
* Where each min is less than corresponding max.
* @param value The value to map.
* @param oldMin The minimum value of the old range. Must be less than {@link oldMax}.
* @param oldMax The maximum value of the old range. Must be greater than {@link oldMin}.
* @param newMin The minimum value of the new range. Must be less than {@link newMax}.
* @param newMax The maximum value of the new range. Must be greater than {@link newMin}.
*/
export function mapToRange(value, oldMin, oldMax, newMin, newMax) {
if (oldMin >= oldMax) {
throw new ArgumentException("oldMin must be less than oldMax");
}
if (newMin >= newMax) {
throw new ArgumentException("newMin must be less than newMax");
}
return newMin + (value - oldMin) * (newMax - newMin) / (oldMax - oldMin);
}
export function getRotatedRectangleWithBorder(rectangle, border) {
if (rectangle == null || border == null)
return null;
var oldCenter = rectangle.center;
var angle = rectangle.angle;
rectangle.angle = 0;
var bounds = rectangle.bounds;
var result = RotatedRectangleF.FromLTRB(bounds.left - border.left, bounds.top - border.top, bounds.right + border.right, bounds.bottom + border.bottom);
result.width *= Math.sign(rectangle.width);
result.height *= Math.sign(rectangle.height);
result.rotateAt(angle, oldCenter);
rectangle.angle = angle;
return result;
}
//# sourceMappingURL=Math.js.map