UNPKG

3.24 kBJavaScriptView Raw
1/**
2 * Determines the distance between two points.
3 *
4 * @public
5 */
6/* eslint-disable deprecation/deprecation */
7export function getDistanceBetweenPoints(point1, point2) {
8 var left1 = point1.left || point1.x || 0;
9 var top1 = point1.top || point1.y || 0;
10 var left2 = point2.left || point2.x || 0;
11 var top2 = point2.top || point2.y || 0;
12 /* eslint-enable deprecation/deprecation */
13 var distance = Math.sqrt(Math.pow(left1 - left2, 2) + Math.pow(top1 - top2, 2));
14 return distance;
15}
16/**
17 * Produces a proportionally-scaled version of an input content size when fit to a bounding size.
18 * Given a `contentSize` and a `boundsSize`, this function scales `contentSize` proportionally
19 * using either `contain` or `cover` fit behaviors.
20 * Use this function to pre-calculate the layout for the CSS `object-fit` and `background-fit` behaviors.
21 * With `contain`, the output size must be the largest it can be while completely within the `boundsSize`.
22 * With `cover`, the output size must be the smallest it can be while completely around the `boundsSize`.
23 * By default, there is a `maxScale` value of 1, which prevents the `contentSize` from being scaled larger.
24 *
25 * @param options - the options for the bounds fit operation
26 */
27export function fitContentToBounds(options) {
28 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;
29 var contentAspectRatio = contentSize.width / contentSize.height;
30 var boundsAspectRatio = boundsSize.width / boundsSize.height;
31 var scale;
32 if (mode === 'contain' ? contentAspectRatio > boundsAspectRatio : contentAspectRatio < boundsAspectRatio) {
33 scale = boundsSize.width / contentSize.width;
34 }
35 else {
36 scale = boundsSize.height / contentSize.height;
37 }
38 var finalScale = Math.min(maxScale, scale);
39 return {
40 width: contentSize.width * finalScale,
41 height: contentSize.height * finalScale,
42 };
43}
44/**
45 * Calculates a number's precision based on the number of trailing
46 * zeros if the number does not have a decimal indicated by a negative
47 * precision. Otherwise, it calculates the number of digits after
48 * the decimal point indicated by a positive precision.
49 * @param value - the value to determine the precision of
50 */
51export function calculatePrecision(value) {
52 /**
53 * Group 1:
54 * [1-9]([0]+$) matches trailing zeros
55 * Group 2:
56 * \.([0-9]*) matches all digits after a decimal point.
57 */
58 var groups = /[1-9]([0]+$)|\.([0-9]*)/.exec(String(value));
59 if (!groups) {
60 return 0;
61 }
62 if (groups[1]) {
63 return -groups[1].length;
64 }
65 if (groups[2]) {
66 return groups[2].length;
67 }
68 return 0;
69}
70/**
71 * Rounds a number to a certain level of precision. Accepts negative precision.
72 * @param value - The value that is being rounded.
73 * @param precision - The number of decimal places to round the number to
74 */
75export function precisionRound(value, precision, base) {
76 if (base === void 0) { base = 10; }
77 var exp = Math.pow(base, precision);
78 return Math.round(value * exp) / exp;
79}
80//# sourceMappingURL=math.js.map
\No newline at end of file