@tsparticles/shape-image 3.9.1 → 4.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.
Files changed (54) hide show
  1. package/324.min.js +2 -0
  2. package/324.min.js.LICENSE.txt +1 -0
  3. package/554.min.js +2 -0
  4. package/554.min.js.LICENSE.txt +1 -0
  5. package/937.min.js +2 -0
  6. package/937.min.js.LICENSE.txt +1 -0
  7. package/968.min.js +2 -0
  8. package/968.min.js.LICENSE.txt +1 -0
  9. package/browser/GifUtils/Utils.js +10 -15
  10. package/browser/ImageDrawer.js +7 -14
  11. package/browser/ImagePreloader.js +2 -5
  12. package/browser/Utils.js +10 -10
  13. package/browser/index.js +21 -19
  14. package/cjs/GifUtils/ByteStream.js +1 -5
  15. package/cjs/GifUtils/Constants.js +2 -5
  16. package/cjs/GifUtils/Enums/DisposalMethod.js +2 -5
  17. package/cjs/GifUtils/Types/ApplicationExtension.js +1 -2
  18. package/cjs/GifUtils/Types/Frame.js +1 -2
  19. package/cjs/GifUtils/Types/GIF.js +1 -2
  20. package/cjs/GifUtils/Types/GIFDataHeaders.js +2 -5
  21. package/cjs/GifUtils/Types/GIFProgressCallbackFunction.js +1 -2
  22. package/cjs/GifUtils/Types/PlainTextData.js +1 -2
  23. package/cjs/GifUtils/Utils.js +41 -52
  24. package/cjs/IImageShape.js +1 -2
  25. package/cjs/ImageDrawer.js +11 -22
  26. package/cjs/ImagePreloader.js +5 -12
  27. package/cjs/Options/Classes/Preload.js +3 -7
  28. package/cjs/Options/Interfaces/IPreload.js +1 -2
  29. package/cjs/Utils.js +12 -17
  30. package/cjs/index.js +22 -23
  31. package/cjs/types.js +1 -2
  32. package/dist_browser_GifUtils_Utils_js.js +80 -0
  33. package/dist_browser_ImageDrawer_js.js +30 -0
  34. package/dist_browser_ImagePreloader_js.js +40 -0
  35. package/dist_browser_Utils_js.js +30 -0
  36. package/esm/GifUtils/Utils.js +10 -15
  37. package/esm/ImageDrawer.js +7 -14
  38. package/esm/ImagePreloader.js +2 -5
  39. package/esm/Utils.js +10 -10
  40. package/esm/index.js +21 -19
  41. package/package.json +4 -3
  42. package/report.html +5 -4
  43. package/tsparticles.shape.image.js +209 -110
  44. package/tsparticles.shape.image.min.js +1 -1
  45. package/tsparticles.shape.image.min.js.LICENSE.txt +1 -1
  46. package/types/ImagePreloader.d.ts +2 -3
  47. package/types/Options/Classes/Preload.d.ts +2 -2
  48. package/types/Utils.d.ts +1 -1
  49. package/types/index.d.ts +1 -1
  50. package/umd/GifUtils/Utils.js +10 -15
  51. package/umd/ImageDrawer.js +8 -15
  52. package/umd/ImagePreloader.js +2 -5
  53. package/umd/Utils.js +9 -9
  54. package/umd/index.js +57 -21
@@ -109,7 +109,7 @@
109
109
  if (index !== getTransparencyIndex(null)) {
110
110
  return { r, g, b, a: 255 };
111
111
  }
112
- return { r, g, b, a: avgAlpha ? ~~((r + g + b) / 3) : 0 };
112
+ return { r, g, b, a: avgAlpha ? Math.trunc((r + g + b) / 3) : 0 };
113
113
  };
