@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
210 lines • 8.18 kB
JavaScript
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 = ResizeRectangleType.Fit) {
if (fittingSize.width === targetSize.width && fittingSize.height === targetSize.height)
return fittingSize;
if (resizeType === ResizeRectangleType.Original)
return Object.assign({}, fittingSize);
if (resizeType === ResizeRectangleType.Arbitrary)
return Object.assign({}, targetSize);
const 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 = ResizeRectangleType.Fit, originX = OriginPointType.Center, originY = OriginPointType.Center) {
if (RectangleF.isEqual(fittingRect, targetRect))
return fittingRect.clone();
const fittingSize = fitSizeToSize({ width: fittingRect.width, height: fittingRect.height }, { width: targetRect.width, height: targetRect.height }, resizeType);
let x;
let 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;
}
const resultRect = new RectangleF(x, y, fittingSize.width, fittingSize.height);
return resultRect;
}
export function alignRectToRect(sourceRect, targetRect, horizontalAlignment, verticalAlignment) {
sourceRect.angle = targetRect.angle;
let anchorPoint = getRectPoint(targetRect, horizontalAlignment, verticalAlignment);
let point = getRectPoint(sourceRect, horizontalAlignment, verticalAlignment);
const 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) {
const direction = Math.sign(newVal - oldVal);
let 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) {
let x;
let 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(...values) {
let result;
let leastAbsValue = Number.POSITIVE_INFINITY;
for (let value of values) {
const current = Math.abs(value);
if (current < leastAbsValue) {
leastAbsValue = current;
result = value;
}
}
return result;
}
export function getResizedImageAxisDpi(sourceSize, sourceDpi, targetSize) {
return Math.abs(sourceSize * sourceDpi / targetSize);
}
export function getResizedImageMinAxisDpi(sourceWidth, sourceHeight, sourceDpiX, sourceDpiY, targetWidth, targetHeight) {
let dpiX = Math.abs(sourceWidth * sourceDpiX / targetWidth);
let 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;
const oldCenter = rectangle.center;
const angle = rectangle.angle;
rectangle.angle = 0;
const bounds = rectangle.bounds;
const 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