Browse Source
related helper funcitons were moved into a corresponding filedev-feat-component_preview
2 changed files with 84 additions and 85 deletions
@ -0,0 +1,84 @@ |
|||
/** |
|||
* |
|||
* @param {number} start |
|||
* @param {number} end |
|||
* @param {number} value |
|||
* @param {number} tolerance |
|||
* @param {boolean} usePercentage |
|||
* @returns {boolean} |
|||
*/ |
|||
function isValueInBounds(start, end, value, tolerance = 0, usePercentage = false) { |
|||
if (tolerance !== 0) { |
|||
if (usePercentage) { |
|||
start = start * (1 - tolerance / 100); |
|||
end = end * (1 + tolerance / 100); |
|||
} else { |
|||
start = start - tolerance; |
|||
end = end + tolerance; |
|||
} |
|||
} |
|||
|
|||
return value >= start && value <= end; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* @param {number} x |
|||
* @param {number} y |
|||
* @param {Map<SideDirections,number>} area |
|||
* @param {number} tolerance |
|||
* @param {boolean} usePercentage if tolerance is given and this is set to true, |
|||
* the tolerance will be calculated by percentage, |
|||
* otherwise it will be subtracted/added |
|||
* @returns {boolean} |
|||
*/ |
|||
function areXYInArea(x, y, area, tolerance = 0, usePercentage = false) { |
|||
return isValueInBounds( |
|||
area.get(SideDirections.LEFT), |
|||
area.get(SideDirections.RIGHT), |
|||
x, |
|||
tolerance, |
|||
usePercentage |
|||
) && isValueInBounds( |
|||
area.get(SideDirections.TOP), |
|||
area.get(SideDirections.BOTTOM), |
|||
y, |
|||
tolerance, |
|||
usePercentage |
|||
); |
|||
} |
|||
|
|||
/** |
|||
* |
|||
* @param {TwoDimPoint} point |
|||
* @param {Map<SideDirections,number>} area |
|||
* @param {number} tolerance |
|||
* @param {boolean} usePercentage if tolerance is given and this is set to true, |
|||
* the tolerance will be calculated by percentage, |
|||
* otherwise it will be subtracted/added |
|||
*/ |
|||
function isPointInArea(point, area, tolerance = 0, usePercentage = false) { |
|||
return areXYInArea( |
|||
point.x, |
|||
point.y, |
|||
area, |
|||
tolerance, |
|||
usePercentage |
|||
); |
|||
} |
|||
|
|||
/** |
|||
* |
|||
* @param {HTMLElement} element |
|||
* @returns {Object<SideDirections, number} |
|||
*/ |
|||
function getEnclosingBounds(element) { |
|||
let area = element.getBoundingClientRect(); |
|||
let parentArea = element.parentElement.getBoundingClientRect(); |
|||
return { |
|||
[SideDirections.LEFT]: Math.min(area.left, parentArea.left), |
|||
[SideDirections.RIGHT]: Math.max(area.right, parentArea.right), |
|||
[SideDirections.TOP]: Math.min(area.top, parentArea.top), |
|||
[SideDirections.BOTTOM]: Math.max(area.bottom, parentArea.bottom) |
|||
} |
|||
} |
Loading…
Reference in new issue