80 lines
3.2 KiB
JavaScript
80 lines
3.2 KiB
JavaScript
|
/**
|
||
|
* Determines the distance between two points.
|
||
|
*
|
||
|
* @public
|
||
|
*/
|
||
|
/* eslint-disable deprecation/deprecation */
|
||
|
export function getDistanceBetweenPoints(point1, point2) {
|
||
|
var left1 = point1.left || point1.x || 0;
|
||
|
var top1 = point1.top || point1.y || 0;
|
||
|
var left2 = point2.left || point2.x || 0;
|
||
|
var top2 = point2.top || point2.y || 0;
|
||
|
/* eslint-enable deprecation/deprecation */
|
||
|
var distance = Math.sqrt(Math.pow(left1 - left2, 2) + Math.pow(top1 - top2, 2));
|
||
|
return distance;
|
||
|
}
|
||
|
/**
|
||
|
* Produces a proportionally-scaled version of an input content size when fit to a bounding size.
|
||
|
* Given a `contentSize` and a `boundsSize`, this function scales `contentSize` proportionally
|
||
|
* using either `contain` or `cover` fit behaviors.
|
||
|
* Use this function to pre-calculate the layout for the CSS `object-fit` and `background-fit` behaviors.
|
||
|
* With `contain`, the output size must be the largest it can be while completely within the `boundsSize`.
|
||
|
* With `cover`, the output size must be the smallest it can be while completely around the `boundsSize`.
|
||
|
* By default, there is a `maxScale` value of 1, which prevents the `contentSize` from being scaled larger.
|
||
|
*
|
||
|
* @param options - the options for the bounds fit operation
|
||
|
*/
|
||
|
export function fitContentToBounds(options) {
|
||
|
var contentSize = options.contentSize, boundsSize = options.boundsSize, _a = options.mode, mode = _a === void 0 ? 'contain' : _a, _b = options.maxScale, maxScale = _b === void 0 ? 1 : _b;
|
||
|
var contentAspectRatio = contentSize.width / contentSize.height;
|
||
|
var boundsAspectRatio = boundsSize.width / boundsSize.height;
|
||
|
var scale;
|
||
|
if (mode === 'contain' ? contentAspectRatio > boundsAspectRatio : contentAspectRatio < boundsAspectRatio) {
|
||
|
scale = boundsSize.width / contentSize.width;
|
||
|
}
|
||
|
else {
|
||
|
scale = boundsSize.height / contentSize.height;
|
||
|
}
|
||
|
var finalScale = Math.min(maxScale, scale);
|
||
|
return {
|
||
|
width: contentSize.width * finalScale,
|
||
|
height: contentSize.height * finalScale,
|
||
|
};
|
||
|
}
|
||
|
/**
|
||
|
* Calculates a number's precision based on the number of trailing
|
||
|
* zeros if the number does not have a decimal indicated by a negative
|
||
|
* precision. Otherwise, it calculates the number of digits after
|
||
|
* the decimal point indicated by a positive precision.
|
||
|
* @param value - the value to determine the precision of
|
||
|
*/
|
||
|
export function calculatePrecision(value) {
|
||
|
/**
|
||
|
* Group 1:
|
||
|
* [1-9]([0]+$) matches trailing zeros
|
||
|
* Group 2:
|
||
|
* \.([0-9]*) matches all digits after a decimal point.
|
||
|
*/
|
||
|
var groups = /[1-9]([0]+$)|\.([0-9]*)/.exec(String(value));
|
||
|
if (!groups) {
|
||
|
return 0;
|
||
|
}
|
||
|
if (groups[1]) {
|
||
|
return -groups[1].length;
|
||
|
}
|
||
|
if (groups[2]) {
|
||
|
return groups[2].length;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
/**
|
||
|
* Rounds a number to a certain level of precision. Accepts negative precision.
|
||
|
* @param value - The value that is being rounded.
|
||
|
* @param precision - The number of decimal places to round the number to
|
||
|
*/
|
||
|
export function precisionRound(value, precision, base) {
|
||
|
if (base === void 0) { base = 10; }
|
||
|
var exp = Math.pow(base, precision);
|
||
|
return Math.round(value * exp) / exp;
|
||
|
}
|
||
|
//# sourceMappingURL=math.js.map
|