UNPKG

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
### šŸš€ `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.