UNPKG

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