@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.
package/esm/Utils.js ADDED
@@ -0,0 +1,78 @@
1
+ import { getStyleFromHsl } from "@tsparticles/engine";
2
+ const currentColorRegex = /(#(?:[0-9a-f]{2}){2,4}|(#[0-9a-f]{3})|(rgb|hsl)a?\((-?\d+%?[,\s]+){2,3}\s*[\d.]+%?\))|currentcolor/gi;
3
+ function replaceColorSvg(imageShape, color, opacity) {
4
+ const { svgData } = imageShape;
5
+ if (!svgData) {
6
+ return "";
7
+ }
8
+ const colorStyle = getStyleFromHsl(color, opacity);
9
+ if (svgData.includes("fill")) {
10
+ return svgData.replace(currentColorRegex, () => colorStyle);
11
+ }
12
+ const preFillIndex = svgData.indexOf(">");
13
+ return `${svgData.substring(0, preFillIndex)} fill="${colorStyle}"${svgData.substring(preFillIndex)}`;
14
+ }
15
+ export async function loadImage(image) {
16
+ return new Promise((resolve) => {
17
+ image.loading = true;
18
+ const img = new Image();
19
+ image.element = img;
20
+ img.addEventListener("load", () => {
21
+ image.loading = false;
22
+ resolve();
23
+ });
24
+ img.addEventListener("error", () => {
25
+ image.element = undefined;
26
+ image.error = true;
27
+ image.loading = false;
28
+ console.error(`Error tsParticles - loading image: ${image.source}`);
29
+ resolve();
30
+ });
31
+ img.src = image.source;
32
+ });
33
+ }
34
+ export async function downloadSvgImage(image) {
35
+ if (image.type !== "svg") {
36
+ await loadImage(image);
37
+ return;
38
+ }
39
+ image.loading = true;
40
+ const response = await fetch(image.source);
41
+ if (!response.ok) {
42
+ console.error("Error tsParticles - Image not found");
43
+ image.error = true;
44
+ }
45
+ if (!image.error) {
46
+ image.svgData = await response.text();
47
+ }
48
+ image.loading = false;
49
+ }
50
+ export function replaceImageColor(image, imageData, color, particle) {
51
+ var _a, _b, _c;
52
+ const svgColoredData = replaceColorSvg(image, color, (_b = (_a = particle.opacity) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : 1), imageRes = {
53
+ color,
54
+ data: Object.assign(Object.assign({}, image), { svgData: svgColoredData }),
55
+ loaded: false,
56
+ ratio: imageData.width / imageData.height,
57
+ replaceColor: (_c = imageData.replaceColor) !== null && _c !== void 0 ? _c : imageData.replace_color,
58
+ source: imageData.src,
59
+ };
60
+ return new Promise((resolve) => {
61
+ const svg = new Blob([svgColoredData], { type: "image/svg+xml" }), domUrl = URL || window.URL || window.webkitURL || window, url = domUrl.createObjectURL(svg), img = new Image();
62
+ img.addEventListener("load", () => {
63
+ imageRes.loaded = true;
64
+ imageRes.element = img;
65
+ resolve(imageRes);
66
+ domUrl.revokeObjectURL(url);
67
+ });
68
+ img.addEventListener("error", async () => {
69
+ domUrl.revokeObjectURL(url);
70
+ const img2 = Object.assign(Object.assign({}, image), { error: false, loading: true });
71
+ await loadImage(img2);
72
+ imageRes.loaded = true;
73
+ imageRes.element = img2.element;
74
+ resolve(imageRes);
75
+ });
76
+ img.src = url;
77
+ });
78
+ }
package/esm/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import { ImageDrawer } from "./ImageDrawer";
2
+ export async function loadImageShape(engine) {
3
+ await engine.addShape(["image", "images"], new ImageDrawer());
4
+ }
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@tsparticles/shape-image",
3
+ "version": "3.0.0-alpha.0",
4
+ "description": "tsParticles image shape",
5
+ "homepage": "https://particles.js.org",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/matteobruni/tsparticles.git",
9
+ "directory": "shapes/image"
10
+ },
11
+ "keywords": [
12
+ "front-end",
13
+ "frontend",
14
+ "tsparticles",
15
+ "particles",
16
+ "particle",
17
+ "canvas",
18
+ "jsparticles",
19
+ "xparticles",
20
+ "particles-js",
21
+ "particles.js",
22
+ "particles-ts",
23
+ "particles.ts",
24
+ "typescript",
25
+ "javascript",
26
+ "animation",
27
+ "web",
28
+ "html5",
29
+ "web-design",
30
+ "webdesign",
31
+ "css",
32
+ "html",
33
+ "css3",
34
+ "animated",
35
+ "background",
36
+ "@tsparticles/shape"
37
+ ],
38
+ "author": "Matteo Bruni <matteo.bruni@me.com>",
39
+ "license": "MIT",
40
+ "bugs": {
41
+ "url": "https://github.com/matteobruni/tsparticles/issues"
42
+ },
43
+ "main": "cjs/index.js",
44
+ "jsdelivr": "tsparticles.shape.image.min.js",
45
+ "unpkg": "tsparticles.shape.image.min.js",
46
+ "module": "esm/index.js",
47
+ "types": "types/index.d.ts",
48
+ "publishConfig": {
49
+ "access": "public"
50
+ },
51
+ "dependencies": {
52
+ "@tsparticles/engine": "^3.0.0-alpha.0"
53
+ }
54
+ }