@lisergia/utilities 23.0.0 → 25.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.
@@ -0,0 +1,5 @@
1
+ declare function trim(canvas: HTMLCanvasElement): HTMLCanvasElement;
2
+ export declare const CanvasUtils: {
3
+ trim: typeof trim;
4
+ };
5
+ export {};
package/dist/Canvas.js ADDED
@@ -0,0 +1,51 @@
1
+ function trim(canvas) {
2
+ const context = canvas.getContext('2d');
3
+ const copy = document.createElement('canvas').getContext('2d', { willReadFrequently: true });
4
+ const pixels = context.getImageData(0, 0, canvas.width, canvas.height);
5
+ const length = pixels.data.length;
6
+ const bound = {
7
+ bottom: null,
8
+ left: null,
9
+ right: null,
10
+ top: null,
11
+ };
12
+ let x;
13
+ let y;
14
+ for (let i = 0; i < length; i += 4) {
15
+ if (pixels.data[i + 3] !== 0) {
16
+ x = (i / 4) % canvas.width;
17
+ y = ~~(i / 4 / canvas.width);
18
+ if (bound.top === null) {
19
+ bound.top = y;
20
+ }
21
+ if (bound.left === null) {
22
+ bound.left = x;
23
+ }
24
+ else if (x < bound.left) {
25
+ bound.left = x;
26
+ }
27
+ if (bound.right === null) {
28
+ bound.right = x;
29
+ }
30
+ else if (bound.right < x) {
31
+ bound.right = x;
32
+ }
33
+ if (bound.bottom === null) {
34
+ bound.bottom = y;
35
+ }
36
+ else if (bound.bottom < y) {
37
+ bound.bottom = y;
38
+ }
39
+ }
40
+ }
41
+ const trimHeight = bound.bottom - bound.top;
42
+ const trimWidth = bound.right - bound.left;
43
+ const trimmed = context.getImageData(bound.left, bound.top, trimWidth, trimHeight);
44
+ copy.canvas.width = trimWidth;
45
+ copy.canvas.height = trimHeight;
46
+ copy.putImageData(trimmed, 0, 0);
47
+ return copy.canvas;
48
+ }
49
+ export const CanvasUtils = {
50
+ trim,
51
+ };
package/dist/DOM.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ export interface DOMRectBounds {
2
+ bottom: number;
3
+ height: number;
4
+ left: number;
5
+ top: number;
6
+ width: number;
7
+ }
8
+ declare function getBounds(element: HTMLElement, top?: number): DOMRectBounds;
9
+ export declare const DOMUtils: {
10
+ getBounds: typeof getBounds;
11
+ };
12
+ export {};
package/dist/DOM.js ADDED
@@ -0,0 +1,13 @@
1
+ function getBounds(element, top = 0) {
2
+ const box = element.getBoundingClientRect();
3
+ return {
4
+ bottom: box.bottom,
5
+ height: box.height,
6
+ left: box.left,
7
+ top: box.top + top,
8
+ width: box.width,
9
+ };
10
+ }
11
+ export const DOMUtils = {
12
+ getBounds,
13
+ };
@@ -0,0 +1,4 @@
1
+ export declare class DetectionManager {
2
+ isMobile(): boolean;
3
+ }
4
+ export declare const Detection: DetectionManager;
@@ -0,0 +1,6 @@
1
+ export class DetectionManager {
2
+ isMobile() {
3
+ return !document.documentElement.classList.contains('desktop');
4
+ }
5
+ }
6
+ export const Detection = new DetectionManager();
package/dist/Math.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ declare function lerp(start: number, end: number, time: number): number;
2
+ declare function clamp(value: number, min: number, max: number): number;
3
+ declare function random(min: number, max: number): number;
4
+ declare function map(value: number, inMin: number, inMax: number, outMin: number, outMax: number, clamp?: boolean): number;
5
+ export declare const MathUtils: {
6
+ clamp: typeof clamp;
7
+ lerp: typeof lerp;
8
+ map: typeof map;
9
+ random: typeof random;
10
+ };
11
+ export {};
package/dist/Math.js ADDED
@@ -0,0 +1,23 @@
1
+ function lerp(start, end, time) {
2
+ return start + (end - start) * time;
3
+ }
4
+ function clamp(value, min, max) {
5
+ return Math.min(Math.max(value, min), max);
6
+ }
7
+ function random(min, max) {
8
+ return Math.random() * (max - min) + min;
9
+ }
10
+ function map(value, inMin, inMax, outMin, outMax, clamp = false) {
11
+ let mapped = ((value - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin;
12
+ if (clamp) {
13
+ const [minOut, maxOut] = outMin < outMax ? [outMin, outMax] : [outMax, outMin];
14
+ mapped = Math.min(Math.max(mapped, minOut), maxOut);
15
+ }
16
+ return mapped;
17
+ }
18
+ export const MathUtils = {
19
+ clamp,
20
+ lerp,
21
+ map,
22
+ random,
23
+ };
@@ -0,0 +1,17 @@
1
+ type HTMLElementForEach = (this: HTMLElement, callback: (value: HTMLElement, key: HTMLElement, parent: HTMLElement) => void, thisArg?: unknown) => void;
2
+ declare interface HTMLElement {
3
+ forEach: HTMLElementForEach;
4
+ }
5
+ declare interface NodeList {
6
+ filter: typeof Array.prototype.filter;
7
+ find: typeof Array.prototype.find;
8
+ map: typeof Array.prototype.map;
9
+ }
10
+ declare const HTMLElementPrototype: {
11
+ forEach: HTMLElementForEach;
12
+ };
13
+ declare const NodeListPrototype: {
14
+ filter: typeof Array.prototype.filter;
15
+ find: typeof Array.prototype.find;
16
+ map: typeof Array.prototype.map;
17
+ };
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ const HTMLElementPrototype = HTMLElement.prototype;
3
+ const NodeListPrototype = NodeList.prototype;
4
+ /**
5
+ * Allow `forEach` to work with single HTMLElement.
6
+ */
7
+ if (!HTMLElementPrototype.forEach) {
8
+ HTMLElementPrototype.forEach = function (callback, thisArg) {
9
+ callback.call(thisArg ?? window, this, this, this);
10
+ };
11
+ }
12
+ /**
13
+ * Allow `filter` to work with NodeList.
14
+ */
15
+ if (!NodeListPrototype.filter) {
16
+ NodeListPrototype.filter = Array.prototype.filter;
17
+ }
18
+ /**
19
+ * Allow `find` to work with NodeList.
20
+ */
21
+ if (!NodeListPrototype.find) {
22
+ NodeListPrototype.find = Array.prototype.find;
23
+ }
24
+ /**
25
+ * Allow `map` to work with NodeList.
26
+ */
27
+ if (!NodeListPrototype.map) {
28
+ NodeListPrototype.map = Array.prototype.map;
29
+ }
@@ -0,0 +1,3 @@
1
+ export declare const createPromise: <T>() => [Promise<T>, (value: T) => void];
2
+ export declare const createPromiseWithReject: <T>() => [Promise<T>, (value: T) => void, (reason?: unknown) => void];
3
+ export declare const createPromiseWithTimeout: <T>(timeout: number) => [Promise<T>, (value: T) => void, (reason?: unknown) => void];
@@ -0,0 +1,31 @@
1
+ export const createPromise = () => {
2
+ let resolve;
3
+ const promise = new Promise((resolvePromise) => {
4
+ resolve = resolvePromise;
5
+ });
6
+ return [promise, resolve];
7
+ };
8
+ export const createPromiseWithReject = () => {
9
+ let resolve;
10
+ let reject;
11
+ const promise = new Promise((resolvePromise, rejectPromise) => {
12
+ resolve = resolvePromise;
13
+ reject = rejectPromise;
14
+ });
15
+ return [promise, resolve, reject];
16
+ };
17
+ export const createPromiseWithTimeout = (timeout) => {
18
+ let resolve;
19
+ let reject;
20
+ const promise = new Promise((resolvePromise, rejectPromise) => {
21
+ resolve = resolvePromise;
22
+ reject = rejectPromise;
23
+ });
24
+ const timer = setTimeout(() => {
25
+ reject(new Error('Promise timed out'));
26
+ }, timeout);
27
+ promise.finally(() => {
28
+ clearTimeout(timer);
29
+ });
30
+ return [promise, resolve, reject];
31
+ };
@@ -0,0 +1,7 @@
1
+ import { type TempusCallback, type TempusOptions } from 'tempus';
2
+ export declare class TempusUtilsManager {
3
+ cache: Map<any, any>;
4
+ add(callback: TempusCallback, options?: TempusOptions): void;
5
+ remove(callback: TempusCallback): void;
6
+ }
7
+ export declare const TempusUtils: TempusUtilsManager;
package/dist/Tempus.js ADDED
@@ -0,0 +1,16 @@
1
+ import Tempus from 'tempus';
2
+ export class TempusUtilsManager {
3
+ cache = new Map();
4
+ add(callback, options) {
5
+ const cancel = Tempus.add(callback, options);
6
+ this.cache.set(callback, cancel);
7
+ }
8
+ remove(callback) {
9
+ const cancel = this.cache.get(callback);
10
+ if (cancel) {
11
+ cancel();
12
+ this.cache.delete(callback);
13
+ }
14
+ }
15
+ }
16
+ export const TempusUtils = new TempusUtilsManager();
package/dist/index.d.ts CHANGED
@@ -1,43 +1,6 @@
1
- import { TempusCallback, TempusOptions } from 'tempus';
2
-
3
- declare function trim(canvas: HTMLCanvasElement): HTMLCanvasElement;
4
- declare const CanvasUtils: {
5
- trim: typeof trim;
6
- };
7
-
8
- declare class DetectionManager {
9
- isMobile(): boolean;
10
- }
11
- declare const Detection: DetectionManager;
12
-
13
- interface DOMRectBounds {
14
- bottom: number;
15
- height: number;
16
- left: number;
17
- top: number;
18
- width: number;
19
- }
20
- declare function getBounds(element: HTMLElement, top?: number): DOMRectBounds;
21
- declare const DOMUtils: {
22
- getBounds: typeof getBounds;
23
- };
24
-
25
- declare function lerp(start: number, end: number, time: number): number;
26
- declare function clamp(value: number, min: number, max: number): number;
27
- declare function random(min: number, max: number): number;
28
- declare function map(value: number, inMin: number, inMax: number, outMin: number, outMax: number, clamp?: boolean): number;
29
- declare const MathUtils: {
30
- clamp: typeof clamp;
31
- lerp: typeof lerp;
32
- map: typeof map;
33
- random: typeof random;
34
- };
35
-
36
- declare class TempusUtilsManager {
37
- cache: Map<any, any>;
38
- add(callback: TempusCallback, options?: TempusOptions): void;
39
- remove(callback: TempusCallback): void;
40
- }
41
- declare const TempusUtils: TempusUtilsManager;
42
-
43
- export { CanvasUtils, type DOMRectBounds, DOMUtils, Detection, DetectionManager, MathUtils, TempusUtils, TempusUtilsManager };
1
+ import './Polyfill.js';
2
+ export * from './Canvas.js';
3
+ export * from './Detection.js';
4
+ export * from './DOM.js';
5
+ export * from './Math.js';
6
+ export * from './Tempus.js';
package/dist/index.js CHANGED
@@ -1,246 +1,6 @@
1
- // src/Polyfill.ts
2
- var HTMLElementPrototype = HTMLElement.prototype;
3
- var NodeListPrototype = NodeList.prototype;
4
- if (!HTMLElementPrototype.forEach) {
5
- HTMLElementPrototype.forEach = function(callback, thisArg) {
6
- thisArg = thisArg || window;
7
- callback.call(thisArg, this, this, this);
8
- };
9
- }
10
- if (!NodeListPrototype.filter) {
11
- NodeListPrototype.filter = Array.prototype.filter;
12
- }
13
- if (!NodeListPrototype.find) {
14
- NodeListPrototype.find = Array.prototype.find;
15
- }
16
- if (!NodeListPrototype.map) {
17
- NodeListPrototype.map = Array.prototype.map;
18
- }
19
-
20
- // src/Canvas.ts
21
- function trim(canvas) {
22
- const context = canvas.getContext("2d");
23
- const copy = document.createElement("canvas").getContext("2d", { willReadFrequently: true });
24
- const pixels = context.getImageData(0, 0, canvas.width, canvas.height);
25
- const length = pixels.data.length;
26
- const bound = {
27
- bottom: null,
28
- left: null,
29
- right: null,
30
- top: null
31
- };
32
- let x;
33
- let y;
34
- for (let i = 0; i < length; i += 4) {
35
- if (pixels.data[i + 3] !== 0) {
36
- x = i / 4 % canvas.width;
37
- y = ~~(i / 4 / canvas.width);
38
- if (bound.top === null) {
39
- bound.top = y;
40
- }
41
- if (bound.left === null) {
42
- bound.left = x;
43
- } else if (x < bound.left) {
44
- bound.left = x;
45
- }
46
- if (bound.right === null) {
47
- bound.right = x;
48
- } else if (bound.right < x) {
49
- bound.right = x;
50
- }
51
- if (bound.bottom === null) {
52
- bound.bottom = y;
53
- } else if (bound.bottom < y) {
54
- bound.bottom = y;
55
- }
56
- }
57
- }
58
- const trimHeight = bound.bottom - bound.top;
59
- const trimWidth = bound.right - bound.left;
60
- const trimmed = context.getImageData(bound.left, bound.top, trimWidth, trimHeight);
61
- copy.canvas.width = trimWidth;
62
- copy.canvas.height = trimHeight;
63
- copy.putImageData(trimmed, 0, 0);
64
- return copy.canvas;
65
- }
66
- var CanvasUtils = {
67
- trim
68
- };
69
-
70
- // src/Detection.ts
71
- var DetectionManager = class {
72
- isMobile() {
73
- return !document.documentElement.classList.contains("desktop");
74
- }
75
- };
76
- var Detection = new DetectionManager();
77
-
78
- // src/DOM.ts
79
- function getBounds(element, top = 0) {
80
- const box = element.getBoundingClientRect();
81
- return {
82
- bottom: box.bottom,
83
- height: box.height,
84
- left: box.left,
85
- top: box.top + top,
86
- width: box.width
87
- };
88
- }
89
- var DOMUtils = {
90
- getBounds
91
- };
92
-
93
- // src/Math.ts
94
- function lerp(start, end, time) {
95
- return start + (end - start) * time;
96
- }
97
- function clamp(value, min, max) {
98
- return Math.min(Math.max(value, min), max);
99
- }
100
- function random(min, max) {
101
- return Math.random() * (max - min) + min;
102
- }
103
- function map(value, inMin, inMax, outMin, outMax, clamp2 = false) {
104
- let mapped = (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
105
- if (clamp2) {
106
- const [minOut, maxOut] = outMin < outMax ? [outMin, outMax] : [outMax, outMin];
107
- mapped = Math.min(Math.max(mapped, minOut), maxOut);
108
- }
109
- return mapped;
110
- }
111
- var MathUtils = {
112
- clamp,
113
- lerp,
114
- map,
115
- random
116
- };
117
-
118
- // ../../node_modules/tempus/dist/tempus.mjs
119
- var index = 0;
120
- function getUID() {
121
- return index++;
122
- }
123
- var isClient = typeof window !== "undefined";
124
- var originalRAF = isClient && window.requestAnimationFrame;
125
- var originalCancelRAF = isClient && window.cancelAnimationFrame;
126
- var Framerate = class {
127
- callbacks;
128
- fps;
129
- time;
130
- lastTickDate;
131
- constructor(fps = Number.POSITIVE_INFINITY) {
132
- this.callbacks = [];
133
- this.fps = fps;
134
- this.time = 0;
135
- this.lastTickDate = performance.now();
136
- }
137
- get executionTime() {
138
- return 1e3 / this.fps;
139
- }
140
- dispatch(time, deltaTime) {
141
- var _a;
142
- for (let i = 0; i < this.callbacks.length; i++) {
143
- (_a = this.callbacks[i]) == null ? void 0 : _a.callback(time, deltaTime);
144
- }
145
- }
146
- raf(time, deltaTime) {
147
- this.time += deltaTime;
148
- if (this.fps === Number.POSITIVE_INFINITY) {
149
- this.dispatch(time, deltaTime);
150
- } else if (this.time >= this.executionTime) {
151
- this.time = this.time % this.executionTime;
152
- const deltaTime2 = time - this.lastTickDate;
153
- this.lastTickDate = time;
154
- this.dispatch(time, deltaTime2);
155
- }
156
- }
157
- add({ callback, priority }) {
158
- if (typeof callback !== "function")
159
- console.error("Tempus.add: callback is not a function");
160
- const uid = getUID();
161
- this.callbacks.push({ callback, priority, uid });
162
- this.callbacks.sort((a, b) => a.priority - b.priority);
163
- return () => this.remove(uid);
164
- }
165
- remove(uid) {
166
- this.callbacks = this.callbacks.filter(({ uid: u }) => uid !== u);
167
- }
168
- };
169
- var TempusImpl = class {
170
- framerates;
171
- time;
172
- constructor() {
173
- this.framerates = {};
174
- this.time = isClient ? performance.now() : 0;
175
- if (!isClient) return;
176
- requestAnimationFrame(this.raf);
177
- }
178
- add(callback, { priority = 0, fps = Number.POSITIVE_INFINITY } = {}) {
179
- if (!isClient) return;
180
- if (typeof fps === "number") {
181
- if (!this.framerates[fps]) this.framerates[fps] = new Framerate(fps);
182
- return this.framerates[fps].add({ callback, priority });
183
- }
184
- }
185
- raf = (time) => {
186
- if (!isClient) return;
187
- requestAnimationFrame(this.raf, true);
188
- const deltaTime = time - this.time;
189
- this.time = time;
190
- for (const framerate of Object.values(this.framerates)) {
191
- framerate.raf(time, deltaTime);
192
- }
193
- };
194
- patch() {
195
- if (!isClient) return;
196
- window.requestAnimationFrame = (callback, { priority = 0, fps = Number.POSITIVE_INFINITY } = {}) => {
197
- if (callback === this.raf || !callback.toString().includes("requestAnimationFrame(")) {
198
- return originalRAF(callback);
199
- }
200
- if (!callback.__tempusPatched) {
201
- callback.__tempusPatched = true;
202
- callback.__tempusUnsubscribe = this.add(callback, { priority, fps });
203
- }
204
- return callback.__tempusUnsubscribe;
205
- };
206
- window.cancelAnimationFrame = (callback) => {
207
- if (typeof callback === "function") {
208
- callback == null ? void 0 : callback();
209
- return;
210
- }
211
- return originalCancelRAF(callback);
212
- };
213
- }
214
- unpatch() {
215
- if (!isClient) return;
216
- window.requestAnimationFrame = originalRAF;
217
- window.cancelAnimationFrame = originalCancelRAF;
218
- }
219
- };
220
- var Tempus = new TempusImpl();
221
-
222
- // src/Tempus.ts
223
- var TempusUtilsManager = class {
224
- cache = /* @__PURE__ */ new Map();
225
- add(callback, options) {
226
- const cancel = Tempus.add(callback, options);
227
- this.cache.set(callback, cancel);
228
- }
229
- remove(callback) {
230
- const cancel = this.cache.get(callback);
231
- if (cancel) {
232
- cancel();
233
- this.cache.delete(callback);
234
- }
235
- }
236
- };
237
- var TempusUtils = new TempusUtilsManager();
238
- export {
239
- CanvasUtils,
240
- DOMUtils,
241
- Detection,
242
- DetectionManager,
243
- MathUtils,
244
- TempusUtils,
245
- TempusUtilsManager
246
- };
1
+ import './Polyfill.js';
2
+ export * from './Canvas.js';
3
+ export * from './Detection.js';
4
+ export * from './DOM.js';
5
+ export * from './Math.js';
6
+ export * from './Tempus.js';
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "@lisergia/utilities",
3
- "version": "23.0.0",
3
+ "version": "25.0.0",
4
+ "files": [
5
+ "dist"
6
+ ],
4
7
  "main": "./dist/index.js",
5
8
  "types": "./dist/index.d.ts",
6
9
  "type": "module",
7
10
  "scripts": {
8
- "build": "tsup src/index.ts --format cjs,esm --dts",
9
- "dev": "tsup src/index.ts --format cjs,esm --dts --watch"
11
+ "build": "tsc --build --clean && tsc --build",
12
+ "dev": "tsc --build --watch"
10
13
  },
11
14
  "dependencies": {
12
- "@lisergia/config-tsconfig": "23.0.0",
13
- "gsap": "^3.13.0",
14
- "tsup": "^8.5.0"
15
+ "@lisergia/config-tsconfig": "25.0.0"
15
16
  }
16
17
  }
@@ -1,20 +0,0 @@
1
-
2
- 
3
- > @lisergia/utilities@22.0.0 build
4
- > tsup src/index.ts --format cjs,esm --dts
5
-
6
- CLI Building entry: src/index.ts
7
- CLI Using tsconfig: tsconfig.json
8
- CLI tsup v8.5.0
9
- CLI Target: node16
10
- CJS Build start
11
- ESM Build start
12
- ESM dist/index.js 6.50 KB
13
- ESM ⚡️ Build success in 134ms
14
- CJS dist/index.cjs 7.71 KB
15
- CJS ⚡️ Build success in 135ms
16
- DTS Build start
17
- DTS ⚡️ Build success in 685ms
18
- DTS dist/index.d.cts 1.32 KB
19
- DTS dist/index.d.ts 1.32 KB
20
- ⠙