@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/LICENSE +21 -0
- package/README.md +71 -0
- package/browser/IImageShape.js +1 -0
- package/browser/ImageDrawer.js +120 -0
- package/browser/Utils.js +78 -0
- package/browser/index.js +4 -0
- package/cjs/IImageShape.js +2 -0
- package/cjs/ImageDrawer.js +135 -0
- package/cjs/Utils.js +97 -0
- package/cjs/index.js +19 -0
- package/esm/IImageShape.js +1 -0
- package/esm/ImageDrawer.js +120 -0
- package/esm/Utils.js +78 -0
- package/esm/index.js +4 -0
- package/package.json +54 -0
- package/report.html +39 -0
- package/tsparticles.shape.image.js +330 -0
- package/tsparticles.shape.image.min.js +2 -0
- package/tsparticles.shape.image.min.js.LICENSE.txt +8 -0
- package/types/IImageShape.d.ts +8 -0
- package/types/ImageDrawer.d.ts +14 -0
- package/types/Utils.d.ts +30 -0
- package/types/index.d.ts +2 -0
- package/umd/IImageShape.js +12 -0
- package/umd/ImageDrawer.js +134 -0
- package/umd/Utils.js +94 -0
- package/umd/index.js +18 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Matteo Bruni
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
[](https://particles.js.org)
|
|
2
|
+
|
|
3
|
+
# tsParticles Image Shape
|
|
4
|
+
|
|
5
|
+
[](https://www.jsdelivr.com/package/npm/tsparticles-shape-image)
|
|
6
|
+
[](https://www.npmjs.com/package/tsparticles-shape-image)
|
|
7
|
+
[](https://www.npmjs.com/package/tsparticles-shape-image) [](https://github.com/sponsors/matteobruni)
|
|
8
|
+
|
|
9
|
+
[tsParticles](https://github.com/matteobruni/tsparticles) additional image shape.
|
|
10
|
+
|
|
11
|
+
## How to use it
|
|
12
|
+
|
|
13
|
+
### CDN / Vanilla JS / jQuery
|
|
14
|
+
|
|
15
|
+
The CDN/Vanilla version JS has one required file in vanilla configuration:
|
|
16
|
+
|
|
17
|
+
Including the `tsparticles.shape.image.min.js` file will export the function to load the shape:
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
loadImageShape;
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Usage
|
|
24
|
+
|
|
25
|
+
Once the scripts are loaded you can set up `tsParticles` and the shape like this:
|
|
26
|
+
|
|
27
|
+
```javascript
|
|
28
|
+
(async () => {
|
|
29
|
+
await loadImageShape();
|
|
30
|
+
|
|
31
|
+
await tsParticles.load({
|
|
32
|
+
id: "tsparticles",
|
|
33
|
+
options: {
|
|
34
|
+
/* options */
|
|
35
|
+
/* here you can use particles.shape.type: "image" */
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
})();
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### ESM / CommonJS
|
|
42
|
+
|
|
43
|
+
This package is compatible also with ES or CommonJS modules, firstly this needs to be installed, like this:
|
|
44
|
+
|
|
45
|
+
```shell
|
|
46
|
+
$ npm install tsparticles-shape-image
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
or
|
|
50
|
+
|
|
51
|
+
```shell
|
|
52
|
+
$ yarn add tsparticles-shape-image
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Then you need to import it in the app, like this:
|
|
56
|
+
|
|
57
|
+
```javascript
|
|
58
|
+
const { tsParticles } = require("tsparticles-engine");
|
|
59
|
+
const { loadImageShape } = require("tsparticles-shape-image");
|
|
60
|
+
|
|
61
|
+
loadImageShape(tsParticles);
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
or
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
import { tsParticles } from "tsparticles-engine";
|
|
68
|
+
import { loadImageShape } from "tsparticles-shape-image";
|
|
69
|
+
|
|
70
|
+
loadImageShape(tsParticles);
|
|
71
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { downloadSvgImage, loadImage, replaceImageColor } from "./Utils";
|
|
2
|
+
export class ImageDrawer {
|
|
3
|
+
constructor() {
|
|
4
|
+
this._images = [];
|
|
5
|
+
}
|
|
6
|
+
addImage(container, image) {
|
|
7
|
+
const containerImages = this.getImages(container);
|
|
8
|
+
containerImages === null || containerImages === void 0 ? void 0 : containerImages.images.push(image);
|
|
9
|
+
}
|
|
10
|
+
destroy() {
|
|
11
|
+
this._images = [];
|
|
12
|
+
}
|
|
13
|
+
draw(context, particle, radius, opacity) {
|
|
14
|
+
var _a;
|
|
15
|
+
const image = particle.image, element = image === null || image === void 0 ? void 0 : image.element;
|
|
16
|
+
if (!element) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const ratio = (_a = image === null || image === void 0 ? void 0 : image.ratio) !== null && _a !== void 0 ? _a : 1, pos = {
|
|
20
|
+
x: -radius,
|
|
21
|
+
y: -radius,
|
|
22
|
+
};
|
|
23
|
+
context.globalAlpha = opacity;
|
|
24
|
+
context.drawImage(element, pos.x, pos.y, radius * 2, (radius * 2) / ratio);
|
|
25
|
+
context.globalAlpha = 1;
|
|
26
|
+
}
|
|
27
|
+
getImages(container) {
|
|
28
|
+
const containerImages = this._images.find((t) => t.id === container.id);
|
|
29
|
+
if (!containerImages) {
|
|
30
|
+
this._images.push({
|
|
31
|
+
id: container.id,
|
|
32
|
+
images: [],
|
|
33
|
+
});
|
|
34
|
+
return this.getImages(container);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
return containerImages;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
getSidesCount() {
|
|
41
|
+
return 12;
|
|
42
|
+
}
|
|
43
|
+
loadShape(particle) {
|
|
44
|
+
if (particle.shape !== "image" && particle.shape !== "images") {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const container = particle.container, images = this.getImages(container).images, imageData = particle.shapeData, image = images.find((t) => t.source === imageData.src);
|
|
48
|
+
if (!image) {
|
|
49
|
+
this.loadImageShape(container, imageData).then(() => {
|
|
50
|
+
this.loadShape(particle);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
particleInit(container, particle) {
|
|
55
|
+
var _a;
|
|
56
|
+
if (particle.shape !== "image" && particle.shape !== "images") {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
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);
|
|
60
|
+
if (!image) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (image.loading) {
|
|
64
|
+
setTimeout(() => {
|
|
65
|
+
this.particleInit(container, particle);
|
|
66
|
+
});
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
(async () => {
|
|
70
|
+
var _a, _b;
|
|
71
|
+
let imageRes;
|
|
72
|
+
if (image.svgData && color) {
|
|
73
|
+
imageRes = await replaceImageColor(image, imageData, color, particle);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
imageRes = {
|
|
77
|
+
color,
|
|
78
|
+
data: image,
|
|
79
|
+
element: image.element,
|
|
80
|
+
loaded: true,
|
|
81
|
+
ratio: imageData.width / imageData.height,
|
|
82
|
+
replaceColor: replaceColor,
|
|
83
|
+
source: imageData.src,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
if (!imageRes.ratio) {
|
|
87
|
+
imageRes.ratio = 1;
|
|
88
|
+
}
|
|
89
|
+
const fill = (_a = imageData.fill) !== null && _a !== void 0 ? _a : particle.fill, close = (_b = imageData.close) !== null && _b !== void 0 ? _b : particle.close, imageShape = {
|
|
90
|
+
image: imageRes,
|
|
91
|
+
fill,
|
|
92
|
+
close,
|
|
93
|
+
};
|
|
94
|
+
particle.image = imageShape.image;
|
|
95
|
+
particle.fill = imageShape.fill;
|
|
96
|
+
particle.close = imageShape.close;
|
|
97
|
+
})();
|
|
98
|
+
}
|
|
99
|
+
async loadImageShape(container, imageShape) {
|
|
100
|
+
var _a;
|
|
101
|
+
const source = imageShape.src;
|
|
102
|
+
if (!source) {
|
|
103
|
+
throw new Error("Error tsParticles - No image.src");
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
const image = {
|
|
107
|
+
source: source,
|
|
108
|
+
type: source.substring(source.length - 3),
|
|
109
|
+
error: false,
|
|
110
|
+
loading: true,
|
|
111
|
+
};
|
|
112
|
+
this.addImage(container, image);
|
|
113
|
+
const imageFunc = ((_a = imageShape.replaceColor) !== null && _a !== void 0 ? _a : imageShape.replace_color) ? downloadSvgImage : loadImage;
|
|
114
|
+
await imageFunc(image);
|
|
115
|
+
}
|
|
116
|
+
catch (_b) {
|
|
117
|
+
throw new Error(`tsParticles error - ${imageShape.src} not found`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
package/browser/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/browser/index.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
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
|
+
(() => __awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
var _b, _c;
|
|
83
|
+
let imageRes;
|
|
84
|
+
if (image.svgData && color) {
|
|
85
|
+
imageRes = yield (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 = (_b = imageData.fill) !== null && _b !== void 0 ? _b : particle.fill, close = (_c = imageData.close) !== null && _c !== void 0 ? _c : 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
|
+
loadImageShape(container, imageShape) {
|
|
112
|
+
var _a;
|
|
113
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
114
|
+
const source = imageShape.src;
|
|
115
|
+
if (!source) {
|
|
116
|
+
throw new Error("Error tsParticles - No image.src");
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
const image = {
|
|
120
|
+
source: source,
|
|
121
|
+
type: source.substring(source.length - 3),
|
|
122
|
+
error: false,
|
|
123
|
+
loading: true,
|
|
124
|
+
};
|
|
125
|
+
this.addImage(container, image);
|
|
126
|
+
const imageFunc = ((_a = imageShape.replaceColor) !== null && _a !== void 0 ? _a : imageShape.replace_color) ? Utils_1.downloadSvgImage : Utils_1.loadImage;
|
|
127
|
+
yield imageFunc(image);
|
|
128
|
+
}
|
|
129
|
+
catch (_b) {
|
|
130
|
+
throw new Error(`tsParticles error - ${imageShape.src} not found`);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
exports.ImageDrawer = ImageDrawer;
|
package/cjs/Utils.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
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
|
+
function loadImage(image) {
|
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
return new Promise((resolve) => {
|
|
30
|
+
image.loading = true;
|
|
31
|
+
const img = new Image();
|
|
32
|
+
image.element = img;
|
|
33
|
+
img.addEventListener("load", () => {
|
|
34
|
+
image.loading = false;
|
|
35
|
+
resolve();
|
|
36
|
+
});
|
|
37
|
+
img.addEventListener("error", () => {
|
|
38
|
+
image.element = undefined;
|
|
39
|
+
image.error = true;
|
|
40
|
+
image.loading = false;
|
|
41
|
+
console.error(`Error tsParticles - loading image: ${image.source}`);
|
|
42
|
+
resolve();
|
|
43
|
+
});
|
|
44
|
+
img.src = image.source;
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
exports.loadImage = loadImage;
|
|
49
|
+
function downloadSvgImage(image) {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
if (image.type !== "svg") {
|
|
52
|
+
yield loadImage(image);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
image.loading = true;
|
|
56
|
+
const response = yield fetch(image.source);
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
console.error("Error tsParticles - Image not found");
|
|
59
|
+
image.error = true;
|
|
60
|
+
}
|
|
61
|
+
if (!image.error) {
|
|
62
|
+
image.svgData = yield response.text();
|
|
63
|
+
}
|
|
64
|
+
image.loading = false;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
exports.downloadSvgImage = downloadSvgImage;
|
|
68
|
+
function replaceImageColor(image, imageData, color, particle) {
|
|
69
|
+
var _a, _b, _c;
|
|
70
|
+
const svgColoredData = replaceColorSvg(image, color, (_b = (_a = particle.opacity) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : 1), imageRes = {
|
|
71
|
+
color,
|
|
72
|
+
data: Object.assign(Object.assign({}, image), { svgData: svgColoredData }),
|
|
73
|
+
loaded: false,
|
|
74
|
+
ratio: imageData.width / imageData.height,
|
|
75
|
+
replaceColor: (_c = imageData.replaceColor) !== null && _c !== void 0 ? _c : imageData.replace_color,
|
|
76
|
+
source: imageData.src,
|
|
77
|
+
};
|
|
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();
|
|
80
|
+
img.addEventListener("load", () => {
|
|
81
|
+
imageRes.loaded = true;
|
|
82
|
+
imageRes.element = img;
|
|
83
|
+
resolve(imageRes);
|
|
84
|
+
domUrl.revokeObjectURL(url);
|
|
85
|
+
});
|
|
86
|
+
img.addEventListener("error", () => __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
domUrl.revokeObjectURL(url);
|
|
88
|
+
const img2 = Object.assign(Object.assign({}, image), { error: false, loading: true });
|
|
89
|
+
yield loadImage(img2);
|
|
90
|
+
imageRes.loaded = true;
|
|
91
|
+
imageRes.element = img2.element;
|
|
92
|
+
resolve(imageRes);
|
|
93
|
+
}));
|
|
94
|
+
img.src = url;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
exports.replaceImageColor = replaceImageColor;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.loadImageShape = void 0;
|
|
13
|
+
const ImageDrawer_1 = require("./ImageDrawer");
|
|
14
|
+
function loadImageShape(engine) {
|
|
15
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
16
|
+
yield engine.addShape(["image", "images"], new ImageDrawer_1.ImageDrawer());
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
exports.loadImageShape = loadImageShape;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { downloadSvgImage, loadImage, replaceImageColor } from "./Utils";
|
|
2
|
+
export class ImageDrawer {
|
|
3
|
+
constructor() {
|
|
4
|
+
this._images = [];
|
|
5
|
+
}
|
|
6
|
+
addImage(container, image) {
|
|
7
|
+
const containerImages = this.getImages(container);
|
|
8
|
+
containerImages === null || containerImages === void 0 ? void 0 : containerImages.images.push(image);
|
|
9
|
+
}
|
|
10
|
+
destroy() {
|
|
11
|
+
this._images = [];
|
|
12
|
+
}
|
|
13
|
+
draw(context, particle, radius, opacity) {
|
|
14
|
+
var _a;
|
|
15
|
+
const image = particle.image, element = image === null || image === void 0 ? void 0 : image.element;
|
|
16
|
+
if (!element) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const ratio = (_a = image === null || image === void 0 ? void 0 : image.ratio) !== null && _a !== void 0 ? _a : 1, pos = {
|
|
20
|
+
x: -radius,
|
|
21
|
+
y: -radius,
|
|
22
|
+
};
|
|
23
|
+
context.globalAlpha = opacity;
|
|
24
|
+
context.drawImage(element, pos.x, pos.y, radius * 2, (radius * 2) / ratio);
|
|
25
|
+
context.globalAlpha = 1;
|
|
26
|
+
}
|
|
27
|
+
getImages(container) {
|
|
28
|
+
const containerImages = this._images.find((t) => t.id === container.id);
|
|
29
|
+
if (!containerImages) {
|
|
30
|
+
this._images.push({
|
|
31
|
+
id: container.id,
|
|
32
|
+
images: [],
|
|
33
|
+
});
|
|
34
|
+
return this.getImages(container);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
return containerImages;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
getSidesCount() {
|
|
41
|
+
return 12;
|
|
42
|
+
}
|
|
43
|
+
loadShape(particle) {
|
|
44
|
+
if (particle.shape !== "image" && particle.shape !== "images") {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const container = particle.container, images = this.getImages(container).images, imageData = particle.shapeData, image = images.find((t) => t.source === imageData.src);
|
|
48
|
+
if (!image) {
|
|
49
|
+
this.loadImageShape(container, imageData).then(() => {
|
|
50
|
+
this.loadShape(particle);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
particleInit(container, particle) {
|
|
55
|
+
var _a;
|
|
56
|
+
if (particle.shape !== "image" && particle.shape !== "images") {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
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);
|
|
60
|
+
if (!image) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (image.loading) {
|
|
64
|
+
setTimeout(() => {
|
|
65
|
+
this.particleInit(container, particle);
|
|
66
|
+
});
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
(async () => {
|
|
70
|
+
var _a, _b;
|
|
71
|
+
let imageRes;
|
|
72
|
+
if (image.svgData && color) {
|
|
73
|
+
imageRes = await replaceImageColor(image, imageData, color, particle);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
imageRes = {
|
|
77
|
+
color,
|
|
78
|
+
data: image,
|
|
79
|
+
element: image.element,
|
|
80
|
+
loaded: true,
|
|
81
|
+
ratio: imageData.width / imageData.height,
|
|
82
|
+
replaceColor: replaceColor,
|
|
83
|
+
source: imageData.src,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
if (!imageRes.ratio) {
|
|
87
|
+
imageRes.ratio = 1;
|
|
88
|
+
}
|
|
89
|
+
const fill = (_a = imageData.fill) !== null && _a !== void 0 ? _a : particle.fill, close = (_b = imageData.close) !== null && _b !== void 0 ? _b : particle.close, imageShape = {
|
|
90
|
+
image: imageRes,
|
|
91
|
+
fill,
|
|
92
|
+
close,
|
|
93
|
+
};
|
|
94
|
+
particle.image = imageShape.image;
|
|
95
|
+
particle.fill = imageShape.fill;
|
|
96
|
+
particle.close = imageShape.close;
|
|
97
|
+
})();
|
|
98
|
+
}
|
|
99
|
+
async loadImageShape(container, imageShape) {
|
|
100
|
+
var _a;
|
|
101
|
+
const source = imageShape.src;
|
|
102
|
+
if (!source) {
|
|
103
|
+
throw new Error("Error tsParticles - No image.src");
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
const image = {
|
|
107
|
+
source: source,
|
|
108
|
+
type: source.substring(source.length - 3),
|
|
109
|
+
error: false,
|
|
110
|
+
loading: true,
|
|
111
|
+
};
|
|
112
|
+
this.addImage(container, image);
|
|
113
|
+
const imageFunc = ((_a = imageShape.replaceColor) !== null && _a !== void 0 ? _a : imageShape.replace_color) ? downloadSvgImage : loadImage;
|
|
114
|
+
await imageFunc(image);
|
|
115
|
+
}
|
|
116
|
+
catch (_b) {
|
|
117
|
+
throw new Error(`tsParticles error - ${imageShape.src} not found`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|