@3dsource/utils 0.0.1

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.
Files changed (94) hide show
  1. package/README.md +64 -0
  2. package/eslint.config.js +37 -0
  3. package/ng-package.json +7 -0
  4. package/package.json +11 -0
  5. package/src/lib/color/CMYKtoRGB.ts +20 -0
  6. package/src/lib/color/HEXtoRGB.ts +9 -0
  7. package/src/lib/color/HSVtoRGB.ts +82 -0
  8. package/src/lib/color/RGBtoCMYK.ts +50 -0
  9. package/src/lib/color/RGBtoHEX.ts +17 -0
  10. package/src/lib/color/RGBtoHSV.ts +53 -0
  11. package/src/lib/color/hsv.ts +14 -0
  12. package/src/lib/color/index.ts +16 -0
  13. package/src/lib/color/max.ts +18 -0
  14. package/src/lib/color/min.ts +18 -0
  15. package/src/lib/color/overlay.ts +25 -0
  16. package/src/lib/color/rgb.ts +10 -0
  17. package/src/lib/color/sub.ts +18 -0
  18. package/src/lib/color/subtract.ts +27 -0
  19. package/src/lib/color/sum.ts +19 -0
  20. package/src/lib/color/toRGB.ts +14 -0
  21. package/src/lib/color/toRGBA.ts +8 -0
  22. package/src/lib/constants/color-codes.constant.ts +9 -0
  23. package/src/lib/constants/index.ts +1 -0
  24. package/src/lib/csv/CSV2Array.ts +66 -0
  25. package/src/lib/csv/CSV2Records.ts +56 -0
  26. package/src/lib/csv/ObjectToCSV.ts +21 -0
  27. package/src/lib/csv/index.ts +3 -0
  28. package/src/lib/csv/test/Csv.spec.ts +51 -0
  29. package/src/lib/dev/dev3d.ts +1 -0
  30. package/src/lib/dev/index.ts +3 -0
  31. package/src/lib/dev/logger.ts +94 -0
  32. package/src/lib/dev/timeToString.ts +16 -0
  33. package/src/lib/filenaming/cleanupFileName.ts +18 -0
  34. package/src/lib/filenaming/index.ts +3 -0
  35. package/src/lib/filenaming/makePath.ts +5 -0
  36. package/src/lib/filenaming/normalizePath.ts +9 -0
  37. package/src/lib/filenaming/test/cleanupFileName.spec.ts +9 -0
  38. package/src/lib/filenaming/test/makePath.spec.ts +7 -0
  39. package/src/lib/filenaming/test/normalizePath.spec.ts +9 -0
  40. package/src/lib/geom/expandOverRectangle.ts +17 -0
  41. package/src/lib/geom/fitIntoRectangle.ts +43 -0
  42. package/src/lib/geom/index.ts +3 -0
  43. package/src/lib/geom/interfaces/area.interface.ts +5 -0
  44. package/src/lib/geom/interfaces/index.ts +4 -0
  45. package/src/lib/geom/interfaces/rect.interface.ts +4 -0
  46. package/src/lib/geom/interfaces/size.interface.ts +4 -0
  47. package/src/lib/geom/interfaces//321/201oords.interface.ts +4 -0
  48. package/src/lib/geom/test/fitRectangle.spec.ts +54 -0
  49. package/src/lib/helpers/BatchLoader.ts +243 -0
  50. package/src/lib/helpers/KeyboardNumericCode.ts +118 -0
  51. package/src/lib/helpers/index.ts +6 -0
  52. package/src/lib/helpers/serialize.ts +11 -0
  53. package/src/lib/helpers/sleep.ts +3 -0
  54. package/src/lib/helpers/test/sleep.spec.ts +11 -0
  55. package/src/lib/helpers/trimLastSlashFromUrl.ts +9 -0
  56. package/src/lib/image/SaveImage.ts +65 -0
  57. package/src/lib/image/getCanvasCached.ts +16 -0
  58. package/src/lib/image/getSnapshot.ts +99 -0
  59. package/src/lib/image/index.ts +4 -0
  60. package/src/lib/image/loadImage.ts +13 -0
  61. package/src/lib/interfaces/image-output.ts +8 -0
  62. package/src/lib/interfaces/index.ts +3 -0
  63. package/src/lib/interfaces/load-args-tmp.interface.ts +5 -0
  64. package/src/lib/interfaces/load-args.interface.ts +15 -0
  65. package/src/lib/math/baseSortedIndex.ts +43 -0
  66. package/src/lib/math/calculateMedian.ts +33 -0
  67. package/src/lib/math/circularIndex.ts +39 -0
  68. package/src/lib/math/clampf.ts +14 -0
  69. package/src/lib/math/degrees.ts +7 -0
  70. package/src/lib/math/floatCompare.ts +69 -0
  71. package/src/lib/math/index.ts +8 -0
  72. package/src/lib/math/inverseLerp.ts +38 -0
  73. package/src/lib/math/lerp.ts +12 -0
  74. package/src/lib/math/test/baseSortedIndex.spec.ts +43 -0
  75. package/src/lib/math/test/circularIndex.spec.ts +38 -0
  76. package/src/lib/mutex/Mutex.ts +50 -0
  77. package/src/lib/mutex/Semaphore.ts +62 -0
  78. package/src/lib/mutex/TaskRunner.ts +26 -0
  79. package/src/lib/mutex/index.ts +3 -0
  80. package/src/lib/predicates/BooleanPredictors.ts +47 -0
  81. package/src/lib/predicates/index.ts +3 -0
  82. package/src/lib/predicates/test/BooleanPredictors.spec.ts +71 -0
  83. package/src/lib/predicates/test/where.spec.ts +94 -0
  84. package/src/lib/predicates/textForSearch.ts +34 -0
  85. package/src/lib/predicates/where.ts +76 -0
  86. package/src/lib/rxjs/index.ts +3 -0
  87. package/src/lib/rxjs/leadingTrailingDebounceTime.ts +86 -0
  88. package/src/lib/rxjs/smoothTransition.ts +29 -0
  89. package/src/lib/rxjs/tapLog.ts +13 -0
  90. package/src/lib/strings/index.ts +1 -0
  91. package/src/lib/strings/pad.ts +18 -0
  92. package/src/public-api.ts +14 -0
  93. package/tsconfig.lib.json +13 -0
  94. package/tsconfig.lib.prod.json +11 -0
