@tsparticles/shape-text 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 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
+ [![banner](https://particles.js.org/images/banner2.png)](https://particles.js.org)
2
+
3
+ # tsParticles Text Shape
4
+
5
+ [![jsDelivr](https://data.jsdelivr.com/v1/package/npm/tsparticles-shape-text/badge)](https://www.jsdelivr.com/package/npm/tsparticles-shape-text)
6
+ [![npmjs](https://badge.fury.io/js/tsparticles-shape-text.svg)](https://www.npmjs.com/package/tsparticles-shape-text)
7
+ [![npmjs](https://img.shields.io/npm/dt/tsparticles-shape-text)](https://www.npmjs.com/package/tsparticles-shape-text) [![GitHub Sponsors](https://img.shields.io/github/sponsors/matteobruni)](https://github.com/sponsors/matteobruni)
8
+
9
+ [tsParticles](https://github.com/matteobruni/tsparticles) additional text 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.text.min.js` file will export the function to load the shape:
18
+
19
+ ```javascript
20
+ loadTextShape;
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 loadTextShape();
30
+
31
+ await tsParticles.load({
32
+ id: "tsparticles",
33
+ options: {
34
+ /* options */
35
+ /* here you can use particles.shape.type: "text" */
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-text
47
+ ```
48
+
49
+ or
50
+
51
+ ```shell
52
+ $ yarn add tsparticles-shape-text
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 { loadTextShape } = require("tsparticles-shape-text");
60
+
61
+ loadTextShape(tsParticles);
62
+ ```
63
+
64
+ or
65
+
66
+ ```javascript
67
+ import { tsParticles } from "tsparticles-engine";
68
+ import { loadTextShape } from "tsparticles-shape-text";
69
+
70
+ loadTextShape(tsParticles);
71
+ ```
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,48 @@
1
+ import { executeOnSingleOrMultiple, isInArray, itemFromSingleOrMultiple, loadFont } from "@tsparticles/engine";
2
+ export const validTypes = ["text", "character", "char"];
3
+ export class TextDrawer {
4
+ draw(context, particle, radius, opacity) {
5
+ var _a, _b, _c;
6
+ const character = particle.shapeData;
7
+ if (character === undefined) {
8
+ return;
9
+ }
10
+ const textData = character.value;
11
+ if (textData === undefined) {
12
+ return;
13
+ }
14
+ const textParticle = particle;
15
+ if (textParticle.text === undefined) {
16
+ textParticle.text = itemFromSingleOrMultiple(textData, particle.randomIndexData);
17
+ }
18
+ const text = textParticle.text, style = (_a = character.style) !== null && _a !== void 0 ? _a : "", weight = (_b = character.weight) !== null && _b !== void 0 ? _b : "400", size = Math.round(radius) * 2, font = (_c = character.font) !== null && _c !== void 0 ? _c : "Verdana", fill = particle.fill, offsetX = (text.length * radius) / 2;
19
+ context.font = `${style} ${weight} ${size}px "${font}"`;
20
+ const pos = {
21
+ x: -offsetX,
22
+ y: radius / 2,
23
+ };
24
+ context.globalAlpha = opacity;
25
+ if (fill) {
26
+ context.fillText(text, pos.x, pos.y);
27
+ }
28
+ else {
29
+ context.strokeText(text, pos.x, pos.y);
30
+ }
31
+ context.globalAlpha = 1;
32
+ }
33
+ getSidesCount() {
34
+ return 12;
35
+ }
36
+ async init(container) {
37
+ const options = container.actualOptions;
38
+ if (validTypes.find((t) => isInArray(t, options.particles.shape.type))) {
39
+ const shapeOptions = validTypes
40
+ .map((t) => options.particles.shape.options[t])
41
+ .find((t) => !!t), promises = [];
42
+ executeOnSingleOrMultiple(shapeOptions, (shape) => {
43
+ promises.push(loadFont(shape.font, shape.weight));
44
+ });
45
+ await Promise.all(promises);
46
+ }
47
+ }
48
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ import { TextDrawer, validTypes } from "./TextDrawer";
2
+ export async function loadTextShape(engine) {
3
+ await engine.addShape(validTypes, new TextDrawer());
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,63 @@
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.TextDrawer = exports.validTypes = void 0;
13
+ const engine_1 = require("@tsparticles/engine");
14
+ exports.validTypes = ["text", "character", "char"];
15
+ class TextDrawer {
16
+ draw(context, particle, radius, opacity) {
17
+ var _a, _b, _c;
18
+ const character = particle.shapeData;
19
+ if (character === undefined) {
20
+ return;
21
+ }
22
+ const textData = character.value;
23
+ if (textData === undefined) {
24
+ return;
25
+ }
26
+ const textParticle = particle;
27
+ if (textParticle.text === undefined) {
28
+ textParticle.text = (0, engine_1.itemFromSingleOrMultiple)(textData, particle.randomIndexData);
29
+ }
30
+ const text = textParticle.text, style = (_a = character.style) !== null && _a !== void 0 ? _a : "", weight = (_b = character.weight) !== null && _b !== void 0 ? _b : "400", size = Math.round(radius) * 2, font = (_c = character.font) !== null && _c !== void 0 ? _c : "Verdana", fill = particle.fill, offsetX = (text.length * radius) / 2;
31
+ context.font = `${style} ${weight} ${size}px "${font}"`;
32
+ const pos = {
33
+ x: -offsetX,
34
+ y: radius / 2,
35
+ };
36
+ context.globalAlpha = opacity;
37
+ if (fill) {
38
+ context.fillText(text, pos.x, pos.y);
39
+ }
40
+ else {
41
+ context.strokeText(text, pos.x, pos.y);
42
+ }
43
+ context.globalAlpha = 1;
44
+ }
45
+ getSidesCount() {
46
+ return 12;
47
+ }
48
+ init(container) {
49
+ return __awaiter(this, void 0, void 0, function* () {
50
+ const options = container.actualOptions;
51
+ if (exports.validTypes.find((t) => (0, engine_1.isInArray)(t, options.particles.shape.type))) {
52
+ const shapeOptions = exports.validTypes
53
+ .map((t) => options.particles.shape.options[t])
54
+ .find((t) => !!t), promises = [];
55
+ (0, engine_1.executeOnSingleOrMultiple)(shapeOptions, (shape) => {
56
+ promises.push((0, engine_1.loadFont)(shape.font, shape.weight));
57
+ });
58
+ yield Promise.all(promises);
59
+ }
60
+ });
61
+ }
62
+ }
63
+ exports.TextDrawer = TextDrawer;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
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.loadTextShape = void 0;
13
+ const TextDrawer_1 = require("./TextDrawer");
14
+ function loadTextShape(engine) {
15
+ return __awaiter(this, void 0, void 0, function* () {
16
+ yield engine.addShape(TextDrawer_1.validTypes, new TextDrawer_1.TextDrawer());
17
+ });
18
+ }
19
+ exports.loadTextShape = loadTextShape;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,48 @@
1
+ import { executeOnSingleOrMultiple, isInArray, itemFromSingleOrMultiple, loadFont } from "@tsparticles/engine";
2
+ export const validTypes = ["text", "character", "char"];
3
+ export class TextDrawer {
4
+ draw(context, particle, radius, opacity) {
5
+ var _a, _b, _c;
6
+ const character = particle.shapeData;
7
+ if (character === undefined) {
8
+ return;
9
+ }
10
+ const textData = character.value;
11
+ if (textData === undefined) {
12
+ return;
13
+ }
14
+ const textParticle = particle;
15
+ if (textParticle.text === undefined) {
16
+ textParticle.text = itemFromSingleOrMultiple(textData, particle.randomIndexData);
17
+ }
18
+ const text = textParticle.text, style = (_a = character.style) !== null && _a !== void 0 ? _a : "", weight = (_b = character.weight) !== null && _b !== void 0 ? _b : "400", size = Math.round(radius) * 2, font = (_c = character.font) !== null && _c !== void 0 ? _c : "Verdana", fill = particle.fill, offsetX = (text.length * radius) / 2;
19
+ context.font = `${style} ${weight} ${size}px "${font}"`;
20
+ const pos = {
21
+ x: -offsetX,
22
+ y: radius / 2,
23
+ };
24
+ context.globalAlpha = opacity;
25
+ if (fill) {
26
+ context.fillText(text, pos.x, pos.y);
27
+ }
28
+ else {
29
+ context.strokeText(text, pos.x, pos.y);
30
+ }
31
+ context.globalAlpha = 1;
32
+ }
33
+ getSidesCount() {
34
+ return 12;
35
+ }
36
+ async init(container) {
37
+ const options = container.actualOptions;
38
+ if (validTypes.find((t) => isInArray(t, options.particles.shape.type))) {
39
+ const shapeOptions = validTypes
40
+ .map((t) => options.particles.shape.options[t])
41
+ .find((t) => !!t), promises = [];
42
+ executeOnSingleOrMultiple(shapeOptions, (shape) => {
43
+ promises.push(loadFont(shape.font, shape.weight));
44
+ });
45
+ await Promise.all(promises);
46
+ }
47
+ }
48
+ }
@@ -0,0 +1 @@
1
+ export {};
package/esm/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import { TextDrawer, validTypes } from "./TextDrawer";
2
+ export async function loadTextShape(engine) {
3
+ await engine.addShape(validTypes, new TextDrawer());
4
+ }
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@tsparticles/shape-text",
3
+ "version": "3.0.0-alpha.0",
4
+ "description": "tsParticles text shape",
5
+ "homepage": "https://particles.js.org",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/matteobruni/tsparticles.git",
9
+ "directory": "shapes/text"
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.text.min.js",
45
+ "unpkg": "tsparticles.shape.text.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
+ }