@3dsource/utils 1.0.4 → 1.0.5

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 (131) hide show
  1. package/fesm2022/3dsource-utils.mjs +1793 -0
  2. package/fesm2022/3dsource-utils.mjs.map +1 -0
  3. package/index.d.ts +5 -0
  4. package/{src/lib/color/CMYKtoRGB.ts → lib/color/CMYKtoRGB.d.ts} +1 -11
  5. package/lib/color/HEXtoRGB.d.ts +5 -0
  6. package/lib/color/HSVtoRGB.d.ts +21 -0
  7. package/{src/lib/color/RGBtoCMYK.ts → lib/color/RGBtoCMYK.d.ts} +1 -31
  8. package/lib/color/RGBtoHEX.d.ts +1 -0
  9. package/lib/color/RGBtoHSV.d.ts +19 -0
  10. package/lib/color/hsv.d.ts +1 -0
  11. package/lib/color/max.d.ts +1 -0
  12. package/lib/color/min.d.ts +1 -0
  13. package/lib/color/overlay.d.ts +1 -0
  14. package/{src/lib/color/rgb.ts → lib/color/rgb.d.ts} +1 -3
  15. package/lib/color/sub.d.ts +1 -0
  16. package/lib/color/subtract.d.ts +1 -0
  17. package/lib/color/sum.d.ts +1 -0
  18. package/{src/lib/color/toRGB.ts → lib/color/toRGB.d.ts} +1 -7
  19. package/lib/color/toRGBA.d.ts +1 -0
  20. package/lib/constants/color-codes.constant.d.ts +9 -0
  21. package/lib/csv/CSV2Array.d.ts +1 -0
  22. package/lib/csv/CSV2Records.d.ts +1 -0
  23. package/lib/csv/ObjectToCSV.d.ts +1 -0
  24. package/lib/dev/dev3d.d.ts +1 -0
  25. package/lib/dev/logger.d.ts +11 -0
  26. package/lib/dev/timeToString.d.ts +5 -0
  27. package/lib/filenaming/cleanupFileName.d.ts +1 -0
  28. package/lib/filenaming/makePath.d.ts +1 -0
  29. package/lib/filenaming/normalizePath.d.ts +1 -0
  30. package/lib/geom/expandOverRectangle.d.ts +2 -0
  31. package/lib/geom/fitIntoRectangle.d.ts +2 -0
  32. package/{src/lib/geom/interfaces/area.interface.ts → lib/geom/interfaces/area.interface.d.ts} +1 -2
  33. package/{src/lib/geom/interfaces/rect.interface.ts → lib/geom/interfaces/rect.interface.d.ts} +2 -2
  34. package/lib/geom/interfaces/size.interface.d.ts +4 -0
  35. package/lib/geom/interfaces//321/201oords.interface.d.ts +4 -0
  36. package/lib/helpers/BatchLoader.d.ts +47 -0
  37. package/lib/helpers/KeyboardNumericCode.d.ts +103 -0
  38. package/lib/helpers/serialize.d.ts +1 -0
  39. package/lib/helpers/sleep.d.ts +1 -0
  40. package/lib/helpers/trimLastSlashFromUrl.d.ts +1 -0
  41. package/lib/image/SaveImage.d.ts +18 -0
  42. package/lib/image/getCanvasCached.d.ts +4 -0
  43. package/lib/image/getSnapshot.d.ts +2 -0
  44. package/lib/image/loadImage.d.ts +1 -0
  45. package/lib/interfaces/image-output.d.ts +7 -0
  46. package/{src/lib/interfaces/load-args-tmp.interface.ts → lib/interfaces/load-args-tmp.interface.d.ts} +1 -2
  47. package/lib/interfaces/load-args.interface.d.ts +15 -0
  48. package/{src/lib/math/baseSortedIndex.ts → lib/math/baseSortedIndex.d.ts} +1 -19
  49. package/{src/lib/math/calculateMedian.ts → lib/math/calculateMedian.d.ts} +1 -17
  50. package/{src/lib/math/circularIndex.ts → lib/math/circularIndex.d.ts} +1 -5
  51. package/lib/math/clampf.d.ts +8 -0
  52. package/lib/math/degrees.d.ts +2 -0
  53. package/{src/lib/math/floatCompare.ts → lib/math/floatCompare.d.ts} +3 -30
  54. package/{src/lib/math/inverseLerp.ts → lib/math/inverseLerp.d.ts} +1 -22
  55. package/lib/math/lerp.d.ts +7 -0
  56. package/{src/lib/mutex/Mutex.ts → lib/mutex/Mutex.d.ts} +8 -15
  57. package/{src/lib/mutex/Semaphore.ts → lib/mutex/Semaphore.d.ts} +7 -27
  58. package/lib/mutex/TaskRunner.d.ts +5 -0
  59. package/lib/predicates/BooleanPredictors.d.ts +27 -0
  60. package/{src/lib/predicates/textForSearch.ts → lib/predicates/textForSearch.d.ts} +1 -17
  61. package/{src/lib/predicates/where.ts → lib/predicates/where.d.ts} +2 -32
  62. package/lib/rxjs/leadingTrailingDebounceTime.d.ts +15 -0
  63. package/{src/lib/rxjs/smoothTransition.ts → lib/rxjs/smoothTransition.d.ts} +1 -19
  64. package/lib/rxjs/tapLog.d.ts +2 -0
  65. package/lib/strings/pad.d.ts +8 -0
  66. package/package.json +13 -2
  67. package/eslint.config.js +0 -37
  68. package/ng-package.json +0 -7
  69. package/src/lib/color/HEXtoRGB.ts +0 -9
  70. package/src/lib/color/HSVtoRGB.ts +0 -82
  71. package/src/lib/color/RGBtoHEX.ts +0 -17
  72. package/src/lib/color/RGBtoHSV.ts +0 -53
  73. package/src/lib/color/hsv.ts +0 -14
  74. package/src/lib/color/max.ts +0 -18
  75. package/src/lib/color/min.ts +0 -18
  76. package/src/lib/color/overlay.ts +0 -25
  77. package/src/lib/color/sub.ts +0 -18
  78. package/src/lib/color/subtract.ts +0 -27
  79. package/src/lib/color/sum.ts +0 -19
  80. package/src/lib/color/toRGBA.ts +0 -8
  81. package/src/lib/constants/color-codes.constant.ts +0 -9
  82. package/src/lib/csv/CSV2Array.ts +0 -66
  83. package/src/lib/csv/CSV2Records.ts +0 -56
  84. package/src/lib/csv/ObjectToCSV.ts +0 -21
  85. package/src/lib/dev/dev3d.ts +0 -1
  86. package/src/lib/dev/logger.ts +0 -94
  87. package/src/lib/dev/timeToString.ts +0 -16
  88. package/src/lib/filenaming/cleanupFileName.ts +0 -18
  89. package/src/lib/filenaming/makePath.ts +0 -5
  90. package/src/lib/filenaming/normalizePath.ts +0 -9
  91. package/src/lib/geom/expandOverRectangle.ts +0 -17
  92. package/src/lib/geom/fitIntoRectangle.ts +0 -43
  93. package/src/lib/geom/interfaces/size.interface.ts +0 -4
  94. package/src/lib/geom/interfaces//321/201oords.interface.ts +0 -4
  95. package/src/lib/helpers/BatchLoader.ts +0 -243
  96. package/src/lib/helpers/KeyboardNumericCode.ts +0 -118
  97. package/src/lib/helpers/serialize.ts +0 -11
  98. package/src/lib/helpers/sleep.ts +0 -3
  99. package/src/lib/helpers/trimLastSlashFromUrl.ts +0 -9
  100. package/src/lib/image/SaveImage.ts +0 -65
  101. package/src/lib/image/getCanvasCached.ts +0 -16
  102. package/src/lib/image/getSnapshot.ts +0 -99
  103. package/src/lib/image/loadImage.ts +0 -13
  104. package/src/lib/interfaces/image-output.ts +0 -8
  105. package/src/lib/interfaces/load-args.interface.ts +0 -15
  106. package/src/lib/math/clampf.ts +0 -14
  107. package/src/lib/math/degrees.ts +0 -7
  108. package/src/lib/math/lerp.ts +0 -12
  109. package/src/lib/mutex/TaskRunner.ts +0 -26
  110. package/src/lib/predicates/BooleanPredictors.ts +0 -47
  111. package/src/lib/rxjs/leadingTrailingDebounceTime.ts +0 -86
  112. package/src/lib/rxjs/tapLog.ts +0 -13
  113. package/src/lib/strings/pad.ts +0 -18
  114. package/tsconfig.lib.json +0 -13
  115. package/tsconfig.lib.prod.json +0 -11
  116. /package/{src/lib/color/index.ts → lib/color/index.d.ts} +0 -0
  117. /package/{src/lib/constants/index.ts → lib/constants/index.d.ts} +0 -0
  118. /package/{src/lib/csv/index.ts → lib/csv/index.d.ts} +0 -0
  119. /package/{src/lib/dev/index.ts → lib/dev/index.d.ts} +0 -0
  120. /package/{src/lib/filenaming/index.ts → lib/filenaming/index.d.ts} +0 -0
  121. /package/{src/lib/geom/index.ts → lib/geom/index.d.ts} +0 -0
  122. /package/{src/lib/geom/interfaces/index.ts → lib/geom/interfaces/index.d.ts} +0 -0
  123. /package/{src/lib/helpers/index.ts → lib/helpers/index.d.ts} +0 -0
  124. /package/{src/lib/image/index.ts → lib/image/index.d.ts} +0 -0
  125. /package/{src/lib/interfaces/index.ts → lib/interfaces/index.d.ts} +0 -0
  126. /package/{src/lib/math/index.ts → lib/math/index.d.ts} +0 -0
  127. /package/{src/lib/mutex/index.ts → lib/mutex/index.d.ts} +0 -0
  128. /package/{src/lib/predicates/index.ts → lib/predicates/index.d.ts} +0 -0
  129. /package/{src/lib/rxjs/index.ts → lib/rxjs/index.d.ts} +0 -0
  130. /package/{src/lib/strings/index.ts → lib/strings/index.d.ts} +0 -0
  131. /package/{src/public-api.ts → public-api.d.ts} +0 -0