114
114
  const image = (() => {
115
115
  try {
@@ -179,9 +179,9 @@
179
179
  frame.bitmap = await createImageBitmap(image);
180
180
  }
181
181
  else {
182
- let code = 0, size = minCodeSize + 1, pos = 0, pixelPos = -4, exit = false;
182
+ let code = 0, size = minCodeSize + 1, pos = 0, pixelPos = -4;
183
183
  const dic = [[0]];
184
- while (!exit) {
184
+ for (;;) {
185
185
  const last = code;
186
186
  code = readBits(pos, size);
187
187
  pos += size;
@@ -194,7 +194,6 @@
194
194
  }
195
195
  else {
196
196
  if (code === clearCode + 1) {
197
- exit = true;
198
197
  break;
199
198
  }
200
199
  if (code >= dic.length) {
@@ -205,7 +204,8 @@
205
204
  }
206
205
  for (const item of dic[code]) {
207
206
  const { r, g, b, a } = getColor(item);
208
- image.data.set([r, g, b, a], (pixelPos += 4));
207
+ image.data.set([r, g, b, a], pixelPos);
208
+ pixelPos += 4;
209
209
  }
210
210
  if (dic.length >= 1 << size && size < 0xc) {
211
211
  size++;
@@ -242,8 +242,7 @@
242
242
  return NaN;
243
243
  }
244
244
  async function decodeGIF(gifURL, progressCallback, avgAlpha) {
245
- if (!avgAlpha)
246
- avgAlpha = false;
245
+ avgAlpha ??= false;
247
246
  const res = await fetch(gifURL);
248
247
  if (!res.ok && res.status === 404) {
249
248
  throw new EvalError("file not found");
@@ -346,7 +345,7 @@
346
345
  }
347
346
  catch (error) {
348
347
  if (error instanceof EvalError) {
349
- throw new Error(`error while parsing frame ${frameIndex} "${error.message}"`);
348
+ throw new Error(`error while parsing frame ${frameIndex.toString()} "${error.message}"`);
350
349
  }
351
350
  throw error;
352
351
  }
@@ -363,14 +362,10 @@
363
362
  offscreenContext.imageSmoothingQuality = "low";
364
363
  offscreenContext.imageSmoothingEnabled = false;
365
364
  offscreenContext.clearRect(origin.x, origin.y, offscreenCanvas.width, offscreenCanvas.height);
366
- if (particle.gifLoopCount === undefined) {
367
- particle.gifLoopCount = image.gifLoopCount ?? defaultLoopCount;
368
- }
365
+ particle.gifLoopCount ??= image.gifLoopCount ?? defaultLoopCount;
369
366
  let frameIndex = particle.gifFrame ?? defaultFrame;
370
367
  const pos = { x: -image.gifData.width * half, y: -image.gifData.height * half }, frame = image.gifData.frames[frameIndex];
371
- if (particle.gifTime === undefined) {
372
- particle.gifTime = initialTime;
373
- }
368
+ particle.gifTime ??= initialTime;
374
369
  if (!frame.bitmap) {
375
370
  return;
376
371
  }
@@ -432,7 +427,7 @@
432
427
  image.loading = true;
433
428
  try {
434
429
  image.gifData = await decodeGIF(image.source);
435
- image.gifLoopCount = getGIFLoopAmount(image.gifData) ?? defaultLoopCount;
430
+ image.gifLoopCount = getGIFLoopAmount(image.gifData);
436
431
  if (!image.gifLoopCount) {
437
432
  image.gifLoopCount = Infinity;
438
433
  }
@@ -4,13 +4,12 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "@tsparticles/engine", "./Utils.js", "./GifUtils/Utils.js"], factory);
7
+ define(["require", "exports", "./Utils.js", "./GifUtils/Utils.js"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.ImageDrawer = void 0;
13
- const engine_1 = require("@tsparticles/engine");
14
13
  const Utils_js_1 = require("./Utils.js");
15
14
  const Utils_js_2 = require("./GifUtils/Utils.js");
16
15
  const double = 2, defaultAlpha = 1, sides = 12, defaultRatio = 1;
@@ -19,21 +18,19 @@
19
18
  this.validTypes = ["image", "images"];
20
19
  this.loadImageShape = async (imageShape) => {
21
20
  if (!this._engine.loadImage) {
22
- throw new Error(`${engine_1.errorPrefix} image shape not initialized`);
21
+ throw new Error(`Image shape not initialized`);
23
22
  }
24
23
  await this._engine.loadImage({
25
24
  gif: imageShape.gif,
26
25
  name: imageShape.name,
27
- replaceColor: imageShape.replaceColor ?? false,
26
+ replaceColor: imageShape.replaceColor,
28
27
  src: imageShape.src,
29
28
  });
30
29
  };
31
30
  this._engine = engine;
32
31
  }
33
32
  addImage(image) {
34
- if (!this._engine.images) {
35
- this._engine.images = [];
36
- }
33
+ this._engine.images ??= [];
37
34
  this._engine.images.push(image);
38
35
  }
39
36
  draw(data) {
@@ -70,9 +67,7 @@
70
67
  if (particle.shape !== "image" && particle.shape !== "images") {
71
68
  return;
72
69
  }
73
- if (!this._engine.images) {
74
- this._engine.images = [];
75
- }
70
+ this._engine.images ??= [];
76
71
  const imageData = particle.shapeData;
77
72
  if (!imageData) {
78
73
  return;
@@ -88,9 +83,7 @@
88
83
  if (particle.shape !== "image" && particle.shape !== "images") {
89
84
  return;
90
85
  }
91
- if (!this._engine.images) {
92
- this._engine.images = [];
93
- }
86
+ this._engine.images ??= [];
94
87
  const images = this._engine.images, imageData = particle.shapeData;
95
88
  if (!imageData) {
96
89
  return;
@@ -99,7 +92,7 @@
99
92
  if (!image) {
100
93
  return;
101
94
  }
102
- const replaceColor = imageData.replaceColor ?? image.replaceColor;
95
+ const replaceColor = imageData.replaceColor;
103
96
  if (image.loading) {
104
97
  setTimeout(() => {
105
98
  this.particleInit(container, particle);
@@ -109,7 +102,7 @@
109
102
  void (async () => {
110
103
  let imageRes;
111
104
  if (image.svgData && color) {
112
- imageRes = await (0, Utils_js_1.replaceImageColor)(image, imageData, color, particle);
105
+ imageRes = await (0, Utils_js_1.replaceImageColor)(image, imageData, color, particle, container.hdr);
113
106
  }
114
107
  else {
115
108
  imageRes = {
@@ -12,9 +12,8 @@
12
12
  exports.ImagePreloaderPlugin = void 0;
13
13
  const Preload_js_1 = require("./Options/Classes/Preload.js");
14
14
  class ImagePreloaderPlugin {
15
- constructor(engine) {
15
+ constructor() {
16
16
  this.id = "imagePreloader";
17
- this._engine = engine;
18
17
  }
19
18
  async getPlugin() {
20
19
  await Promise.resolve();
@@ -24,9 +23,7 @@
24
23
  if (!source?.preload) {
25
24
  return;
26
25
  }
27
- if (!options.preload) {
28
- options.preload = [];
29
- }
26
+ options.preload ??= [];
30
27
  const preloadOptions = options.preload;
31
28
  for (const item of source.preload) {
32
29
  const existing = preloadOptions.find(t => t.name === item.name || t.src === item.src);
package/umd/Utils.js CHANGED
@@ -15,12 +15,12 @@
15
15
  const engine_1 = require("@tsparticles/engine");
16
16
  const stringStart = 0, defaultOpacity = 1;
17
17
  const currentColorRegex = /(#(?:[0-9a-f]{2}){2,4}|(#[0-9a-f]{3})|(rgb|hsl)a?\((-?\d+%?[,\s]+){2,3}\s*[\d.]+%?\))|currentcolor/gi;
18
- function replaceColorSvg(imageShape, color, opacity) {
18
+ function replaceColorSvg(imageShape, color, opacity, hdr = false) {
19
19
  const { svgData } = imageShape;
20
20
  if (!svgData) {
21
21
  return "";
22
22
  }
23
- const colorStyle = (0, engine_1.getStyleFromHsl)(color, opacity);
23
+ const colorStyle = (0, engine_1.getStyleFromHsl)(color, hdr, opacity);
24
24
  if (svgData.includes("fill")) {
25
25
  return svgData.replace(currentColorRegex, () => colorStyle);
26
26
  }
@@ -40,7 +40,7 @@
40
40
  image.element = undefined;
41
41
  image.error = true;
42
42
  image.loading = false;
43
- (0, engine_1.getLogger)().error(`${engine_1.errorPrefix} loading image: ${image.source}`);
43
+ (0, engine_1.getLogger)().error(`Error loading image: ${image.source}`);
44
44
  resolve();
45
45
  });
46
46
  img.src = image.source;
@@ -54,7 +54,7 @@
54
54
  image.loading = true;
55
55
  const response = await fetch(image.source);
56
56
  if (!response.ok) {
57
- (0, engine_1.getLogger)().error(`${engine_1.errorPrefix} Image not found`);
57
+ (0, engine_1.getLogger)().error("Image not found");
58
58
  image.error = true;
59
59
  }
60
60
  else {
@@ -62,8 +62,8 @@
62
62
  }
63
63
  image.loading = false;
64
64
  }
65
- function replaceImageColor(image, imageData, color, particle) {
66
- const svgColoredData = replaceColorSvg(image, color, particle.opacity?.value ?? defaultOpacity), imageRes = {
65
+ function replaceImageColor(image, imageData, color, particle, hdr = false) {
66
+ const svgColoredData = replaceColorSvg(image, color, particle.opacity?.value ?? defaultOpacity, hdr), imageRes = {
67
67
  color,
68
68
  gif: imageData.gif,
69
69
  data: {
@@ -76,15 +76,15 @@
76
76
  source: imageData.src,
77
77
  };
78
78
  return new Promise(resolve => {
79
- const svg = new Blob([svgColoredData], { type: "image/svg+xml" }), domUrl = URL || window.URL || window.webkitURL || window, url = domUrl.createObjectURL(svg), img = new Image();
79
+ const svg = new Blob([svgColoredData], { type: "image/svg+xml" }), url = URL.createObjectURL(svg), img = new Image();
80
80
  img.addEventListener("load", () => {
81
81
  imageRes.loaded = true;
82
82
  imageRes.element = img;
83
83
  resolve(imageRes);
84
- domUrl.revokeObjectURL(url);
84
+ URL.revokeObjectURL(url);
85
85
  });
86
86
  const errorHandler = async () => {
87
- domUrl.revokeObjectURL(url);
87
+ URL.revokeObjectURL(url);
88
88
  const img2 = {
89
89
  ...image,
90
90
  error: false,
package/umd/index.js CHANGED
@@ -1,20 +1,49 @@
1
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
2
+ if (k2 === undefined) k2 = k;
3
+ var desc = Object.getOwnPropertyDescriptor(m, k);
4
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
5
+ desc = { enumerable: true, get: function() { return m[k]; } };
6
+ }
7
+ Object.defineProperty(o, k2, desc);
8
+ }) : (function(o, m, k, k2) {
9
+ if (k2 === undefined) k2 = k;
10
+ o[k2] = m[k];
11
+ }));
12
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
13
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
14
+ }) : function(o, v) {
15
+ o["default"] = v;
16
+ });
17
+ var __importStar = (this && this.__importStar) || (function () {
18
+ var ownKeys = function(o) {
19
+ ownKeys = Object.getOwnPropertyNames || function (o) {
20
+ var ar = [];
21
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
22
+ return ar;
23
+ };
24
+ return ownKeys(o);
25
+ };
26
+ return function (mod) {
27
+ if (mod && mod.__esModule) return mod;
28
+ var result = {};
29
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
30
+ __setModuleDefault(result, mod);
31
+ return result;
32
+ };
33
+ })();
1
34
  (function (factory) {
2
35
  if (typeof module === "object" && typeof module.exports === "object") {
3
36
  var v = factory(require, exports);
4
37
  if (v !== undefined) module.exports = v;
5
38
  }
6
39
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "./Utils.js", "./ImageDrawer.js", "./ImagePreloader.js", "@tsparticles/engine", "./GifUtils/Utils.js"], factory);
40
+ define(["require", "exports"], factory);
8
41
  }
9
42
  })(function (require, exports) {
10
43
  "use strict";
44
+ var __syncRequire = typeof module === "object" && typeof module.exports === "object";
11
45
  Object.defineProperty(exports, "__esModule", { value: true });
12
46
  exports.loadImageShape = loadImageShape;
13
- const Utils_js_1 = require("./Utils.js");
14
- const ImageDrawer_js_1 = require("./ImageDrawer.js");
15
- const ImagePreloader_js_1 = require("./ImagePreloader.js");
16
- const engine_1 = require("@tsparticles/engine");
17
- const Utils_js_2 = require("./GifUtils/Utils.js");
18
47
  const extLength = 3;
19
48
  function addLoadImageToEngine(engine) {
20
49
  if (engine.loadImage) {
@@ -22,17 +51,15 @@
22
51
  }
23
52
  engine.loadImage = async (data) => {
24
53
  if (!data.name && !data.src) {
25
- throw new Error(`${engine_1.errorPrefix} no image source provided`);
54
+ throw new Error("No image source provided");
26
55
  }
27
- if (!engine.images) {
28
- engine.images = [];
29
- }
30
- if (engine.images.find((t) => t.name === data.name || t.source === data.src)) {
56
+ engine.images ??= [];
57
+ if (engine.images.some((t) => t.name === data.name || t.source === data.src)) {
31
58
  return;
32
59
  }
33
60
  try {
34
61
  const image = {
35
- gif: data.gif ?? false,
62
+ gif: data.gif,
36
63
  name: data.name ?? data.src,
37
64
  source: data.src,
38
65
  type: data.src.substring(data.src.length - extLength),
@@ -44,23 +71,32 @@
44
71
  engine.images.push(image);
45
72
  let imageFunc;
46
73
  if (data.gif) {
47
- imageFunc = Utils_js_2.loadGifImage;
74
+ const { loadGifImage } = await (__syncRequire ? Promise.resolve().then(() => __importStar(require("./GifUtils/Utils.js"))) : new Promise((resolve_1, reject_1) => { require(["./GifUtils/Utils.js"], resolve_1, reject_1); }).then(__importStar));
75
+ imageFunc = loadGifImage;
76
+ }
77
+ else if (data.replaceColor) {
78
+ const { downloadSvgImage } = await (__syncRequire ? Promise.resolve().then(() => __importStar(require("./Utils.js"))) : new Promise((resolve_2, reject_2) => { require(["./Utils.js"], resolve_2, reject_2); }).then(__importStar));
79
+ imageFunc = downloadSvgImage;
48
80
  }
49
81
  else {
50
- imageFunc = data.replaceColor ? Utils_js_1.downloadSvgImage : Utils_js_1.loadImage;
82
+ const { loadImage } = await (__syncRequire ? Promise.resolve().then(() => __importStar(require("./Utils.js"))) : new Promise((resolve_3, reject_3) => { require(["./Utils.js"], resolve_3, reject_3); }).then(__importStar));
83
+ imageFunc = loadImage;
51
84
  }
52
85
  await imageFunc(image);
53
86
  }
54
87
  catch {
55
- throw new Error(`${engine_1.errorPrefix} ${data.name ?? data.src} not found`);
88
+ throw new Error(`${data.name ?? data.src} not found`);
56
89
  }
57
90
  };
58
91
  }
59
- async function loadImageShape(engine, refresh = true) {
60
- engine.checkVersion("3.9.1");
61
- addLoadImageToEngine(engine);
62
- const preloader = new ImagePreloader_js_1.ImagePreloaderPlugin(engine);
63
- await engine.addPlugin(preloader, refresh);
64
- await engine.addShape(new ImageDrawer_js_1.ImageDrawer(engine), refresh);
92
+ function loadImageShape(engine) {
93
+ engine.checkVersion("4.0.0-alpha.0");
94
+ engine.register(async (e) => {
95
+ const { ImageDrawer } = await (__syncRequire ? Promise.resolve().then(() => __importStar(require("./ImageDrawer.js"))) : new Promise((resolve_4, reject_4) => { require(["./ImageDrawer.js"], resolve_4, reject_4); }).then(__importStar)), { ImagePreloaderPlugin } = await (__syncRequire ? Promise.resolve().then(() => __importStar(require("./ImagePreloader.js"))) : new Promise((resolve_5, reject_5) => { require(["./ImagePreloader.js"], resolve_5, reject_5); }).then(__importStar));
96
+ addLoadImageToEngine(e);
97
+ const preloader = new ImagePreloaderPlugin();
98
+ e.addPlugin(preloader);
99
+ e.addShape(new ImageDrawer(e));
100
+ });
65
101
  }
66
102
  });