@tsparticles/shape-image 3.0.0-alpha.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,330 @@
1
+ /*!
2
+ * Author : Matteo Bruni
3
+ * MIT license: https://opensource.org/licenses/MIT
4
+ * Demo / Generator : https://particles.js.org/
5
+ * GitHub : https://www.github.com/matteobruni/tsparticles
6
+ * How to use? : Check the GitHub README
7
+ * v3.0.0-alpha.0
8
+ */
9
+ (function webpackUniversalModuleDefinition(root, factory) {
10
+ if(typeof exports === 'object' && typeof module === 'object')
11
+ module.exports = factory(require("@tsparticles/engine"));
12
+ else if(typeof define === 'function' && define.amd)
13
+ define(["@tsparticles/engine"], factory);
14
+ else {
15
+ var a = typeof exports === 'object' ? factory(require("@tsparticles/engine")) : factory(root["window"]);
16
+ for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
17
+ }
18
+ })(this, (__WEBPACK_EXTERNAL_MODULE__533__) => {
19
+ return /******/ (() => { // webpackBootstrap
20
+ /******/ "use strict";
21
+ /******/ var __webpack_modules__ = ({
22
+
23
+ /***/ 533:
24
+ /***/ ((module) => {
25
+
26
+ module.exports = __WEBPACK_EXTERNAL_MODULE__533__;
27
+
28
+ /***/ })
29
+
30
+ /******/ });
31
+ /************************************************************************/
32
+ /******/ // The module cache
33
+ /******/ var __webpack_module_cache__ = {};
34
+ /******/
35
+ /******/ // The require function
36
+ /******/ function __webpack_require__(moduleId) {
37
+ /******/ // Check if module is in cache
38
+ /******/ var cachedModule = __webpack_module_cache__[moduleId];
39
+ /******/ if (cachedModule !== undefined) {
40
+ /******/ return cachedModule.exports;
41
+ /******/ }
42
+ /******/ // Create a new module (and put it into the cache)
43
+ /******/ var module = __webpack_module_cache__[moduleId] = {
44
+ /******/ // no module.id needed
45
+ /******/ // no module.loaded needed
46
+ /******/ exports: {}
47
+ /******/ };
48
+ /******/
49
+ /******/ // Execute the module function
50
+ /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
51
+ /******/
52
+ /******/ // Return the exports of the module
53
+ /******/ return module.exports;
54
+ /******/ }
55
+ /******/
56
+ /************************************************************************/
57
+ /******/ /* webpack/runtime/define property getters */
58
+ /******/ (() => {
59
+ /******/ // define getter functions for harmony exports
60
+ /******/ __webpack_require__.d = (exports, definition) => {
61
+ /******/ for(var key in definition) {
62
+ /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
63
+ /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
64
+ /******/ }
65
+ /******/ }
66
+ /******/ };
67
+ /******/ })();
68
+ /******/
69
+ /******/ /* webpack/runtime/hasOwnProperty shorthand */
70
+ /******/ (() => {
71
+ /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
72
+ /******/ })();
73
+ /******/
74
+ /******/ /* webpack/runtime/make namespace object */
75
+ /******/ (() => {
76
+ /******/ // define __esModule on exports
77
+ /******/ __webpack_require__.r = (exports) => {
78
+ /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
79
+ /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
80
+ /******/ }
81
+ /******/ Object.defineProperty(exports, '__esModule', { value: true });
82
+ /******/ };
83
+ /******/ })();
84
+ /******/
85
+ /************************************************************************/
86
+ var __webpack_exports__ = {};
87
+ // This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
88
+ (() => {
89
+ // ESM COMPAT FLAG
90
+ __webpack_require__.r(__webpack_exports__);
91
+
92
+ // EXPORTS
93
+ __webpack_require__.d(__webpack_exports__, {
94
+ "loadImageShape": () => (/* binding */ loadImageShape)
95
+ });
96
+
97
+ // EXTERNAL MODULE: external {"commonjs":"@tsparticles/engine","commonjs2":"@tsparticles/engine","amd":"@tsparticles/engine","root":"window"}
98
+ var engine_root_window_ = __webpack_require__(533);
99
+ ;// CONCATENATED MODULE: ./dist/browser/Utils.js
100
+
101
+ const currentColorRegex = /(#(?:[0-9a-f]{2}){2,4}|(#[0-9a-f]{3})|(rgb|hsl)a?\((-?\d+%?[,\s]+){2,3}\s*[\d.]+%?\))|currentcolor/gi;
102
+ function replaceColorSvg(imageShape, color, opacity) {
103
+ const {
104
+ svgData
105
+ } = imageShape;
106
+ if (!svgData) {
107
+ return "";
108
+ }
109
+ const colorStyle = (0,engine_root_window_.getStyleFromHsl)(color, opacity);
110
+ if (svgData.includes("fill")) {
111
+ return svgData.replace(currentColorRegex, () => colorStyle);
112
+ }
113
+ const preFillIndex = svgData.indexOf(">");
114
+ return `${svgData.substring(0, preFillIndex)} fill="${colorStyle}"${svgData.substring(preFillIndex)}`;
115
+ }
116
+ async function loadImage(image) {
117
+ return new Promise(resolve => {
118
+ image.loading = true;
119
+ const img = new Image();
120
+ image.element = img;
121
+ img.addEventListener("load", () => {
122
+ image.loading = false;
123
+ resolve();
124
+ });
125
+ img.addEventListener("error", () => {
126
+ image.element = undefined;
127
+ image.error = true;
128
+ image.loading = false;
129
+ console.error(`Error tsParticles - loading image: ${image.source}`);
130
+ resolve();
131
+ });
132
+ img.src = image.source;
133
+ });
134
+ }
135
+ async function downloadSvgImage(image) {
136
+ if (image.type !== "svg") {
137
+ await loadImage(image);
138
+ return;
139
+ }
140
+ image.loading = true;
141
+ const response = await fetch(image.source);
142
+ if (!response.ok) {
143
+ console.error("Error tsParticles - Image not found");
144
+ image.error = true;
145
+ }
146
+ if (!image.error) {
147
+ image.svgData = await response.text();
148
+ }
149
+ image.loading = false;
150
+ }
151
+ function replaceImageColor(image, imageData, color, particle) {
152
+ var _a, _b, _c;
153
+ const svgColoredData = replaceColorSvg(image, color, (_b = (_a = particle.opacity) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : 1),
154
+ imageRes = {
155
+ color,
156
+ data: Object.assign(Object.assign({}, image), {
157
+ svgData: svgColoredData
158
+ }),
159
+ loaded: false,
160
+ ratio: imageData.width / imageData.height,
161
+ replaceColor: (_c = imageData.replaceColor) !== null && _c !== void 0 ? _c : imageData.replace_color,
162
+ source: imageData.src
163
+ };
164
+ return new Promise(resolve => {
165
+ const svg = new Blob([svgColoredData], {
166
+ type: "image/svg+xml"
167
+ }),
168
+ domUrl = URL || window.URL || window.webkitURL || window,
169
+ url = domUrl.createObjectURL(svg),
170
+ img = new Image();
171
+ img.addEventListener("load", () => {
172
+ imageRes.loaded = true;
173
+ imageRes.element = img;
174
+ resolve(imageRes);
175
+ domUrl.revokeObjectURL(url);
176
+ });
177
+ img.addEventListener("error", async () => {
178
+ domUrl.revokeObjectURL(url);
179
+ const img2 = Object.assign(Object.assign({}, image), {
180
+ error: false,
181
+ loading: true
182
+ });
183
+ await loadImage(img2);
184
+ imageRes.loaded = true;
185
+ imageRes.element = img2.element;
186
+ resolve(imageRes);
187
+ });
188
+ img.src = url;
189
+ });
190
+ }
191
+ ;// CONCATENATED MODULE: ./dist/browser/ImageDrawer.js
192
+
193
+ class ImageDrawer {
194
+ constructor() {
195
+ this._images = [];
196
+ }
197
+ addImage(container, image) {
198
+ const containerImages = this.getImages(container);
199
+ containerImages === null || containerImages === void 0 ? void 0 : containerImages.images.push(image);
200
+ }
201
+ destroy() {
202
+ this._images = [];
203
+ }
204
+ draw(context, particle, radius, opacity) {
205
+ var _a;
206
+ const image = particle.image,
207
+ element = image === null || image === void 0 ? void 0 : image.element;
208
+ if (!element) {
209
+ return;
210
+ }
211
+ const ratio = (_a = image === null || image === void 0 ? void 0 : image.ratio) !== null && _a !== void 0 ? _a : 1,
212
+ pos = {
213
+ x: -radius,
214
+ y: -radius
215
+ };
216
+ context.globalAlpha = opacity;
217
+ context.drawImage(element, pos.x, pos.y, radius * 2, radius * 2 / ratio);
218
+ context.globalAlpha = 1;
219
+ }
220
+ getImages(container) {
221
+ const containerImages = this._images.find(t => t.id === container.id);
222
+ if (!containerImages) {
223
+ this._images.push({
224
+ id: container.id,
225
+ images: []
226
+ });
227
+ return this.getImages(container);
228
+ } else {
229
+ return containerImages;
230
+ }
231
+ }
232
+ getSidesCount() {
233
+ return 12;
234
+ }
235
+ loadShape(particle) {
236
+ if (particle.shape !== "image" && particle.shape !== "images") {
237
+ return;
238
+ }
239
+ const container = particle.container,
240
+ images = this.getImages(container).images,
241
+ imageData = particle.shapeData,
242
+ image = images.find(t => t.source === imageData.src);
243
+ if (!image) {
244
+ this.loadImageShape(container, imageData).then(() => {
245
+ this.loadShape(particle);
246
+ });
247
+ }
248
+ }
249
+ particleInit(container, particle) {
250
+ var _a;
251
+ if (particle.shape !== "image" && particle.shape !== "images") {
252
+ return;
253
+ }
254
+ const images = this.getImages(container).images,
255
+ imageData = particle.shapeData,
256
+ color = particle.getFillColor(),
257
+ replaceColor = (_a = imageData.replaceColor) !== null && _a !== void 0 ? _a : imageData.replace_color,
258
+ image = images.find(t => t.source === imageData.src);
259
+ if (!image) {
260
+ return;
261
+ }
262
+ if (image.loading) {
263
+ setTimeout(() => {
264
+ this.particleInit(container, particle);
265
+ });
266
+ return;
267
+ }
268
+ (async () => {
269
+ var _a, _b;
270
+ let imageRes;
271
+ if (image.svgData && color) {
272
+ imageRes = await replaceImageColor(image, imageData, color, particle);
273
+ } else {
274
+ imageRes = {
275
+ color,
276
+ data: image,
277
+ element: image.element,
278
+ loaded: true,
279
+ ratio: imageData.width / imageData.height,
280
+ replaceColor: replaceColor,
281
+ source: imageData.src
282
+ };
283
+ }
284
+ if (!imageRes.ratio) {
285
+ imageRes.ratio = 1;
286
+ }
287
+ const fill = (_a = imageData.fill) !== null && _a !== void 0 ? _a : particle.fill,
288
+ close = (_b = imageData.close) !== null && _b !== void 0 ? _b : particle.close,
289
+ imageShape = {
290
+ image: imageRes,
291
+ fill,
292
+ close
293
+ };
294
+ particle.image = imageShape.image;
295
+ particle.fill = imageShape.fill;
296
+ particle.close = imageShape.close;
297
+ })();
298
+ }
299
+ async loadImageShape(container, imageShape) {
300
+ var _a;
301
+ const source = imageShape.src;
302
+ if (!source) {
303
+ throw new Error("Error tsParticles - No image.src");
304
+ }
305
+ try {
306
+ const image = {
307
+ source: source,
308
+ type: source.substring(source.length - 3),
309
+ error: false,
310
+ loading: true
311
+ };
312
+ this.addImage(container, image);
313
+ const imageFunc = ((_a = imageShape.replaceColor) !== null && _a !== void 0 ? _a : imageShape.replace_color) ? downloadSvgImage : loadImage;
314
+ await imageFunc(image);
315
+ } catch (_b) {
316
+ throw new Error(`tsParticles error - ${imageShape.src} not found`);
317
+ }
318
+ }
319
+ }
320
+ ;// CONCATENATED MODULE: ./dist/browser/index.js
321
+
322
+ async function loadImageShape(engine) {
323
+ await engine.addShape(["image", "images"], new ImageDrawer());
324
+ }
325
+ })();
326
+
327
+ /******/ return __webpack_exports__;
328
+ /******/ })()
329
+ ;
330
+ });
@@ -0,0 +1,2 @@
1
+ /*! For license information please see tsparticles.shape.image.min.js.LICENSE.txt */
2
+ !function(e,r){if("object"==typeof exports&&"object"==typeof module)module.exports=r(require("@tsparticles/engine"));else if("function"==typeof define&&define.amd)define(["@tsparticles/engine"],r);else{var t="object"==typeof exports?r(require("@tsparticles/engine")):r(e.window);for(var o in t)("object"==typeof exports?exports:e)[o]=t[o]}}(this,(e=>(()=>{"use strict";var r={533:r=>{r.exports=e}},t={};function o(e){var a=t[e];if(void 0!==a)return a.exports;var i=t[e]={exports:{}};return r[e](i,i.exports,o),i.exports}o.d=(e,r)=>{for(var t in r)o.o(r,t)&&!o.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:r[t]})},o.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),o.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var a={};return(()=>{o.r(a),o.d(a,{loadImageShape:()=>l});var e=o(533);const r=/(#(?:[0-9a-f]{2}){2,4}|(#[0-9a-f]{3})|(rgb|hsl)a?\((-?\d+%?[,\s]+){2,3}\s*[\d.]+%?\))|currentcolor/gi;async function t(e){return new Promise((r=>{e.loading=!0;const t=new Image;e.element=t,t.addEventListener("load",(()=>{e.loading=!1,r()})),t.addEventListener("error",(()=>{e.element=void 0,e.error=!0,e.loading=!1,console.error(`Error tsParticles - loading image: ${e.source}`),r()})),t.src=e.source}))}async function i(e){if("svg"!==e.type)return void await t(e);e.loading=!0;const r=await fetch(e.source);r.ok||(console.error("Error tsParticles - Image not found"),e.error=!0),e.error||(e.svgData=await r.text()),e.loading=!1}function s(o,a,i,s){var n,l,c;const d=function(t,o,a){const{svgData:i}=t;if(!i)return"";const s=(0,e.getStyleFromHsl)(o,a);if(i.includes("fill"))return i.replace(r,(()=>s));const n=i.indexOf(">");return`${i.substring(0,n)} fill="${s}"${i.substring(n)}`}(o,i,null!==(l=null===(n=s.opacity)||void 0===n?void 0:n.value)&&void 0!==l?l:1),g={color:i,data:Object.assign(Object.assign({},o),{svgData:d}),loaded:!1,ratio:a.width/a.height,replaceColor:null!==(c=a.replaceColor)&&void 0!==c?c:a.replace_color,source:a.src};return new Promise((e=>{const r=new Blob([d],{type:"image/svg+xml"}),a=URL||window.URL||window.webkitURL||window,i=a.createObjectURL(r),s=new Image;s.addEventListener("load",(()=>{g.loaded=!0,g.element=s,e(g),a.revokeObjectURL(i)})),s.addEventListener("error",(async()=>{a.revokeObjectURL(i);const r=Object.assign(Object.assign({},o),{error:!1,loading:!0});await t(r),g.loaded=!0,g.element=r.element,e(g)})),s.src=i}))}class n{constructor(){this._images=[]}addImage(e,r){const t=this.getImages(e);null==t||t.images.push(r)}destroy(){this._images=[]}draw(e,r,t,o){var a;const i=r.image,s=null==i?void 0:i.element;if(!s)return;const n=null!==(a=null==i?void 0:i.ratio)&&void 0!==a?a:1,l={x:-t,y:-t};e.globalAlpha=o,e.drawImage(s,l.x,l.y,2*t,2*t/n),e.globalAlpha=1}getImages(e){const r=this._images.find((r=>r.id===e.id));return r||(this._images.push({id:e.id,images:[]}),this.getImages(e))}getSidesCount(){return 12}loadShape(e){if("image"!==e.shape&&"images"!==e.shape)return;const r=e.container,t=this.getImages(r).images,o=e.shapeData;t.find((e=>e.source===o.src))||this.loadImageShape(r,o).then((()=>{this.loadShape(e)}))}particleInit(e,r){var t;if("image"!==r.shape&&"images"!==r.shape)return;const o=this.getImages(e).images,a=r.shapeData,i=r.getFillColor(),n=null!==(t=a.replaceColor)&&void 0!==t?t:a.replace_color,l=o.find((e=>e.source===a.src));l&&(l.loading?setTimeout((()=>{this.particleInit(e,r)})):(async()=>{var e,t;let o;o=l.svgData&&i?await s(l,a,i,r):{color:i,data:l,element:l.element,loaded:!0,ratio:a.width/a.height,replaceColor:n,source:a.src},o.ratio||(o.ratio=1);const c={image:o,fill:null!==(e=a.fill)&&void 0!==e?e:r.fill,close:null!==(t=a.close)&&void 0!==t?t:r.close};r.image=c.image,r.fill=c.fill,r.close=c.close})())}async loadImageShape(e,r){var o;const a=r.src;if(!a)throw new Error("Error tsParticles - No image.src");try{const s={source:a,type:a.substring(a.length-3),error:!1,loading:!0};this.addImage(e,s);const n=(null!==(o=r.replaceColor)&&void 0!==o?o:r.replace_color)?i:t;await n(s)}catch(e){throw new Error(`tsParticles error - ${r.src} not found`)}}}async function l(e){await e.addShape(["image","images"],new n)}})(),a})()));
@@ -0,0 +1,8 @@
1
+ /*!
2
+ * Author : Matteo Bruni
3
+ * MIT license: https://opensource.org/licenses/MIT
4
+ * Demo / Generator : https://particles.js.org/
5
+ * GitHub : https://www.github.com/matteobruni/tsparticles
6
+ * How to use? : Check the GitHub README
7
+ * v3.0.0-alpha.0
8
+ */
@@ -0,0 +1,8 @@
1
+ import type { IShapeValues } from "@tsparticles/engine";
2
+ export interface IImageShape extends IShapeValues {
3
+ height: number;
4
+ replaceColor: boolean;
5
+ replace_color: boolean;
6
+ src: string;
7
+ width: number;
8
+ }
@@ -0,0 +1,14 @@
1
+ import type { Container, IShapeDrawer } from "@tsparticles/engine";
2
+ import type { ContainerImage, IImage, ImageParticle } from "./Utils";
3
+ export declare class ImageDrawer implements IShapeDrawer {
4
+ private _images;
5
+ constructor();
6
+ addImage(container: Container, image: IImage): void;
7
+ destroy(): void;
8
+ draw(context: CanvasRenderingContext2D, particle: ImageParticle, radius: number, opacity: number): void;
9
+ getImages(container: Container): ContainerImage;
10
+ getSidesCount(): number;
11
+ loadShape(particle: ImageParticle): void;
12
+ particleInit(container: Container, particle: ImageParticle): void;
13
+ private loadImageShape;
14
+ }
@@ -0,0 +1,30 @@
1
+ import type { IHsl, Particle } from "@tsparticles/engine";
2
+ import type { IImageShape } from "./IImageShape";
3
+ export interface IImage {
4
+ color?: IHsl;
5
+ element?: HTMLImageElement;
6
+ error: boolean;
7
+ loading: boolean;
8
+ source: string;
9
+ svgData?: string;
10
+ type: string;
11
+ }
12
+ export interface IParticleImage {
13
+ color?: IHsl;
14
+ data: IImage;
15
+ element?: HTMLImageElement;
16
+ loaded?: boolean;
17
+ ratio: number;
18
+ replaceColor: boolean;
19
+ source: string;
20
+ }
21
+ export interface ContainerImage {
22
+ id: string;
23
+ images: IImage[];
24
+ }
25
+ export type ImageParticle = Particle & {
26
+ image?: IParticleImage;
27
+ };
28
+ export declare function loadImage(image: IImage): Promise<void>;
29
+ export declare function downloadSvgImage(image: IImage): Promise<void>;
30
+ export declare function replaceImageColor(image: IImage, imageData: IImageShape, color: IHsl, particle: Particle): Promise<IParticleImage>;
@@ -0,0 +1,2 @@
1
+ import type { Engine } from "@tsparticles/engine";
2
+ export declare function loadImageShape(engine: Engine): Promise<void>;
@@ -0,0 +1,12 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ });
@@ -0,0 +1,134 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports", "./Utils"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.ImageDrawer = void 0;
13
+ const Utils_1 = require("./Utils");
14
+ class ImageDrawer {
15
+ constructor() {
16
+ this._images = [];
17
+ }
18
+ addImage(container, image) {
19
+ const containerImages = this.getImages(container);
20
+ containerImages === null || containerImages === void 0 ? void 0 : containerImages.images.push(image);
21
+ }
22
+ destroy() {
23
+ this._images = [];
24
+ }
25
+ draw(context, particle, radius, opacity) {
26
+ var _a;
27
+ const image = particle.image, element = image === null || image === void 0 ? void 0 : image.element;
28
+ if (!element) {
29
+ return;
30
+ }
31
+ const ratio = (_a = image === null || image === void 0 ? void 0 : image.ratio) !== null && _a !== void 0 ? _a : 1, pos = {
32
+ x: -radius,
33
+ y: -radius,
34
+ };
35
+ context.globalAlpha = opacity;
36
+ context.drawImage(element, pos.x, pos.y, radius * 2, (radius * 2) / ratio);
37
+ context.globalAlpha = 1;
38
+ }
39
+ getImages(container) {
40
+ const containerImages = this._images.find((t) => t.id === container.id);
41
+ if (!containerImages) {
42
+ this._images.push({
43
+ id: container.id,
44
+ images: [],
45
+ });
46
+ return this.getImages(container);
47
+ }
48
+ else {
49
+ return containerImages;
50
+ }
51
+ }
52
+ getSidesCount() {
53
+ return 12;
54
+ }
55
+ loadShape(particle) {
56
+ if (particle.shape !== "image" && particle.shape !== "images") {
57
+ return;
58
+ }
59
+ const container = particle.container, images = this.getImages(container).images, imageData = particle.shapeData, image = images.find((t) => t.source === imageData.src);
60
+ if (!image) {
61
+ this.loadImageShape(container, imageData).then(() => {
62
+ this.loadShape(particle);
63
+ });
64
+ }
65
+ }
66
+ particleInit(container, particle) {
67
+ var _a;
68
+ if (particle.shape !== "image" && particle.shape !== "images") {
69
+ return;
70
+ }
71
+ const images = this.getImages(container).images, imageData = particle.shapeData, color = particle.getFillColor(), replaceColor = (_a = imageData.replaceColor) !== null && _a !== void 0 ? _a : imageData.replace_color, image = images.find((t) => t.source === imageData.src);
72
+ if (!image) {
73
+ return;
74
+ }
75
+ if (image.loading) {
76
+ setTimeout(() => {
77
+ this.particleInit(container, particle);
78
+ });
79
+ return;
80
+ }
81
+ (async () => {
82
+ var _a, _b;
83
+ let imageRes;
84
+ if (image.svgData && color) {
85
+ imageRes = await (0, Utils_1.replaceImageColor)(image, imageData, color, particle);
86
+ }
87
+ else {
88
+ imageRes = {
89
+ color,
90
+ data: image,
91
+ element: image.element,
92
+ loaded: true,
93
+ ratio: imageData.width / imageData.height,
94
+ replaceColor: replaceColor,
95
+ source: imageData.src,
96
+ };
97
+ }
98
+ if (!imageRes.ratio) {
99
+ imageRes.ratio = 1;
100
+ }
101
+ const fill = (_a = imageData.fill) !== null && _a !== void 0 ? _a : particle.fill, close = (_b = imageData.close) !== null && _b !== void 0 ? _b : particle.close, imageShape = {
102
+ image: imageRes,
103
+ fill,
104
+ close,
105
+ };
106
+ particle.image = imageShape.image;
107
+ particle.fill = imageShape.fill;
108
+ particle.close = imageShape.close;
109
+ })();
110
+ }
111
+ async loadImageShape(container, imageShape) {
112
+ var _a;
113
+ const source = imageShape.src;
114
+ if (!source) {
115
+ throw new Error("Error tsParticles - No image.src");
116
+ }
117
+ try {
118
+ const image = {
119
+ source: source,
120
+ type: source.substring(source.length - 3),
121
+ error: false,
122
+ loading: true,
123
+ };
124
+ this.addImage(container, image);
125
+ const imageFunc = ((_a = imageShape.replaceColor) !== null && _a !== void 0 ? _a : imageShape.replace_color) ? Utils_1.downloadSvgImage : Utils_1.loadImage;
126
+ await imageFunc(image);
127
+ }
128
+ catch (_b) {
129
+ throw new Error(`tsParticles error - ${imageShape.src} not found`);
130
+ }
131
+ }
132
+ }
133
+ exports.ImageDrawer = ImageDrawer;
134
+ });
package/umd/Utils.js ADDED
@@ -0,0 +1,94 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports", "@tsparticles/engine"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.replaceImageColor = exports.downloadSvgImage = exports.loadImage = void 0;
13
+ const engine_1 = require("@tsparticles/engine");
14
+ const currentColorRegex = /(#(?:[0-9a-f]{2}){2,4}|(#[0-9a-f]{3})|(rgb|hsl)a?\((-?\d+%?[,\s]+){2,3}\s*[\d.]+%?\))|currentcolor/gi;
15
+ function replaceColorSvg(imageShape, color, opacity) {
16
+ const { svgData } = imageShape;
17
+ if (!svgData) {
18
+ return "";
19
+ }
20
+ const colorStyle = (0, engine_1.getStyleFromHsl)(color, opacity);
21
+ if (svgData.includes("fill")) {
22
+ return svgData.replace(currentColorRegex, () => colorStyle);
23
+ }
24
+ const preFillIndex = svgData.indexOf(">");
25
+ return `${svgData.substring(0, preFillIndex)} fill="${colorStyle}"${svgData.substring(preFillIndex)}`;
26
+ }
27
+ async function loadImage(image) {
28
+ return new Promise((resolve) => {
29
+ image.loading = true;
30
+ const img = new Image();
31
+ image.element = img;
32
+ img.addEventListener("load", () => {
33
+ image.loading = false;
34
+ resolve();
35
+ });
36
+ img.addEventListener("error", () => {
37
+ image.element = undefined;
38
+ image.error = true;
39
+ image.loading = false;
40
+ console.error(`Error tsParticles - loading image: ${image.source}`);
41
+ resolve();
42
+ });
43
+ img.src = image.source;
44
+ });
45
+ }
46
+ exports.loadImage = loadImage;
47
+ async function downloadSvgImage(image) {
48
+ if (image.type !== "svg") {
49
+ await loadImage(image);
50
+ return;
51
+ }
52
+ image.loading = true;
53
+ const response = await fetch(image.source);
54
+ if (!response.ok) {
55
+ console.error("Error tsParticles - Image not found");
56
+ image.error = true;
57
+ }
58
+ if (!image.error) {
59
+ image.svgData = await response.text();
60
+ }
61
+ image.loading = false;
62
+ }
63
+ exports.downloadSvgImage = downloadSvgImage;
64
+ function replaceImageColor(image, imageData, color, particle) {
65
+ var _a, _b, _c;
66
+ const svgColoredData = replaceColorSvg(image, color, (_b = (_a = particle.opacity) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : 1), imageRes = {
67
+ color,
68
+ data: Object.assign(Object.assign({}, image), { svgData: svgColoredData }),
69
+ loaded: false,
70
+ ratio: imageData.width / imageData.height,
71
+ replaceColor: (_c = imageData.replaceColor) !== null && _c !== void 0 ? _c : imageData.replace_color,
72
+ source: imageData.src,
73
+ };
74
+ return new Promise((resolve) => {
75
+ const svg = new Blob([svgColoredData], { type: "image/svg+xml" }), domUrl = URL || window.URL || window.webkitURL || window, url = domUrl.createObjectURL(svg), img = new Image();
76
+ img.addEventListener("load", () => {
77
+ imageRes.loaded = true;
78
+ imageRes.element = img;
79
+ resolve(imageRes);
80
+ domUrl.revokeObjectURL(url);
81
+ });
82
+ img.addEventListener("error", async () => {
83
+ domUrl.revokeObjectURL(url);
84
+ const img2 = Object.assign(Object.assign({}, image), { error: false, loading: true });
85
+ await loadImage(img2);
86
+ imageRes.loaded = true;
87
+ imageRes.element = img2.element;
88
+ resolve(imageRes);
89
+ });
90
+ img.src = url;
91
+ });
92
+ }
93
+ exports.replaceImageColor = replaceImageColor;
94
+ });
package/umd/index.js ADDED
@@ -0,0 +1,18 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports", "./ImageDrawer"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.loadImageShape = void 0;
13
+ const ImageDrawer_1 = require("./ImageDrawer");
14
+ async function loadImageShape(engine) {
15
+ await engine.addShape(["image", "images"], new ImageDrawer_1.ImageDrawer());
16
+ }
17
+ exports.loadImageShape = loadImageShape;
18
+ });