@@ -1,4 +0,0 @@
1
- export interface Coords {
2
- x: number;
3
- y: number;
4
- }
@@ -1,243 +0,0 @@
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
- }
@@ -1,118 +0,0 @@
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 };
@@ -1,11 +0,0 @@
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
- }
@@ -1,3 +0,0 @@
1
- export function sleep(time: number) {
2
- return new Promise((r) => setTimeout(r, time));
3
- }
@@ -1,9 +0,0 @@
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
- }
@@ -1,65 +0,0 @@
1
- import { getSnapshot } from './getSnapshot';
2
- import type { ImageOutputValues } from '../interfaces';
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 };
@@ -1,16 +0,0 @@
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
- }
@@ -1,99 +0,0 @@
1
- import { fitIntoRectangle } from '../geom';
2
- import type { ImageOutputValues } from '../interfaces';
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
- }
@@ -1,13 +0,0 @@
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
- }
@@ -1,8 +0,0 @@
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];
@@ -1,15 +0,0 @@
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
- }
@@ -1,14 +0,0 @@
1
- /**
2
- * Clamps value between min and max;
3
- * @param min minValue
4
- * @param max maxValue
5
- * @param value inputValue
6
- * @returns number
7
- */
8
-
9
- export function clampf(min: number, max: number, value: number): number {
10
- if (min > max) {
11
- return min;
12
- }
13
- return Math.min(max, Math.max(min, value));
14
- }
@@ -1,7 +0,0 @@
1
- export function degreesToRadians(degrees: number): number {
2
- return degrees * (Math.PI / 180);
3
- }
4
-
5
- export function radiansToDegrees(radians: number): number {
6
- return radians * (180 / Math.PI);
7
- }
@@ -1,12 +0,0 @@
1
- import { clampf } from './clampf';
2
-
3
- /**
4
- * @param min minimums
5
- * @param max maximums
6
- * @param value current float value in 0-1 range
7
- * @returns clamped value
8
- */
9
- export function lerp(min: number, max: number, value: number): number {
10
- value = clampf(0, 1, value);
11
- return value * (max - min) + min;
12
- }
@@ -1,26 +0,0 @@
1
- import type { Observable } from 'rxjs';
2
- import { finalize, from, switchMap } from 'rxjs';
3
- import { Logger } from '../dev';
4
- import { Mutex } from './Mutex';
5
-
6
- export class TaskRunner {
7
- private mutex = new Mutex();
8
-
9
- runTask(name: string, task: () => Observable<string | null>) {
10
- // Log that the task is waiting to acquire the mutex
11
- Logger.log(`${name} waiting to acquire mutex`);
12
-
13
- return from(this.mutex.acquire()).pipe(
14
- switchMap((release) => {
15
- // Log that the mutex has been acquired
16
- Logger.log(`${name} acquired mutex`);
17
- return task().pipe(
18
- finalize(() => {
19
- Logger.log(`${name} releasing mutex`);
20
- release();
21
- }),
22
- );
23
- }),
24
- );
25
- }
26
- }