@combeenation/3d-viewer 5.0.1 → 5.0.3-beta2

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 (109) hide show
  1. package/README.md +111 -114
  2. package/dist/lib-cjs/api/classes/animationInterface.d.ts +8 -8
  3. package/dist/lib-cjs/api/classes/animationInterface.js +1 -1
  4. package/dist/lib-cjs/api/classes/dottedPath.d.ts +79 -79
  5. package/dist/lib-cjs/api/classes/dottedPath.js +187 -187
  6. package/dist/lib-cjs/api/classes/element.d.ts +139 -139
  7. package/dist/lib-cjs/api/classes/element.js +794 -794
  8. package/dist/lib-cjs/api/classes/element.js.map +1 -1
  9. package/dist/lib-cjs/api/classes/elementParameterizable.d.ts +14 -0
  10. package/dist/lib-cjs/api/classes/elementParameterizable.js +135 -0
  11. package/dist/lib-cjs/api/classes/elementParameterizable.js.map +1 -0
  12. package/dist/lib-cjs/api/classes/event.d.ts +326 -326
  13. package/dist/lib-cjs/api/classes/event.js +371 -371
  14. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  15. package/dist/lib-cjs/api/classes/eventBroadcaster.js +53 -53
  16. package/dist/lib-cjs/api/classes/parameter.d.ts +316 -259
  17. package/dist/lib-cjs/api/classes/parameter.js +451 -388
  18. package/dist/lib-cjs/api/classes/parameter.js.map +1 -1
  19. package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
  20. package/dist/lib-cjs/api/classes/parameterObservable.js +101 -101
  21. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  22. package/dist/lib-cjs/api/classes/parameterizable.js +149 -149
  23. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +44 -44
  24. package/dist/lib-cjs/api/classes/placementAnimation.js +163 -163
  25. package/dist/lib-cjs/api/classes/variant.d.ts +234 -234
  26. package/dist/lib-cjs/api/classes/variant.js +1154 -1154
  27. package/dist/lib-cjs/api/classes/variantInstance.d.ts +45 -45
  28. package/dist/lib-cjs/api/classes/variantInstance.js +108 -108
  29. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  30. package/dist/lib-cjs/api/classes/variantParameterizable.js +99 -99
  31. package/dist/lib-cjs/api/classes/viewer.d.ts +177 -175
  32. package/dist/lib-cjs/api/classes/viewer.js +717 -701
  33. package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
  34. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
  35. package/dist/lib-cjs/api/classes/viewerLight.js +392 -392
  36. package/dist/lib-cjs/api/internal/debugViewer.d.ts +13 -13
  37. package/dist/lib-cjs/api/internal/debugViewer.js +86 -86
  38. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  39. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  40. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +13 -10
  41. package/dist/lib-cjs/api/internal/sceneSetup.js +238 -234
  42. package/dist/lib-cjs/api/internal/sceneSetup.js.map +1 -1
  43. package/dist/lib-cjs/api/manager/animationManager.d.ts +29 -29
  44. package/dist/lib-cjs/api/manager/animationManager.js +130 -130
  45. package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +65 -65
  46. package/dist/lib-cjs/api/manager/gltfExportManager.js +223 -222
  47. package/dist/lib-cjs/api/manager/gltfExportManager.js.map +1 -1
  48. package/dist/lib-cjs/api/manager/sceneManager.d.ts +31 -32
  49. package/dist/lib-cjs/api/manager/sceneManager.js +153 -132
  50. package/dist/lib-cjs/api/manager/sceneManager.js.map +1 -1
  51. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +92 -92
  52. package/dist/lib-cjs/api/manager/variantInstanceManager.js +335 -335
  53. package/dist/lib-cjs/api/store/specStorage.d.ts +24 -24
  54. package/dist/lib-cjs/api/store/specStorage.js +51 -51
  55. package/dist/lib-cjs/api/util/babylonHelper.d.ts +174 -166
  56. package/dist/lib-cjs/api/util/babylonHelper.js +575 -497
  57. package/dist/lib-cjs/api/util/babylonHelper.js.map +1 -1
  58. package/dist/lib-cjs/api/util/globalTypes.d.ts +366 -356
  59. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  60. package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -53
  61. package/dist/lib-cjs/api/util/resourceHelper.js +257 -242
  62. package/dist/lib-cjs/api/util/resourceHelper.js.map +1 -1
  63. package/dist/lib-cjs/api/util/stringHelper.d.ts +9 -9
  64. package/dist/lib-cjs/api/util/stringHelper.js +25 -25
  65. package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
  66. package/dist/lib-cjs/api/util/structureHelper.js +48 -44
  67. package/dist/lib-cjs/api/util/structureHelper.js.map +1 -1
  68. package/dist/lib-cjs/buildinfo.json +3 -3
  69. package/dist/lib-cjs/index.d.ts +49 -49
  70. package/dist/lib-cjs/index.js +89 -89
  71. package/dist/webpack-stats.json +0 -0
  72. package/package.json +87 -87
  73. package/src/api/classes/animationInterface.ts +10 -10
  74. package/src/api/classes/dottedPath.ts +181 -181
  75. package/src/api/classes/element.ts +690 -692
  76. package/src/api/classes/event.ts +367 -367
  77. package/src/api/classes/eventBroadcaster.ts +52 -52
  78. package/src/api/classes/parameter.ts +474 -405
  79. package/src/api/classes/parameterObservable.ts +100 -100
  80. package/src/api/classes/parameterizable.ts +87 -87
  81. package/src/api/classes/placementAnimation.ts +160 -160
  82. package/src/api/classes/variant.ts +845 -845
  83. package/src/api/classes/variantInstance.ts +97 -97
  84. package/src/api/classes/variantParameterizable.ts +85 -85
  85. package/src/api/classes/viewer.ts +650 -624
  86. package/src/api/classes/viewerLight.ts +334 -334
  87. package/src/api/internal/debugViewer.ts +90 -90
  88. package/src/api/internal/lensRendering.ts +10 -10
  89. package/src/api/internal/sceneSetup.ts +204 -201
  90. package/src/api/manager/animationManager.ts +142 -142
  91. package/src/api/manager/gltfExportManager.ts +191 -191
  92. package/src/api/manager/sceneManager.ts +128 -102
  93. package/src/api/manager/variantInstanceManager.ts +265 -265
  94. package/src/api/store/specStorage.ts +51 -51
  95. package/src/api/util/babylonHelper.ts +628 -538
  96. package/src/api/util/globalTypes.ts +413 -402
  97. package/src/api/util/resourceHelper.ts +189 -173
  98. package/src/api/util/stringHelper.ts +23 -23
  99. package/src/api/util/structureHelper.ts +49 -43
  100. package/src/buildinfo.json +3 -3
  101. package/src/commonjs.tsconfig.json +10 -10
  102. package/src/declaration.tsconfig.json +8 -8
  103. package/src/dev.ts +42 -42
  104. package/src/es6.tsconfig.json +10 -10
  105. package/src/index.ts +94 -94
  106. package/src/pagesconfig.json +77 -73
  107. package/src/tsconfig.json +32 -32
  108. package/src/tsconfig.types.json +9 -9
  109. package/src/types.d.ts +3 -3