@@ -0,0 +1,243 @@
1
+ import type { LoadArgs, LoadArgsTmp } from '../interfaces';
2
+ import { loadImage } from '../image';
3
+
4
+ export class BatchLoader {
5
+ private maximumSlotsNumber = 1;
6
+ private queue: LoadArgsTmp[] = [];
7
+ private slots: LoadArgsTmp[] = [];
8
+ private _whenDone!: () => any;
9
+
10
+ clear() {
11
+ this.queue.forEach((item) => {
12
+ try {
13
+ if (item.img) {
14
+ item.img.src = '';
15
+ }
16
+ } catch (e) {
17
+ console.warn(e);
18
+ }
19
+
20
+ try {
21
+ item.xhr?.abort();
22
+ } catch (e) {
23
+ console.warn(e);
24
+ }
25
+ });
26
+
27
+ this.slots.forEach((item) => {
28
+ try {
29
+ if (item.img) {
30
+ item.img.src = '';
31
+ }
32
+ } catch (e) {
33
+ console.warn(e);
34
+ }
35
+
36
+ try {
37
+ item.xhr?.abort();
38
+ } catch (e) {
39
+ console.warn(e);
40
+ }
41
+ });
42
+ this.slots.length = 0;
43
+ this.queue.length = 0;
44
+ }
45
+
46
+ /**
47
+ * new settings
48
+ * you must pass new settings
49
+ * @param sett
50
+ */
51
+ setSettings(sett: any) {
52
+ this.maximumSlotsNumber =
53
+ sett.maximumSlotsNumber !== undefined
54
+ ? sett.maximumSlotsNumber
55
+ : this.maximumSlotsNumber;
56
+ }
57
+
58
+ /**
59
+ * execute with a chosen file
60
+ * @param id
61
+ * @param func
62
+ */
63
+ withItem(id: string, func: (item: LoadArgs) => any): boolean {
64
+ let item = this.getById(id);
65
+ if (!item) {
66
+ return false;
67
+ }
68
+ item = func(item);
69
+ this.queue.sort(this.sortByOrder);
70
+ return true;
71
+ }
72
+
73
+ remove(id: string): LoadArgs[] {
74
+ this.queue = this.queue.filter((arg) => arg.id !== id);
75
+ return this.queue;
76
+ }
77
+
78
+ /**
79
+ * executes when all files are downloaded
80
+ * @param cb
81
+ */
82
+ whenDone(cb: any) {
83
+ this._whenDone = cb;
84
+ }
85
+
86
+ // order {number} lower will execute first, default *100*
87
+ load(args: LoadArgs[]) {
88
+ const out: { path: string; self?: any; id: string }[] = args
89
+ .filter((arg) => !!arg.path)
90
+ .map((arg) => ({
91
+ path: arg.path,
92
+ id: this.add(arg),
93
+ }));
94
+
95
+ this.queue.sort(this.sortByOrder);
96
+ this.checkSlots();
97
+
98
+ return out;
99
+ }
100
+
101
+ /**
102
+ * operates with one file
103
+ * @param item
104
+ * @private
105
+ */
106
+ private add(item: LoadArgs): string {
107
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
108
+ const that = this;
109
+ const temp: LoadArgsTmp = {
110
+ id: item.id,
111
+ resolutionId: null,
112
+ useXhr: item.useXhr,
113
+ order: item.order !== undefined ? item.order : 100,
114
+ fallBack: item.fallBack,
115
+ path: item.path,
116
+ onLoadEnd: item.onLoadEnd,
117
+ onProgress: item.onProgress,
118
+ onLoadStart: item.onLoadStart,
119
+ extra: item.extra,
120
+ };
121
+
122
+ let retryCount = 1;
123
+ if (temp.useXhr) {
124
+ temp.xhr = this.getXmlHttp();
125
+ temp.xhr.onloadstart = (data: ProgressEvent) => item.onLoadStart(data);
126
+ temp.xhr.onprogress = (data: ProgressEvent) => item.onProgress(data);
127
+ temp.xhr.onreadystatechange = async function (data: any) {
128
+ if (this.readyState === 1 && this.status === 0) {
129
+ return;
130
+ }
131
+ if (
132
+ this.readyState === 4 &&
133
+ (this.status === 404 || this.status === 0)
134
+ ) {
135
+ temp.error = true;
136
+ that.loadEnd(temp);
137
+ return;
138
+ }
139
+ if (this.readyState === 4 && this.status === 200) {
140
+ const urlCreator = window.URL;
141
+ const imageUrl = urlCreator.createObjectURL(data.target.response);
142
+ temp.img = await loadImage(imageUrl);
143
+ that.loadEnd(temp);
144
+ return;
145
+ }
146
+ };
147
+ } else {
148
+ temp.img = new Image();
149
+ temp.img.crossOrigin = 'anonymous';
150
+ temp.img.onload = () => {
151
+ that.loadEnd(temp);
152
+ };
153
+ temp.img.onerror = () => {
154
+ if (temp.fallBack && retryCount-- > 0) {
155
+ console.warn('Fallback used =>', temp.path, '=>', temp.fallBack);
156
+ if (temp.img) {
157
+ temp.img.onload = null;
158
+ temp.img.src = temp.fallBack;
159
+ }
160
+
161
+ that.loadEnd(temp);
162
+ } else {
163
+ temp.error = true;
164
+ that.loadEnd(temp);
165
+ }
166
+ };
167
+ }
168
+ this.queue.push(temp);
169
+ return temp.id as string;
170
+ }
171
+
172
+ private sortByOrder(a: LoadArgs, b: LoadArgs): number {
173
+ return a.order > b.order ? 1 : a.order < b.order ? -1 : 0;
174
+ }
175
+
176
+ private loadEnd(item: LoadArgs) {
177
+ if (item.onLoadEnd !== undefined) {
178
+ for (let i = 0, len = this.slots.length; i < len; i += 1) {
179
+ if (this.slots[i].id === item.id) {
180
+ this.slots.splice(i, 1);
181
+ break;
182
+ }
183
+ }
184
+ item.onLoadEnd(item);
185
+ }
186
+ this.checkSlots();
187
+ }
188
+
189
+ private checkSlots() {
190
+ while (
191
+ (this.slots.length < this.maximumSlotsNumber ||
192
+ this.maximumSlotsNumber === -1) &&
193
+ this.queue.length
194
+ ) {
195
+ this.slots.push(this.queue.shift() as LoadArgsTmp);
196
+ const slot: LoadArgsTmp = this.slots[
197
+ this.slots.length - 1
198
+ ] as LoadArgsTmp;
199
+ if (slot.useXhr) {
200
+ slot.xhr?.open('GET', slot.path, true);
201
+ slot.xhr?.send(null);
202
+ } else {
203
+ if (slot.img) {
204
+ slot.img.src = slot.path;
205
+ }
206
+ }
207
+ }
208
+
209
+ if (
210
+ this.slots.length === 0 &&
211
+ this.queue.length === 0 &&
212
+ this._whenDone !== undefined
213
+ ) {
214
+ this._whenDone();
215
+ }
216
+ }
217
+
218
+ /**
219
+ * get Item By id for interaction with files in this.queue
220
+ * @param id
221
+ * @private
222
+ */
223
+ private getById(id: string): LoadArgs | null {
224
+ for (let i = 0, len = this.queue.length; i < len; i += 1) {
225
+ if (this.queue[i].id === id) {
226
+ return this.queue[i];
227
+ }
228
+ }
229
+ return null;
230
+ }
231
+
232
+ private getXmlHttp(): XMLHttpRequest {
233
+ let xhr = null;
234
+ try {
235
+ xhr = new XMLHttpRequest();
236
+ xhr.responseType = 'blob';
237
+ } catch (e) {
238
+ console.warn(e);
239
+ }
240
+
241
+ return xhr as any;
242
+ }
243
+ }
@@ -0,0 +1,118 @@
1
+ const KeyboardNumericCode = {
2
+ Backspace: 8,
3
+ Tab: 9,
4
+ Numpad5: 12,
5
+ NumpadEnter: 13,
6
+ ShiftRight: 16,
7
+ ControlRight: 17,
8
+ ControlLeft: 17,
9
+ AltRight: 18,
10
+ Escape: 27,
11
+ Space: 32,
12
+ Numpad9: 33,
13
+ Numpad3: 34,
14
+ Numpad1: 35,
15
+ Numpad7: 36,
16
+ Numpad4: 37,
17
+ Numpad8: 38,
18
+ Numpad6: 39,
19
+ Numpad2: 40,
20
+ Numpad0: 45,
21
+ NumpadDecimal: 46,
22
+ Digit0: 48,
23
+ Digit1: 49,
24
+ Digit2: 50,
25
+ Digit3: 51,
26
+ Digit4: 52,
27
+ Digit5: 53,
28
+ Digit6: 54,
29
+ Digit7: 55,
30
+ Digit8: 56,
31
+ Digit9: 57,
32
+ KeyA: 65,
33
+ KeyB: 66,
34
+ KeyC: 67,
35
+ KeyD: 68,
36
+ KeyE: 69,
37
+ KeyF: 70,
38
+ KeyG: 71,
39
+ KeyH: 72,
40
+ KeyI: 73,
41
+ KeyJ: 74,
42
+ KeyK: 75,
43
+ KeyL: 76,
44
+ KeyM: 77,
45
+ KeyN: 78,
46
+ KeyO: 79,
47
+ KeyP: 80,
48
+ KeyQ: 81,
49
+ KeyR: 82,
50
+ KeyS: 83,
51
+ KeyT: 84,
52
+ KeyU: 85,
53
+ KeyV: 86,
54
+ KeyW: 87,
55
+ KeyX: 88,
56
+ KeyY: 89,
57
+ KeyZ: 90,
58
+ MetaLeft: 91,
59
+ ContextMenu: 93,
60
+ NumpadMultiply: 106,
61
+ NumpadAdd: 107,
62
+ NumpadSubtract: 109,
63
+ NumpadDivide: 111,
64
+ F1: 112,
65
+ F2: 113,
66
+ F3: 114,
67
+ F4: 115,
68
+ F5: 116,
69
+ F6: 117,
70
+ F7: 118,
71
+ F8: 119,
72
+ F9: 120,
73
+ F10: 121,
74
+ F11: 122,
75
+ F12: 123,
76
+ NumLock: 144,
77
+ ScrollLock: 145,
78
+ Semicolon: 186,
79
+ Equal: 187,
80
+ Comma: 188,
81
+ Minus: 189,
82
+ Period: 190,
83
+ Slash: 191,
84
+ Backquote: 192,
85
+ BracketLeft: 219,
86
+ Backslash: 220,
87
+ BracketRight: 221,
88
+ Quote: 222,
89
+ IntlBackslash: 226,
90
+ ArrowUp: 38,
91
+ ArrowDown: 40,
92
+ ArrowLeft: 37,
93
+ ArrowRight: 39,
94
+ } as const;
95
+
96
+ type IKeyName = keyof typeof KeyboardNumericCode;
97
+ type IKeyCode = (typeof KeyboardNumericCode)[IKeyName];
98
+
99
+ type InvertedKeyMapType = {
100
+ [K in IKeyName as (typeof KeyboardNumericCode)[K]]: K;
101
+ };
102
+ const InvertedKeyMapValues = {} as { [K in IKeyCode]: IKeyName[K] };
103
+
104
+ //For use for InvertedKeyMap[32] will return 'Space' and so on
105
+ const InvertedKeyMap = InvertedKeyMapValues as InvertedKeyMapType;
106
+
107
+ const KeyboardStringCode = {} as { [K in IKeyName]: K };
108
+
109
+ Object.entries(KeyboardNumericCode).forEach(
110
+ // @ts-expect-error @typescript-eslint/ban-ts-comment
111
+ ([key, value]: [IKeyName, IKeyCode]) => {
112
+ InvertedKeyMapValues[value] = key;
113
+ // @ts-expect-error @typescript-eslint/ban-ts-comment
114
+ KeyboardStringCode[key] = key;
115
+ },
116
+ );
117
+ export type { IKeyName, IKeyCode };
118
+ export { KeyboardStringCode, InvertedKeyMap, KeyboardNumericCode };
@@ -0,0 +1,6 @@
1
+ export * from './interfaces';
2
+ export * from './BatchLoader';
3
+ export * from './KeyboardNumericCode';
4
+ export * from './serialize';
5
+ export * from './sleep';
6
+ export * from './trimLastSlashFromUrl';
@@ -0,0 +1,11 @@
1
+ export function serialize(obj: any): string {
2
+ const str = [];
3
+
4
+ for (const p in obj) {
5
+ if (Object.prototype.hasOwnProperty.call(obj, p)) {
6
+ str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
7
+ }
8
+ }
9
+
10
+ return str.join('&');
11
+ }
@@ -0,0 +1,3 @@
1
+ export function sleep(time: number) {
2
+ return new Promise((r) => setTimeout(r, time));
3
+ }
@@ -0,0 +1,11 @@
1
+ import { sleep } from '@shared/utils/helpers/sleep';
2
+
3
+ describe('sleep', () => {
4
+ it('should wait 100 msec', async () => {
5
+ const time = new Date().getTime();
6
+ await sleep(100);
7
+ const diff = (new Date().getTime() - time) / 100;
8
+
9
+ expect(diff).toBeCloseTo(1.01, 1);
10
+ });
11
+ });
@@ -0,0 +1,9 @@
1
+ export function trimLastSlashFromUrl(baseUrl: string) {
2
+ if (!baseUrl) {
3
+ return null;
4
+ } else if (baseUrl[baseUrl.length - 1] === '/') {
5
+ return baseUrl.substring(0, baseUrl.length - 1);
6
+ }
7
+
8
+ return;
9
+ }
@@ -0,0 +1,65 @@
1
+ import { getSnapshot } from './getSnapshot';
2
+ import type { ImageOutputValues } from '../helpers';
3
+
4
+ /**
5
+ * USE DownloadImage instead!
6
+ * @deprecated use DownloadImage instead!
7
+ * @param imageSource
8
+ * @param name
9
+ * @param width
10
+ * @param format
11
+ */
12
+ function SaveImage(
13
+ imageSource:
14
+ | HTMLImageElement
15
+ | HTMLCanvasElement
16
+ | HTMLVideoElement
17
+ | ImageBitmap
18
+ | string,
19
+ name: string,
20
+ width?: number,
21
+ format?: string | ImageOutputValues,
22
+ ): any {
23
+ let out;
24
+ const image: any = imageSource;
25
+
26
+ if (typeof image !== 'string') {
27
+ out = getSnapshot(image, width, format as ImageOutputValues);
28
+ } else {
29
+ out = image;
30
+ }
31
+
32
+ if ((navigator as any).msSaveBlob) {
33
+ // IE10+
34
+ try {
35
+ const blob = image.msToBlob();
36
+ return (navigator as any).msSaveBlob(blob, name);
37
+ } catch (e) {
38
+ console.warn(e);
39
+ }
40
+ } else {
41
+ const uri = out;
42
+ const link = document.createElement('a');
43
+ link.download = name;
44
+ link.href = uri as string;
45
+
46
+ document.body.appendChild(link);
47
+ if (link.click) {
48
+ link.click();
49
+ } else {
50
+ const event = document.createEvent('MouseEvents');
51
+ (event as any).initMouseEvent('click', true, true, window);
52
+ link.dispatchEvent(event);
53
+ }
54
+ document.body.removeChild(link);
55
+ }
56
+ }
57
+
58
+ /**
59
+ * @param imageSource
60
+ * @param string name
61
+ * @param number width
62
+ * @param string format
63
+ */
64
+ const DownloadImage = SaveImage;
65
+ export { SaveImage, DownloadImage };
@@ -0,0 +1,16 @@
1
+ const canvasCacheObj: any = {};
2
+
3
+ export function getCanvasCached(id: string): {
4
+ canvas: HTMLCanvasElement;
5
+ ctx: CanvasRenderingContext2D;
6
+ } {
7
+ if (!canvasCacheObj[id]) {
8
+ const canvas = document.createElement('canvas');
9
+ canvasCacheObj[id] = {
10
+ canvas,
11
+ ctx: canvas.getContext('2d'),
12
+ };
13
+ }
14
+
15
+ return canvasCacheObj[id];
16
+ }
@@ -0,0 +1,99 @@
1
+ import { fitIntoRectangle } from '../geom';
2
+ import type { ImageOutputValues } from '../helpers';
3
+
4
+ function getSnapshot2(
5
+ imageElement:
6
+ | HTMLImageElement
7
+ | HTMLCanvasElement
8
+ | HTMLVideoElement
9
+ | ImageBitmap,
10
+ maxWidth = 1024,
11
+ output: ImageOutputValues = 'image/png',
12
+ quality = 0.9,
13
+ ): HTMLCanvasElement | string {
14
+ const drawableImage = imageElement as any;
15
+ const canvasHelper: HTMLCanvasElement = document.createElement('canvas');
16
+ const drawCTX: CanvasRenderingContext2D = canvasHelper.getContext(
17
+ '2d',
18
+ ) as CanvasRenderingContext2D;
19
+
20
+ const prop = fitIntoRectangle(
21
+ {
22
+ x: 0,
23
+ y: 0,
24
+ w: drawableImage.naturalWidth || drawableImage.width,
25
+ h: drawableImage.naturalHeight || drawableImage.height,
26
+ },
27
+ {
28
+ x: 0,
29
+ y: 0,
30
+ w: maxWidth,
31
+ h: drawableImage.naturalHeight || drawableImage.height,
32
+ },
33
+ );
34
+
35
+ const width = prop.w;
36
+ const height = prop.h;
37
+ canvasHelper.width = width;
38
+ canvasHelper.height = height;
39
+
40
+ drawCTX.save();
41
+ drawCTX.clearRect(0, 0, width, height);
42
+ drawCTX.drawImage(drawableImage, 0, 0, width, height);
43
+ drawCTX.restore();
44
+
45
+ if (output === 'canvas') {
46
+ return canvasHelper;
47
+ }
48
+
49
+ return canvasHelper.toDataURL(output, quality);
50
+ }
51
+
52
+ export function getSnapshot(
53
+ imageElement:
54
+ | HTMLImageElement
55
+ | HTMLCanvasElement
56
+ | HTMLVideoElement
57
+ | ImageBitmap,
58
+ maxWidth = 1024,
59
+ output: ImageOutputValues = 'image/png',
60
+ quality = 0.9,
61
+ ): HTMLCanvasElement | string {
62
+ const drawableImage = imageElement as any;
63
+ const canvasHelper: HTMLCanvasElement = document.createElement('canvas');
64
+ const drawCTX: CanvasRenderingContext2D = canvasHelper.getContext(
65
+ '2d',
66
+ ) as CanvasRenderingContext2D;
67
+
68
+ const prop = fitIntoRectangle(
69
+ {
70
+ x: 0,
71
+ y: 0,
72
+ w: drawableImage.naturalWidth || drawableImage.width,
73
+ h: drawableImage.naturalHeight || drawableImage.height,
74
+ },
75
+ {
76
+ x: 0,
77
+ y: 0,
78
+ w: (drawableImage.naturalWidth || drawableImage.width) / 2,
79
+ h: drawableImage.naturalHeight || drawableImage.height,
80
+ },
81
+ );
82
+
83
+ const width = prop.w;
84
+ const height = prop.h;
85
+ canvasHelper.width = width;
86
+ canvasHelper.height = height;
87
+
88
+ drawCTX.save();
89
+ drawCTX.clearRect(0, 0, width, height);
90
+ drawCTX.drawImage(drawableImage, 0, 0, width, height);
91
+
92
+ drawCTX.restore();
93
+
94
+ if (width > maxWidth * 2) {
95
+ return getSnapshot(canvasHelper, maxWidth, output, quality);
96
+ }
97
+
98
+ return getSnapshot2(drawableImage, maxWidth, output, quality);
99
+ }
@@ -0,0 +1,4 @@
1
+ export * from './getCanvasCached';
2
+ export * from './getSnapshot';
3
+ export * from './loadImage';
4
+ export * from './SaveImage';
@@ -0,0 +1,13 @@
1
+ export function loadImage(url: string): Promise<HTMLImageElement> {
2
+ return new Promise((resolve: any, reject) => {
3
+ const img = new Image();
4
+ img.crossOrigin = 'anonymous';
5
+ img.onload = () => {
6
+ setTimeout(() => resolve(img), 1);
7
+ };
8
+ img.onerror = () => {
9
+ setTimeout(() => reject(null), 1);
10
+ };
11
+ img.src = url;
12
+ });
13
+ }
@@ -0,0 +1,8 @@
1
+ export const ImageOutput = {
2
+ CANVAS: 'canvas',
3
+ PNG: 'image/png',
4
+ WEBP: 'image/webp',
5
+ JPG: 'image/jpeg',
6
+ } as const;
7
+
8
+ export type ImageOutputValues = (typeof ImageOutput)[keyof typeof ImageOutput];
@@ -0,0 +1,3 @@
1
+ export * from './load-args.interface';
2
+ export * from './load-args-tmp.interface';
3
+ export * from './image-output';
@@ -0,0 +1,5 @@
1
+ import type { LoadArgs } from './load-args.interface';
2
+
3
+ export interface LoadArgsTmp extends LoadArgs {
4
+ xhr?: XMLHttpRequest;
5
+ }
@@ -0,0 +1,15 @@
1
+ export interface LoadArgs {
2
+ order: number;
3
+ extra: any;
4
+ item?: HTMLImageElement;
5
+ useXhr?: boolean;
6
+ path: string;
7
+ onLoadEnd: (data: LoadArgs) => void;
8
+ onLoadStart: (data: ProgressEvent) => void;
9
+ onProgress: (data: ProgressEvent) => void;
10
+ id?: string;
11
+ img?: HTMLImageElement;
12
+ error?: boolean;
13
+ fallBack?: string;
14
+ resolutionId: number | string | null;
15
+ }
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Calculates the index at which the `value` should be inserted into the `array` to maintain its
3
+ * sorted order. This function performs a binary search with a complexity of O(log n), making it
4
+ * efficient for large datasets. It assumes that the input array is sorted in ascending order and
5
+ * contains comparable elements.
6
+ *
7
+ * Note: The function is designed to handle arrays with lengths up to a maximum of `HALF_MAX_ARRAY_LENGTH`
8
+ * to ensure the search operation remains within JavaScript's maximum array index limit.
9
+ *
10
+ * @param {any[]} array The sorted array into which the value should be inserted. The array elements
11
+ * should be comparable with the `value` using less-than and greater-than operations.
12
+ * @param {number} value The value to insert into the array. This function assumes that `array` is sorted
13
+ * in ascending order and will find the correct position for `value` accordingly.
14
+ * @returns {number} The index at which the `value` should be inserted into `array` to maintain its sorted order.
15
+ * If the `array` is empty or if `value` is less than or equal to all elements in `array`,
16
+ * returns 0. If `value` is greater than all elements in `array`, returns the length of `array`.
17
+ * @example
18
+ * // returns 2
19
+ * baseSortedIndex([1, 3, 4], 3);
20
+ *
21
+ * @example
22
+ * // returns 1
23
+ * baseSortedIndex([1, 2, 3], 2);
24
+ */
25
+ export function baseSortedIndex(array: number[], value: number): number {
26
+ const MAX_ARRAY_LENGTH = 4294967295;
27
+ const HALF_MAX_ARRAY_LENGTH = MAX_ARRAY_LENGTH >>> 1;
28
+ let low = 0;
29
+ let high = array?.length || low;
30
+
31
+ if (high <= HALF_MAX_ARRAY_LENGTH) {
32
+ while (low < high) {
33
+ const mid = (low + high) >>> 1;
34
+ const computed = array[mid];
35
+ if (computed !== null && computed < value) {
36
+ low = mid + 1;
37
+ } else {
38
+ high = mid;
39
+ }
40
+ }
41
+ }
42
+ return high;
43
+ }