@lisergia/utilities 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "@lisergia/utilities",
3
+ "version": "0.0.0",
4
+ "exports": {
5
+ "./*": "./src/*.ts"
6
+ },
7
+ "main": "./src/index.ts",
8
+ "files": [
9
+ "src"
10
+ ],
11
+ "dependencies": {
12
+ "gsap": "^3.13.0"
13
+ }
14
+ }
package/src/Canvas.ts ADDED
@@ -0,0 +1,65 @@
1
+ function trim(canvas: HTMLCanvasElement) {
2
+ const context = canvas.getContext('2d')!
3
+
4
+ const copy = document.createElement('canvas').getContext('2d', { willReadFrequently: true })
5
+ const pixels = context.getImageData(0, 0, canvas.width, canvas.height)
6
+ const length = pixels.data.length
7
+
8
+ const bound: {
9
+ bottom: number | null
10
+ left: number | null
11
+ right: number | null
12
+ top: number | null
13
+ } = {
14
+ bottom: null,
15
+ left: null,
16
+ right: null,
17
+ top: null,
18
+ }
19
+
20
+ let x: number
21
+ let y: number
22
+
23
+ for (let i = 0; i < length; i += 4) {
24
+ if (pixels.data[i + 3] !== 0) {
25
+ x = (i / 4) % canvas.width
26
+ y = ~~(i / 4 / canvas.width)
27
+
28
+ if (bound.top === null) {
29
+ bound.top = y
30
+ }
31
+
32
+ if (bound.left === null) {
33
+ bound.left = x
34
+ } else if (x < bound.left) {
35
+ bound.left = x
36
+ }
37
+
38
+ if (bound.right === null) {
39
+ bound.right = x
40
+ } else if (bound.right < x) {
41
+ bound.right = x
42
+ }
43
+
44
+ if (bound.bottom === null) {
45
+ bound.bottom = y
46
+ } else if (bound.bottom < y) {
47
+ bound.bottom = y
48
+ }
49
+ }
50
+ }
51
+
52
+ const trimHeight = bound.bottom! - bound.top!
53
+ const trimWidth = bound.right! - bound.left!
54
+ const trimmed = context.getImageData(bound.left!, bound.top!, trimWidth, trimHeight)
55
+
56
+ copy!.canvas.width = trimWidth
57
+ copy!.canvas.height = trimHeight
58
+ copy!.putImageData(trimmed, 0, 0)
59
+
60
+ return copy!.canvas
61
+ }
62
+
63
+ export const CanvasUtils = {
64
+ trim,
65
+ }
package/src/DOM.ts ADDED
@@ -0,0 +1,23 @@
1
+ export interface DOMRectBounds {
2
+ bottom: number
3
+ height: number
4
+ left: number
5
+ top: number
6
+ width: number
7
+ }
8
+
9
+ function getBounds(element: HTMLElement, top = 0) {
10
+ const box = element.getBoundingClientRect()
11
+
12
+ return {
13
+ bottom: box.bottom,
14
+ height: box.height,
15
+ left: box.left,
16
+ top: box.top + top,
17
+ width: box.width,
18
+ } as DOMRectBounds
19
+ }
20
+
21
+ export const DOMUtils = {
22
+ getBounds,
23
+ }
@@ -0,0 +1,7 @@
1
+ export class DetectionManager {
2
+ isMobile() {
3
+ return !document.documentElement.classList.contains('desktop')
4
+ }
5
+ }
6
+
7
+ export const Detection = new DetectionManager()
package/src/Math.ts ADDED
@@ -0,0 +1,37 @@
1
+ function lerp(start: number, end: number, time: number) {
2
+ return start + (end - start) * time
3
+ }
4
+
5
+ function clamp(value: number, min: number, max: number) {
6
+ return Math.min(Math.max(value, min), max)
7
+ }
8
+
9
+ function random(min: number, max: number) {
10
+ return Math.random() * (max - min) + min
11
+ }
12
+
13
+ function map(
14
+ value: number,
15
+ inMin: number,
16
+ inMax: number,
17
+ outMin: number,
18
+ outMax: number,
19
+ clamp: boolean = false,
20
+ ): number {
21
+ let mapped = ((value - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin
22
+
23
+ if (clamp) {
24
+ const [minOut, maxOut] = outMin < outMax ? [outMin, outMax] : [outMax, outMin]
25
+
26
+ mapped = Math.min(Math.max(mapped, minOut), maxOut)
27
+ }
28
+
29
+ return mapped
30
+ }
31
+
32
+ export const MathUtils = {
33
+ clamp,
34
+ lerp,
35
+ map,
36
+ random,
37
+ }
@@ -0,0 +1,51 @@
1
+ declare interface HTMLElement {
2
+ forEach: Function
3
+ }
4
+
5
+ declare interface NodeList {
6
+ filter: Function
7
+ find: Function
8
+ map: Function
9
+ }
10
+
11
+ const HTMLElementPrototype = HTMLElement.prototype as {
12
+ forEach: Function
13
+ }
14
+
15
+ const NodeListPrototype = NodeList.prototype as {
16
+ filter: Function
17
+ find: Function
18
+ map: Function
19
+ }
20
+
21
+ /**
22
+ * Allow `forEach` to work with single HTMLElement.
23
+ */
24
+ if (!HTMLElementPrototype.forEach) {
25
+ HTMLElementPrototype.forEach = function (callback: Function, thisArg: any) {
26
+ thisArg = thisArg || window
27
+
28
+ callback.call(thisArg, this, this, this)
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Allow `filter` to work with NodeList.
34
+ */
35
+ if (!NodeListPrototype.filter) {
36
+ NodeListPrototype.filter = Array.prototype.filter
37
+ }
38
+
39
+ /**
40
+ * Allow `find` to work with NodeList.
41
+ */
42
+ if (!NodeListPrototype.find) {
43
+ NodeListPrototype.find = Array.prototype.find
44
+ }
45
+
46
+ /**
47
+ * Allow `map` to work with NodeList.
48
+ */
49
+ if (!NodeListPrototype.map) {
50
+ NodeListPrototype.map = Array.prototype.map
51
+ }
@@ -0,0 +1,43 @@
1
+ export const createPromise = <T>(): [Promise<T>, (value: T) => void] => {
2
+ let resolve: (value: T) => void
3
+
4
+ const promise = new Promise<T>((resolvePromise) => {
5
+ resolve = resolvePromise
6
+ })
7
+
8
+ return [promise, resolve!]
9
+ }
10
+
11
+ export const createPromiseWithReject = <T>(): [Promise<T>, (value: T) => void, (reason?: any) => void] => {
12
+ let resolve: (value: T) => void
13
+ let reject: (reason?: any) => void
14
+
15
+ const promise = new Promise<T>((resolvePromise, rejectPromise) => {
16
+ resolve = resolvePromise
17
+ reject = rejectPromise
18
+ })
19
+
20
+ return [promise, resolve!, reject!]
21
+ }
22
+
23
+ export const createPromiseWithTimeout = <T>(
24
+ timeout: number,
25
+ ): [Promise<T>, (value: T) => void, (reason?: any) => void] => {
26
+ let resolve: (value: T) => void
27
+ let reject: (reason?: any) => void
28
+
29
+ const promise = new Promise<T>((resolvePromise, rejectPromise) => {
30
+ resolve = resolvePromise
31
+ reject = rejectPromise
32
+ })
33
+
34
+ const timer = setTimeout(() => {
35
+ reject(new Error('Promise timed out'))
36
+ }, timeout)
37
+
38
+ promise.finally(() => {
39
+ clearTimeout(timer)
40
+ })
41
+
42
+ return [promise, resolve!, reject!]
43
+ }
package/src/Text.ts ADDED
@@ -0,0 +1 @@
1
+ export * from 'gsap/src/SplitText'
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ import './Polyfill'
2
+
3
+ export * from './Canvas'
4
+ export * from './Detection'
5
+ export * from './DOM'
6
+ export * from './Math'
7
+ export * from './Text'