tiny-essentials
Version:
Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.
128 lines (81 loc) ⢠3.65 kB
Markdown
### š `areHtmlElsColliding()`
Check if two DOM elements are **colliding on the screen**! Perfect for games, draggable elements, UI interactions, and more.
This function checks whether **two HTML elements are overlapping** (colliding) within the viewport. It uses their bounding boxes (`getBoundingClientRect()`) to perform a simple AABB (Axis-Aligned Bounding Box) collision detection.
Combine it with `mousemove`, `drag`, or animation listeners for dynamic collision detection in real-time!
It compares the bounding rectangles of both elements:
* ā
Checks if **rect1** is NOT entirely to the left, right, above, or below **rect2**.
* ā
If none of these are true, then the elements are overlapping.
#### š§ Syntax
```javascript
areHtmlElsColliding(elem1, elem2);
```
#### šÆ Parameters
| Parameter | Type | Description |
| --------- | --------- | ----------------------- |
| `elem1` | `Element` | The first DOM element. |
| `elem2` | `Element` | The second DOM element. |
#### š Return
| Type | Description |
| --------- | ------------------------------------------------------------------ |
| `boolean` | ā
`true` if elements are colliding. <br>ā `false` if they are not. |
#### š¦ Example
```javascript
const box1 = document.getElementById('box1');
const box2 = document.getElementById('box2');
if (areHtmlElsColliding(box1, box2)) {
console.log('šÆ Collision detected!');
} else {
console.log('ā No collision.');
}
```
#### š§ Limitations
* Only works with **axis-aligned elements** (rectangular shapes).
* Does not handle rotated elements or complex shapes.
---
### š² `getHtmlElBordersWidth(el)`
š Returns the total **border width** of an element using `border{Side}Width` values from computed styles.
```js
getHtmlElBordersWidth(el: Element): HtmlElBoxSides
```
* `el`: The target DOM element.
* **Returns**: An object containing total horizontal/vertical border widths, and each side individually.
---
### š³ `getHtmlElBorders(el)`
š Returns the total **border size** of an element using `border{Side}` shorthand values from computed styles.
```js
getHtmlElBorders(el: Element): HtmlElBoxSides
```
* `el`: The target DOM element.
* **Returns**: An object with total horizontal/vertical border sizes and all four sides.
---
### ā `getHtmlElMargin(el)`
š Returns the total **margin** of an element using `margin{Side}` from computed styles.
```js
getHtmlElMargin(el: Element): HtmlElBoxSides
```
* `el`: The target DOM element.
* **Returns**: An object containing margin values for each side and totals for horizontal (`x`) and vertical (`y`).
---
### ā `getHtmlElPadding(el)`
š§© Returns the total **padding** of an element using `padding{Side}` from computed styles.
```js
getHtmlElPadding(el: Element): HtmlElBoxSides
```
* `el`: The target DOM element.
* **Returns**: Padding values for all sides and summed horizontal (`x`) and vertical (`y`) values.
---
### šļø `isInViewport(element)`
š Checks if an element is **partially visible** in the current viewport.
```js
isInViewport(element: HTMLElement): boolean
```
* `element`: The DOM element to check.
* **Returns**: `true` if the element is at least partially visible; `false` otherwise.
---
### ā
`isScrolledIntoView(element)`
š¦ Checks if an element is **fully visible** (top and bottom) within the viewport.
```js
isScrolledIntoView(element: HTMLElement): boolean
```
* `element`: The DOM element to check.
* **Returns**: `true` if the entire element is within the viewport; `false` otherwise.