@@ -1,2 +1,2 @@
1
- "use strict";
1
+ "use strict";
2
2
  //# sourceMappingURL=globalTypes.js.map
@@ -1,53 +1,58 @@
1
- /**
2
- * Sleeps for a certain amount of microseconds.
3
- */
4
- declare const sleep: (ms: number) => Promise<unknown>;
5
- /**
6
- * Loads a JSON from a given path.
7
- * @emits {@link Event.LOADING_START}
8
- * @emits {@link Event.LOADING_END}
9
- */
10
- declare const loadJson: <T>(path: string) => Promise<T>;
11
- /**
12
- * Loads a Text from a given path.
13
- * @emits {@link Event.LOADING_START}
14
- * @emits {@link Event.LOADING_END}
15
- */
16
- declare const loadText: (path: string) => Promise<string>;
17
- /**
18
- * Debounces: it limits the rate at which a function can fire.
19
- */
20
- declare const debounce: (func: Function, wait: number, immediate?: boolean) => () => void;
21
- /**
22
- * Merges multiple maps.
23
- */
24
- declare const mergeMaps: <TKey, TValue>(...maps: Map<TKey, TValue>[]) => Map<TKey, TValue>;
25
- /**
26
- * Creates a HTML image element based on a SVG string, whereas all the embedded assets in the SVG (eg: fonts, images)
27
- * are already loaded and exchanged by their base64 representation.\
28
- * There the output image can exist as "standalone" image and may be used for example as a paintable.
29
- *
30
- * !!CAUTION!!: The used functions within this code section are very well evaluated since most alternatives
31
- * somehow don't work in Safari, as mentioned in the following BJS forum entries:
32
- * - https://forum.babylonjs.com/t/drawing-svg-content-text-into-dynamictexture-doesnt-work-in-safari-v15/25048
33
- * - https://forum.babylonjs.com/t/texture-createfrombase64string-doesnt-seem-to-work-for-ios-devices-initially/25502
34
- */
35
- declare const createImageFromSvg: (svgSrc: string) => Promise<HTMLImageElement>;
36
- /**
37
- * Creates an HTML image element from a dedicated image source.\
38
- * Also waits until the image has loaded.
39
- *
40
- * !!CAUTION!!: The `setTimeout` after loading is finished is required due to a Safari bug:
41
- * - https://bugs.webkit.org/show_bug.cgi?id=39059
42
- * - https://bugs.webkit.org/show_bug.cgi?id=219770
43
- *
44
- * It's not 100% ensured that the timeout solves the issue in every case, but there is no other way unfortunately.\
45
- * => Keep an eye on it in future projects
46
- *
47
- * @param imgSrc Theoretically every source is valid which is also supported by
48
- * [HTMLImageElement.src](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/src).\
49
- * Known exceptions are SVGs with embedded assets that are provided as object URL. See comments in
50
- * {@link createImageFromSvg} for further details.
51
- */
52
- declare const createImageFromImgSrc: (imgSrc: string) => Promise<HTMLImageElement>;
53
- export { sleep, loadJson, loadText, debounce, mergeMaps, createImageFromSvg, createImageFromImgSrc };
1
+ /**
2
+ * Sleeps for a certain amount of microseconds.
3
+ */
4
+ declare const sleep: (ms: number) => Promise<unknown>;
5
+ /**
6
+ * Loads a JSON from a given path.
7
+ * @emits {@link Event.LOADING_START}
8
+ * @emits {@link Event.LOADING_END}
9
+ */
10
+ declare const loadJson: <T>(path: string) => Promise<T>;
11
+ /**
12
+ * Loads a Text from a given path.
13
+ * @emits {@link Event.LOADING_START}
14
+ * @emits {@link Event.LOADING_END}
15
+ */
16
+ declare const loadText: (path: string) => Promise<string>;
17
+ /**
18
+ * Loads a Javascript ressource.\
19
+ * Usefull for including packages via CDN (eg: BabylonJS Inspector)
20
+ */
21
+ declare const loadJavascript: (url: string) => Promise<void>;
22
+ /**
23
+ * Debounces: it limits the rate at which a function can fire.
24
+ */
25
+ declare const debounce: (func: Function, wait: number, immediate?: boolean) => () => void;
26
+ /**
27
+ * Merges multiple maps.
28
+ */
29
+ declare const mergeMaps: <TKey, TValue>(...maps: Map<TKey, TValue>[]) => Map<TKey, TValue>;
30
+ /**
31
+ * Creates a HTML image element based on a SVG string, whereas all the embedded assets in the SVG (eg: fonts, images)
32
+ * are already loaded and exchanged by their base64 representation.\
33
+ * There the output image can exist as "standalone" image and may be used for example as a paintable.
34
+ *
35
+ * !!CAUTION!!: The used functions within this code section are very well evaluated since most alternatives
36
+ * somehow don't work in Safari, as mentioned in the following BJS forum entries:
37
+ * - https://forum.babylonjs.com/t/drawing-svg-content-text-into-dynamictexture-doesnt-work-in-safari-v15/25048
38
+ * - https://forum.babylonjs.com/t/texture-createfrombase64string-doesnt-seem-to-work-for-ios-devices-initially/25502
39
+ */
40
+ declare const createImageFromSvg: (svgSrc: string) => Promise<HTMLImageElement>;
41
+ /**
42
+ * Creates an HTML image element from a dedicated image source.\
43
+ * Also waits until the image has loaded.
44
+ *
45
+ * !!CAUTION!!: The `setTimeout` after loading is finished is required due to a Safari bug:
46
+ * - https://bugs.webkit.org/show_bug.cgi?id=39059
47
+ * - https://bugs.webkit.org/show_bug.cgi?id=219770
48
+ *
49
+ * It's not 100% ensured that the timeout solves the issue in every case, but there is no other way unfortunately.\
50
+ * => Keep an eye on it in future projects
51
+ *
52
+ * @param imgSrc Theoretically every source is valid which is also supported by
53
+ * [HTMLImageElement.src](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/src).\
54
+ * Known exceptions are SVGs with embedded assets that are provided as object URL. See comments in
55
+ * {@link createImageFromSvg} for further details.
56
+ */
57
+ declare const createImageFromImgSrc: (imgSrc: string) => Promise<HTMLImageElement>;
58
+ export { sleep, loadJson, loadText, loadJavascript, debounce, mergeMaps, createImageFromSvg, createImageFromImgSrc };
@@ -1,243 +1,258 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createImageFromImgSrc = exports.createImageFromSvg = exports.mergeMaps = exports.debounce = exports.loadText = exports.loadJson = exports.sleep = void 0;
4
- var tslib_1 = require("tslib");
5
- var event_1 = require("../classes/event");
6
- /**
7
- * Sleeps for a certain amount of microseconds.
8
- */
9
- var sleep = function (ms) { return new Promise(function (resolve) { return window.setTimeout(resolve, ms); }); };
10
- exports.sleep = sleep;
11
- /**
12
- * Loads any kind of response from given path.
13
- * @emits {@link Event.LOADING_START}
14
- * @emits {@link Event.LOADING_END}
15
- */
16
- var load = function (path) {
17
- return tslib_1.__awaiter(this, void 0, void 0, function () {
18
- var data;
19
- return tslib_1.__generator(this, function (_a) {
20
- switch (_a.label) {
21
- case 0:
22
- event_1.emitter.emit(event_1.Event.LOADING_START, path);
23
- return [4 /*yield*/, fetch(path)];
24
- case 1:
25
- data = _a.sent();
26
- event_1.emitter.emit(event_1.Event.LOADING_END, path);
27
- return [2 /*return*/, data];
28
- }
29
- });
30
- });
31
- };
32
- /**
33
- * Loads a JSON from a given path.
34
- * @emits {@link Event.LOADING_START}
35
- * @emits {@link Event.LOADING_END}
36
- */
37
- var loadJson = function (path) {
38
- return tslib_1.__awaiter(this, void 0, void 0, function () {
39
- var response;
40
- return tslib_1.__generator(this, function (_a) {
41
- switch (_a.label) {
42
- case 0: return [4 /*yield*/, load(path)];
43
- case 1:
44
- response = _a.sent();
45
- return [2 /*return*/, response.json()];
46
- }
47
- });
48
- });
49
- };
50
- exports.loadJson = loadJson;
51
- /**
52
- * Loads a Text from a given path.
53
- * @emits {@link Event.LOADING_START}
54
- * @emits {@link Event.LOADING_END}
55
- */
56
- var loadText = function (path) {
57
- return tslib_1.__awaiter(this, void 0, void 0, function () {
58
- var response;
59
- return tslib_1.__generator(this, function (_a) {
60
- switch (_a.label) {
61
- case 0: return [4 /*yield*/, load(path)];
62
- case 1:
63
- response = _a.sent();
64
- return [2 /*return*/, response.text()];
65
- }
66
- });
67
- });
68
- };
69
- exports.loadText = loadText;
70
- /**
71
- * Debounces: it limits the rate at which a function can fire.
72
- */
73
- var debounce = function (func, wait, immediate) {
74
- if (immediate === void 0) { immediate = false; }
75
- var timeout;
76
- return function () {
77
- // @ts-ignore
78
- var context = this;
79
- var args = arguments;
80
- var later = function () {
81
- timeout = null;
82
- if (!immediate) {
83
- func.apply(context, args);
84
- }
85
- };
86
- var callNow = immediate && !timeout;
87
- clearTimeout(timeout);
88
- timeout = setTimeout(later, wait);
89
- if (callNow) {
90
- func.apply(context, args);
91
- }
92
- };
93
- };
94
- exports.debounce = debounce;
95
- /**
96
- * Merges multiple maps.
97
- */
98
- var mergeMaps = function () {
99
- var maps = [];
100
- for (var _i = 0; _i < arguments.length; _i++) {
101
- maps[_i] = arguments[_i];
102
- }
103
- var resultMap = new Map();
104
- maps.forEach(function (map) {
105
- map.forEach(function (v, k) {
106
- resultMap.set(k, v);
107
- });
108
- });
109
- return resultMap;
110
- };
111
- exports.mergeMaps = mergeMaps;
112
- /**
113
- * Creates a HTML image element based on a SVG string, whereas all the embedded assets in the SVG (eg: fonts, images)
114
- * are already loaded and exchanged by their base64 representation.\
115
- * There the output image can exist as "standalone" image and may be used for example as a paintable.
116
- *
117
- * !!CAUTION!!: The used functions within this code section are very well evaluated since most alternatives
118
- * somehow don't work in Safari, as mentioned in the following BJS forum entries:
119
- * - https://forum.babylonjs.com/t/drawing-svg-content-text-into-dynamictexture-doesnt-work-in-safari-v15/25048
120
- * - https://forum.babylonjs.com/t/texture-createfrombase64string-doesnt-seem-to-work-for-ios-devices-initially/25502
121
- */
122
- var createImageFromSvg = function (svgSrc) {
123
- return tslib_1.__awaiter(this, void 0, void 0, function () {
124
- var svgWithAssetsEmbedded, svgEncoded;
125
- return tslib_1.__generator(this, function (_a) {
126
- switch (_a.label) {
127
- case 0: return [4 /*yield*/, _embedAssets(svgSrc)];
128
- case 1:
129
- svgWithAssetsEmbedded = _a.sent();
130
- svgEncoded = 'data:image/svg+xml,' + encodeURIComponent(svgWithAssetsEmbedded);
131
- return [2 /*return*/, createImageFromImgSrc(svgEncoded)];
132
- }
133
- });
134
- });
135
- };
136
- exports.createImageFromSvg = createImageFromSvg;
137
- /**
138
- * Creates an HTML image element from a dedicated image source.\
139
- * Also waits until the image has loaded.
140
- *
141
- * !!CAUTION!!: The `setTimeout` after loading is finished is required due to a Safari bug:
142
- * - https://bugs.webkit.org/show_bug.cgi?id=39059
143
- * - https://bugs.webkit.org/show_bug.cgi?id=219770
144
- *
145
- * It's not 100% ensured that the timeout solves the issue in every case, but there is no other way unfortunately.\
146
- * => Keep an eye on it in future projects
147
- *
148
- * @param imgSrc Theoretically every source is valid which is also supported by
149
- * [HTMLImageElement.src](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/src).\
150
- * Known exceptions are SVGs with embedded assets that are provided as object URL. See comments in
151
- * {@link createImageFromSvg} for further details.
152
- */
153
- var createImageFromImgSrc = function (imgSrc) {
154
- return tslib_1.__awaiter(this, void 0, void 0, function () {
155
- var img;
156
- return tslib_1.__generator(this, function (_a) {
157
- switch (_a.label) {
158
- case 0:
159
- img = new Image();
160
- return [4 /*yield*/, new Promise(function (resolve) {
161
- img.onload = function () {
162
- setTimeout(resolve, 0);
163
- };
164
- img.src = imgSrc;
165
- })];
166
- case 1:
167
- _a.sent();
168
- return [2 /*return*/, img];
169
- }
170
- });
171
- });
172
- };
173
- exports.createImageFromImgSrc = createImageFromImgSrc;
174
- /**
175
- * Replaces all supported image & font URLs in the given SVG with their base64 representation.
176
- */
177
- var _embedAssets = function (svgSrc) {
178
- return tslib_1.__awaiter(this, void 0, void 0, function () {
179
- var _imageExtensions, _fontExtensions, _assetExtensions, urlRegex, allUrls, assetUrls, assetBase64Fetcher, assetFetcherResults;
180
- return tslib_1.__generator(this, function (_a) {
181
- switch (_a.label) {
182
- case 0:
183
- _imageExtensions = ['png', 'gif', 'jpg', 'jpeg', 'svg', 'bmp'];
184
- _fontExtensions = ['woff2', 'woff', 'ttf', 'otf'];
185
- _assetExtensions = tslib_1.__spreadArrays(_imageExtensions, _fontExtensions);
186
- urlRegex = /(\bhttps?:\/\/[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|])/gi;
187
- allUrls = svgSrc.match(urlRegex);
188
- assetUrls = allUrls.filter(function (url) {
189
- return _assetExtensions.some(function (extension) { return url.toLowerCase().endsWith("." + extension); });
190
- });
191
- assetBase64Fetcher = assetUrls.map(_fetchBase64AssetUrl);
192
- return [4 /*yield*/, Promise.all(assetBase64Fetcher)];
193
- case 1:
194
- assetFetcherResults = _a.sent();
195
- return [2 /*return*/, assetFetcherResults.reduce(function (svgSrc, x) { return svgSrc.replace(x.url, x.base64); }, svgSrc)];
196
- }
197
- });
198
- });
199
- };
200
- /**
201
- * Fetch asset (image or font) and convert it to base64 string representation.
202
- */
203
- var _fetchBase64AssetUrl = function (assetUrl) {
204
- return tslib_1.__awaiter(this, void 0, void 0, function () {
205
- var _this = this;
206
- return tslib_1.__generator(this, function (_a) {
207
- // TODO: Cache known base64 representation and only fetch/convert when not already known.
208
- // Usually the fetch shouldn't hit the network but the browser cache since the SVG was already drawn.
209
- // See comments of "createImageBitmapFromSvg" for some considerations...
210
- return [2 /*return*/, new Promise(function (resolve, reject) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
211
- var resp, blob, reader;
212
- return tslib_1.__generator(this, function (_a) {
213
- switch (_a.label) {
214
- case 0: return [4 /*yield*/, fetch(assetUrl)];
215
- case 1:
216
- resp = _a.sent();
217
- return [4 /*yield*/, resp.blob()];
218
- case 2:
219
- blob = _a.sent();
220
- reader = new FileReader();
221
- reader.onloadend = function (event) {
222
- var target = event.target;
223
- if (!target) {
224
- return reject("Asset with URL \"" + assetUrl + "\" could not be loaded.");
225
- }
226
- var result = target.result;
227
- if (!result) {
228
- return reject("Asset with URL \"" + assetUrl + "\" returned an empty result.");
229
- }
230
- resolve({
231
- url: assetUrl,
232
- base64: result.toString(),
233
- });
234
- };
235
- reader.readAsDataURL(blob);
236
- return [2 /*return*/];
237
- }
238
- });
239
- }); })];
240
- });
241
- });
242
- };
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createImageFromImgSrc = exports.createImageFromSvg = exports.mergeMaps = exports.debounce = exports.loadJavascript = exports.loadText = exports.loadJson = exports.sleep = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var event_1 = require("../classes/event");
6
+ /**
7
+ * Sleeps for a certain amount of microseconds.
8
+ */
9
+ var sleep = function (ms) { return new Promise(function (resolve) { return window.setTimeout(resolve, ms); }); };
10
+ exports.sleep = sleep;
11
+ /**
12
+ * Loads any kind of response from given path.
13
+ * @emits {@link Event.LOADING_START}
14
+ * @emits {@link Event.LOADING_END}
15
+ */
16
+ var load = function (path) {
17
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
18
+ var data;
19
+ return tslib_1.__generator(this, function (_a) {
20
+ switch (_a.label) {
21
+ case 0:
22
+ event_1.emitter.emit(event_1.Event.LOADING_START, path);
23
+ return [4 /*yield*/, fetch(path)];
24
+ case 1:
25
+ data = _a.sent();
26
+ event_1.emitter.emit(event_1.Event.LOADING_END, path);
27
+ return [2 /*return*/, data];
28
+ }
29
+ });
30
+ });
31
+ };
32
+ /**
33
+ * Loads a JSON from a given path.
34
+ * @emits {@link Event.LOADING_START}
35
+ * @emits {@link Event.LOADING_END}
36
+ */
37
+ var loadJson = function (path) {
38
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
39
+ var response;
40
+ return tslib_1.__generator(this, function (_a) {
41
+ switch (_a.label) {
42
+ case 0: return [4 /*yield*/, load(path)];
43
+ case 1:
44
+ response = _a.sent();
45
+ return [2 /*return*/, response.json()];
46
+ }
47
+ });
48
+ });
49
+ };
50
+ exports.loadJson = loadJson;
51
+ /**
52
+ * Loads a Text from a given path.
53
+ * @emits {@link Event.LOADING_START}
54
+ * @emits {@link Event.LOADING_END}
55
+ */
56
+ var loadText = function (path) {
57
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
58
+ var response;
59
+ return tslib_1.__generator(this, function (_a) {
60
+ switch (_a.label) {
61
+ case 0: return [4 /*yield*/, load(path)];
62
+ case 1:
63
+ response = _a.sent();
64
+ return [2 /*return*/, response.text()];
65
+ }
66
+ });
67
+ });
68
+ };
69
+ exports.loadText = loadText;
70
+ /**
71
+ * Loads a Javascript ressource.\
72
+ * Usefull for including packages via CDN (eg: BabylonJS Inspector)
73
+ */
74
+ var loadJavascript = function (url) {
75
+ return new Promise(function (resolve) {
76
+ var scriptEl = document.createElement('script');
77
+ scriptEl.setAttribute('src', url);
78
+ scriptEl.setAttribute('type', 'text/javascript');
79
+ scriptEl.setAttribute('crossorigin', 'anonymous');
80
+ scriptEl.onload = function () { return resolve(); };
81
+ document.head.appendChild(scriptEl);
82
+ });
83
+ };
84
+ exports.loadJavascript = loadJavascript;
85
+ /**
86
+ * Debounces: it limits the rate at which a function can fire.
87
+ */
88
+ var debounce = function (func, wait, immediate) {
89
+ if (immediate === void 0) { immediate = false; }
90
+ var timeout;
91
+ return function () {
92
+ // @ts-ignore
93
+ var context = this;
94
+ var args = arguments;
95
+ var later = function () {
96
+ timeout = null;
97
+ if (!immediate) {
98
+ func.apply(context, args);
99
+ }
100
+ };
101
+ var callNow = immediate && !timeout;
102
+ clearTimeout(timeout);
103
+ timeout = setTimeout(later, wait);
104
+ if (callNow) {
105
+ func.apply(context, args);
106
+ }
107
+ };
108
+ };
109
+ exports.debounce = debounce;
110
+ /**
111
+ * Merges multiple maps.
112
+ */
113
+ var mergeMaps = function () {
114
+ var maps = [];
115
+ for (var _i = 0; _i < arguments.length; _i++) {
116
+ maps[_i] = arguments[_i];
117
+ }
118
+ var resultMap = new Map();
119
+ maps.forEach(function (map) {
120
+ map.forEach(function (v, k) {
121
+ resultMap.set(k, v);
122
+ });
123
+ });
124
+ return resultMap;
125
+ };
126
+ exports.mergeMaps = mergeMaps;
127
+ /**
128
+ * Creates a HTML image element based on a SVG string, whereas all the embedded assets in the SVG (eg: fonts, images)
129
+ * are already loaded and exchanged by their base64 representation.\
130
+ * There the output image can exist as "standalone" image and may be used for example as a paintable.
131
+ *
132
+ * !!CAUTION!!: The used functions within this code section are very well evaluated since most alternatives
133
+ * somehow don't work in Safari, as mentioned in the following BJS forum entries:
134
+ * - https://forum.babylonjs.com/t/drawing-svg-content-text-into-dynamictexture-doesnt-work-in-safari-v15/25048
135
+ * - https://forum.babylonjs.com/t/texture-createfrombase64string-doesnt-seem-to-work-for-ios-devices-initially/25502
136
+ */
137
+ var createImageFromSvg = function (svgSrc) {
138
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
139
+ var svgWithAssetsEmbedded, svgEncoded;
140
+ return tslib_1.__generator(this, function (_a) {
141
+ switch (_a.label) {
142
+ case 0: return [4 /*yield*/, _embedAssets(svgSrc)];
143
+ case 1:
144
+ svgWithAssetsEmbedded = _a.sent();
145
+ svgEncoded = 'data:image/svg+xml,' + encodeURIComponent(svgWithAssetsEmbedded);
146
+ return [2 /*return*/, createImageFromImgSrc(svgEncoded)];
147
+ }
148
+ });
149
+ });
150
+ };
151
+ exports.createImageFromSvg = createImageFromSvg;
152
+ /**
153
+ * Creates an HTML image element from a dedicated image source.\
154
+ * Also waits until the image has loaded.
155
+ *
156
+ * !!CAUTION!!: The `setTimeout` after loading is finished is required due to a Safari bug:
157
+ * - https://bugs.webkit.org/show_bug.cgi?id=39059
158
+ * - https://bugs.webkit.org/show_bug.cgi?id=219770
159
+ *
160
+ * It's not 100% ensured that the timeout solves the issue in every case, but there is no other way unfortunately.\
161
+ * => Keep an eye on it in future projects
162
+ *
163
+ * @param imgSrc Theoretically every source is valid which is also supported by
164
+ * [HTMLImageElement.src](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/src).\
165
+ * Known exceptions are SVGs with embedded assets that are provided as object URL. See comments in
166
+ * {@link createImageFromSvg} for further details.
167
+ */
168
+ var createImageFromImgSrc = function (imgSrc) {
169
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
170
+ var img;
171
+ return tslib_1.__generator(this, function (_a) {
172
+ switch (_a.label) {
173
+ case 0:
174
+ img = new Image();
175
+ return [4 /*yield*/, new Promise(function (resolve) {
176
+ img.onload = function () {
177
+ setTimeout(resolve, 0);
178
+ };
179
+ img.src = imgSrc;
180
+ })];
181
+ case 1:
182
+ _a.sent();
183
+ return [2 /*return*/, img];
184
+ }
185
+ });
186
+ });
187
+ };
188
+ exports.createImageFromImgSrc = createImageFromImgSrc;
189
+ /**
190
+ * Replaces all supported image & font URLs in the given SVG with their base64 representation.
191
+ */
192
+ var _embedAssets = function (svgSrc) {
193
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
194
+ var _imageExtensions, _fontExtensions, _assetExtensions, urlRegex, allUrls, assetUrls, assetBase64Fetcher, assetFetcherResults;
195
+ return tslib_1.__generator(this, function (_a) {
196
+ switch (_a.label) {
197
+ case 0:
198
+ _imageExtensions = ['png', 'gif', 'jpg', 'jpeg', 'svg', 'bmp'];
199
+ _fontExtensions = ['woff2', 'woff', 'ttf', 'otf'];
200
+ _assetExtensions = tslib_1.__spreadArrays(_imageExtensions, _fontExtensions);
201
+ urlRegex = /(\bhttps?:\/\/[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|])/gi;
202
+ allUrls = svgSrc.match(urlRegex);
203
+ assetUrls = allUrls.filter(function (url) {
204
+ return _assetExtensions.some(function (extension) { return url.toLowerCase().endsWith("." + extension); });
205
+ });
206
+ assetBase64Fetcher = assetUrls.map(_fetchBase64AssetUrl);
207
+ return [4 /*yield*/, Promise.all(assetBase64Fetcher)];
208
+ case 1:
209
+ assetFetcherResults = _a.sent();
210
+ return [2 /*return*/, assetFetcherResults.reduce(function (svgSrc, x) { return svgSrc.replace(x.url, x.base64); }, svgSrc)];
211
+ }
212
+ });
213
+ });
214
+ };
215
+ /**
216
+ * Fetch asset (image or font) and convert it to base64 string representation.
217
+ */
218
+ var _fetchBase64AssetUrl = function (assetUrl) {
219
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
220
+ var _this = this;
221
+ return tslib_1.__generator(this, function (_a) {
222
+ // TODO: Cache known base64 representation and only fetch/convert when not already known.
223
+ // Usually the fetch shouldn't hit the network but the browser cache since the SVG was already drawn.
224
+ // See comments of "createImageBitmapFromSvg" for some considerations...
225
+ return [2 /*return*/, new Promise(function (resolve, reject) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
226
+ var resp, blob, reader;
227
+ return tslib_1.__generator(this, function (_a) {
228
+ switch (_a.label) {
229
+ case 0: return [4 /*yield*/, fetch(assetUrl)];
230
+ case 1:
231
+ resp = _a.sent();
232
+ return [4 /*yield*/, resp.blob()];
233
+ case 2:
234
+ blob = _a.sent();
235
+ reader = new FileReader();
236
+ reader.onloadend = function (event) {
237
+ var target = event.target;
238
+ if (!target) {
239
+ return reject("Asset with URL \"" + assetUrl + "\" could not be loaded.");
240
+ }
241
+ var result = target.result;
242
+ if (!result) {
243
+ return reject("Asset with URL \"" + assetUrl + "\" returned an empty result.");
244
+ }
245
+ resolve({
246
+ url: assetUrl,
247
+ base64: result.toString(),
248
+ });
249
+ };
250
+ reader.readAsDataURL(blob);
251
+ return [2 /*return*/];
252
+ }
253
+ });
254
+ }); })];
255
+ });
256
+ });
257
+ };
243
258
  //# sourceMappingURL=resourceHelper.js.map