@tsparticles/interaction-particles-collisions 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,70 @@
1
+ [![banner](https://particles.js.org/images/banner2.png)](https://particles.js.org)
2
+
3
+ # tsParticles Particles Collisions Interaction
4
+
5
+ [![jsDelivr](https://data.jsdelivr.com/v1/package/npm/tsparticles-interaction-particles-collisions/badge)](https://www.jsdelivr.com/package/npm/tsparticles-interaction-particles-collisions)
6
+ [![npmjs](https://badge.fury.io/js/tsparticles-interaction-particles-collisions.svg)](https://www.npmjs.com/package/tsparticles-interaction-particles-collisions)
7
+ [![npmjs](https://img.shields.io/npm/dt/tsparticles-interaction-particles-collisions)](https://www.npmjs.com/package/tsparticles-interaction-particles-collisions) [![GitHub Sponsors](https://img.shields.io/github/sponsors/matteobruni)](https://github.com/sponsors/matteobruni)
8
+
9
+ [tsParticles](https://github.com/matteobruni/tsparticles) interaction plugin for collisions effect between particles.
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.interaction.particles.collisions.min.js` file will export the function to load the
18
+ interaction
19
+ plugin:
20
+
21
+ ```javascript
22
+ loadParticlesCollisionsInteraction;
23
+ ```
24
+
25
+ ### Usage
26
+
27
+ Once the scripts are loaded you can set up `tsParticles` and the interaction plugin like this:
28
+
29
+ ```javascript
30
+ loadParticlesCollisionsInteraction(tsParticles);
31
+
32
+ tsParticles.load({
33
+ id: "tsparticles",
34
+ options: {
35
+ /* options */
36
+ },
37
+ });
38
+ ```
39
+
40
+ ### ESM / CommonJS
41
+
42
+ This package is compatible also with ES or CommonJS modules, firstly this needs to be installed, like this:
43
+
44
+ ```shell
45
+ $ npm install tsparticles-interaction-particles-collisions
46
+ ```
47
+
48
+ or
49
+
50
+ ```shell
51
+ $ yarn add tsparticles-interaction-particles-collisions
52
+ ```
53
+
54
+ Then you need to import it in the app, like this:
55
+
56
+ ```javascript
57
+ const { tsParticles } = require("tsparticles-engine");
58
+ const { loadParticlesCollisionsInteraction } = require("tsparticles-interaction-particles-collisions");
59
+
60
+ loadParticlesCollisionsInteraction(tsParticles);
61
+ ```
62
+
63
+ or
64
+
65
+ ```javascript
66
+ import { tsParticles } from "tsparticles-engine";
67
+ import { loadParticlesCollisionsInteraction } from "tsparticles-interaction-particles-collisions";
68
+
69
+ loadParticlesCollisionsInteraction(tsParticles);
70
+ ```
@@ -0,0 +1,27 @@
1
+ import { clamp } from "@tsparticles/engine";
2
+ function updateAbsorb(p1, r1, p2, r2, delta, pixelRatio) {
3
+ const factor = clamp((p1.options.collisions.absorb.speed * delta.factor) / 10, 0, r2);
4
+ p1.size.value += factor / 2;
5
+ p2.size.value -= factor;
6
+ if (r2 <= pixelRatio) {
7
+ p2.size.value = 0;
8
+ p2.destroy();
9
+ }
10
+ }
11
+ export function absorb(p1, p2, delta, pixelRatio) {
12
+ const r1 = p1.getRadius(), r2 = p2.getRadius();
13
+ if (r1 === undefined && r2 !== undefined) {
14
+ p1.destroy();
15
+ }
16
+ else if (r1 !== undefined && r2 === undefined) {
17
+ p2.destroy();
18
+ }
19
+ else if (r1 !== undefined && r2 !== undefined) {
20
+ if (r1 >= r2) {
21
+ updateAbsorb(p1, r1, p2, r2, delta, pixelRatio);
22
+ }
23
+ else {
24
+ updateAbsorb(p2, r2, p1, r1, delta, pixelRatio);
25
+ }
26
+ }
27
+ }
@@ -0,0 +1,4 @@
1
+ import { circleBounce, circleBounceDataFromParticle } from "@tsparticles/engine";
2
+ export function bounce(p1, p2) {
3
+ circleBounce(circleBounceDataFromParticle(p1), circleBounceDataFromParticle(p2));
4
+ }
@@ -0,0 +1,37 @@
1
+ import { ParticlesInteractorBase, getDistance } from "@tsparticles/engine";
2
+ import { resolveCollision } from "./ResolveCollision";
3
+ export class Collider extends ParticlesInteractorBase {
4
+ constructor(container) {
5
+ super(container);
6
+ }
7
+ clear() {
8
+ }
9
+ init() {
10
+ }
11
+ async interact(p1, delta) {
12
+ const container = this.container, pos1 = p1.getPosition(), radius1 = p1.getRadius(), query = container.particles.quadTree.queryCircle(pos1, radius1 * 2);
13
+ for (const p2 of query) {
14
+ if (p1 === p2 ||
15
+ !p2.options.collisions.enable ||
16
+ p1.options.collisions.mode !== p2.options.collisions.mode ||
17
+ p2.destroyed ||
18
+ p2.spawning) {
19
+ continue;
20
+ }
21
+ const pos2 = p2.getPosition(), radius2 = p2.getRadius();
22
+ if (Math.abs(Math.round(pos1.z) - Math.round(pos2.z)) > radius1 + radius2) {
23
+ continue;
24
+ }
25
+ const dist = getDistance(pos1, pos2), distP = radius1 + radius2;
26
+ if (dist > distP) {
27
+ continue;
28
+ }
29
+ resolveCollision(p1, p2, delta, container.retina.pixelRatio);
30
+ }
31
+ }
32
+ isEnabled(particle) {
33
+ return particle.options.collisions.enable;
34
+ }
35
+ reset() {
36
+ }
37
+ }
@@ -0,0 +1,16 @@
1
+ import { bounce } from "./Bounce";
2
+ export function destroy(p1, p2) {
3
+ if (!p1.unbreakable && !p2.unbreakable) {
4
+ bounce(p1, p2);
5
+ }
6
+ if (p1.getRadius() === undefined && p2.getRadius() !== undefined) {
7
+ p1.destroy();
8
+ }
9
+ else if (p1.getRadius() !== undefined && p2.getRadius() === undefined) {
10
+ p2.destroy();
11
+ }
12
+ else if (p1.getRadius() !== undefined && p2.getRadius() !== undefined) {
13
+ const deleteP = p1.getRadius() >= p2.getRadius() ? p1 : p2;
14
+ deleteP.destroy();
15
+ }
16
+ }
@@ -0,0 +1,19 @@
1
+ import { absorb } from "./Absorb";
2
+ import { bounce } from "./Bounce";
3
+ import { destroy } from "./Destroy";
4
+ export function resolveCollision(p1, p2, delta, pixelRatio) {
5
+ switch (p1.options.collisions.mode) {
6
+ case "absorb": {
7
+ absorb(p1, p2, delta, pixelRatio);
8
+ break;
9
+ }
10
+ case "bounce": {
11
+ bounce(p1, p2);
12
+ break;
13
+ }
14
+ case "destroy": {
15
+ destroy(p1, p2);
16
+ break;
17
+ }
18
+ }
19
+ }
@@ -0,0 +1,4 @@
1
+ import { Collider } from "./Collider";
2
+ export async function loadParticlesCollisionsInteraction(engine) {
3
+ await engine.addInteractor("particlesCollisions", (container) => new Collider(container));
4
+ }
package/cjs/Absorb.js ADDED
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.absorb = void 0;
4
+ const engine_1 = require("@tsparticles/engine");
5
+ function updateAbsorb(p1, r1, p2, r2, delta, pixelRatio) {
6
+ const factor = (0, engine_1.clamp)((p1.options.collisions.absorb.speed * delta.factor) / 10, 0, r2);
7
+ p1.size.value += factor / 2;
8
+ p2.size.value -= factor;
9
+ if (r2 <= pixelRatio) {
10
+ p2.size.value = 0;
11
+ p2.destroy();
12
+ }
13
+ }
14
+ function absorb(p1, p2, delta, pixelRatio) {
15
+ const r1 = p1.getRadius(), r2 = p2.getRadius();
16
+ if (r1 === undefined && r2 !== undefined) {
17
+ p1.destroy();
18
+ }
19
+ else if (r1 !== undefined && r2 === undefined) {
20
+ p2.destroy();
21
+ }
22
+ else if (r1 !== undefined && r2 !== undefined) {
23
+ if (r1 >= r2) {
24
+ updateAbsorb(p1, r1, p2, r2, delta, pixelRatio);
25
+ }
26
+ else {
27
+ updateAbsorb(p2, r2, p1, r1, delta, pixelRatio);
28
+ }
29
+ }
30
+ }
31
+ exports.absorb = absorb;
package/cjs/Bounce.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bounce = void 0;
4
+ const engine_1 = require("@tsparticles/engine");
5
+ function bounce(p1, p2) {
6
+ (0, engine_1.circleBounce)((0, engine_1.circleBounceDataFromParticle)(p1), (0, engine_1.circleBounceDataFromParticle)(p2));
7
+ }
8
+ exports.bounce = bounce;
@@ -0,0 +1,52 @@
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.Collider = void 0;
13
+ const engine_1 = require("@tsparticles/engine");
14
+ const ResolveCollision_1 = require("./ResolveCollision");
15
+ class Collider extends engine_1.ParticlesInteractorBase {
16
+ constructor(container) {
17
+ super(container);
18
+ }
19
+ clear() {
20
+ }
21
+ init() {
22
+ }
23
+ interact(p1, delta) {
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ const container = this.container, pos1 = p1.getPosition(), radius1 = p1.getRadius(), query = container.particles.quadTree.queryCircle(pos1, radius1 * 2);
26
+ for (const p2 of query) {
27
+ if (p1 === p2 ||
28
+ !p2.options.collisions.enable ||
29
+ p1.options.collisions.mode !== p2.options.collisions.mode ||
30
+ p2.destroyed ||
31
+ p2.spawning) {
32
+ continue;
33
+ }
34
+ const pos2 = p2.getPosition(), radius2 = p2.getRadius();
35
+ if (Math.abs(Math.round(pos1.z) - Math.round(pos2.z)) > radius1 + radius2) {
36
+ continue;
37
+ }
38
+ const dist = (0, engine_1.getDistance)(pos1, pos2), distP = radius1 + radius2;
39
+ if (dist > distP) {
40
+ continue;
41
+ }
42
+ (0, ResolveCollision_1.resolveCollision)(p1, p2, delta, container.retina.pixelRatio);
43
+ }
44
+ });
45
+ }
46
+ isEnabled(particle) {
47
+ return particle.options.collisions.enable;
48
+ }
49
+ reset() {
50
+ }
51
+ }
52
+ exports.Collider = Collider;
package/cjs/Destroy.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.destroy = void 0;
4
+ const Bounce_1 = require("./Bounce");
5
+ function destroy(p1, p2) {
6
+ if (!p1.unbreakable && !p2.unbreakable) {
7
+ (0, Bounce_1.bounce)(p1, p2);
8
+ }
9
+ if (p1.getRadius() === undefined && p2.getRadius() !== undefined) {
10
+ p1.destroy();
11
+ }
12
+ else if (p1.getRadius() !== undefined && p2.getRadius() === undefined) {
13
+ p2.destroy();
14
+ }
15
+ else if (p1.getRadius() !== undefined && p2.getRadius() !== undefined) {
16
+ const deleteP = p1.getRadius() >= p2.getRadius() ? p1 : p2;
17
+ deleteP.destroy();
18
+ }
19
+ }
20
+ exports.destroy = destroy;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveCollision = void 0;
4
+ const Absorb_1 = require("./Absorb");
5
+ const Bounce_1 = require("./Bounce");
6
+ const Destroy_1 = require("./Destroy");
7
+ function resolveCollision(p1, p2, delta, pixelRatio) {
8
+ switch (p1.options.collisions.mode) {
9
+ case "absorb": {
10
+ (0, Absorb_1.absorb)(p1, p2, delta, pixelRatio);
11
+ break;
12
+ }
13
+ case "bounce": {
14
+ (0, Bounce_1.bounce)(p1, p2);
15
+ break;
16
+ }
17
+ case "destroy": {
18
+ (0, Destroy_1.destroy)(p1, p2);
19
+ break;
20
+ }
21
+ }
22
+ }
23
+ exports.resolveCollision = resolveCollision;
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.loadParticlesCollisionsInteraction = void 0;
13
+ const Collider_1 = require("./Collider");
14
+ function loadParticlesCollisionsInteraction(engine) {
15
+ return __awaiter(this, void 0, void 0, function* () {
16
+ yield engine.addInteractor("particlesCollisions", (container) => new Collider_1.Collider(container));
17
+ });
18
+ }
19
+ exports.loadParticlesCollisionsInteraction = loadParticlesCollisionsInteraction;
package/esm/Absorb.js ADDED
@@ -0,0 +1,27 @@
1
+ import { clamp } from "@tsparticles/engine";
2
+ function updateAbsorb(p1, r1, p2, r2, delta, pixelRatio) {
3
+ const factor = clamp((p1.options.collisions.absorb.speed * delta.factor) / 10, 0, r2);
4
+ p1.size.value += factor / 2;
5
+ p2.size.value -= factor;
6
+ if (r2 <= pixelRatio) {
7
+ p2.size.value = 0;
8
+ p2.destroy();
9
+ }
10
+ }
11
+ export function absorb(p1, p2, delta, pixelRatio) {
12
+ const r1 = p1.getRadius(), r2 = p2.getRadius();
13
+ if (r1 === undefined && r2 !== undefined) {
14
+ p1.destroy();
15
+ }
16
+ else if (r1 !== undefined && r2 === undefined) {
17
+ p2.destroy();
18
+ }
19
+ else if (r1 !== undefined && r2 !== undefined) {
20
+ if (r1 >= r2) {
21
+ updateAbsorb(p1, r1, p2, r2, delta, pixelRatio);
22
+ }
23
+ else {
24
+ updateAbsorb(p2, r2, p1, r1, delta, pixelRatio);
25
+ }
26
+ }
27
+ }
package/esm/Bounce.js ADDED
@@ -0,0 +1,4 @@
1
+ import { circleBounce, circleBounceDataFromParticle } from "@tsparticles/engine";
2
+ export function bounce(p1, p2) {
3
+ circleBounce(circleBounceDataFromParticle(p1), circleBounceDataFromParticle(p2));
4
+ }
@@ -0,0 +1,37 @@
1
+ import { ParticlesInteractorBase, getDistance } from "@tsparticles/engine";
2
+ import { resolveCollision } from "./ResolveCollision";
3
+ export class Collider extends ParticlesInteractorBase {
4
+ constructor(container) {
5
+ super(container);
6
+ }
7
+ clear() {
8
+ }
9
+ init() {
10
+ }
11
+ async interact(p1, delta) {
12
+ const container = this.container, pos1 = p1.getPosition(), radius1 = p1.getRadius(), query = container.particles.quadTree.queryCircle(pos1, radius1 * 2);
13
+ for (const p2 of query) {
14
+ if (p1 === p2 ||
15
+ !p2.options.collisions.enable ||
16
+ p1.options.collisions.mode !== p2.options.collisions.mode ||
17
+ p2.destroyed ||
18
+ p2.spawning) {
19
+ continue;
20
+ }
21
+ const pos2 = p2.getPosition(), radius2 = p2.getRadius();
22
+ if (Math.abs(Math.round(pos1.z) - Math.round(pos2.z)) > radius1 + radius2) {
23
+ continue;
24
+ }
25
+ const dist = getDistance(pos1, pos2), distP = radius1 + radius2;
26
+ if (dist > distP) {
27
+ continue;
28
+ }
29
+ resolveCollision(p1, p2, delta, container.retina.pixelRatio);
30
+ }
31
+ }
32
+ isEnabled(particle) {
33
+ return particle.options.collisions.enable;
34
+ }
35
+ reset() {
36
+ }
37
+ }
package/esm/Destroy.js ADDED
@@ -0,0 +1,16 @@
1
+ import { bounce } from "./Bounce";
2
+ export function destroy(p1, p2) {
3
+ if (!p1.unbreakable && !p2.unbreakable) {
4
+ bounce(p1, p2);
5
+ }
6
+ if (p1.getRadius() === undefined && p2.getRadius() !== undefined) {
7
+ p1.destroy();
8
+ }
9
+ else if (p1.getRadius() !== undefined && p2.getRadius() === undefined) {
10
+ p2.destroy();
11
+ }
12
+ else if (p1.getRadius() !== undefined && p2.getRadius() !== undefined) {
13
+ const deleteP = p1.getRadius() >= p2.getRadius() ? p1 : p2;
14
+ deleteP.destroy();
15
+ }
16
+ }
@@ -0,0 +1,19 @@
1
+ import { absorb } from "./Absorb";
2
+ import { bounce } from "./Bounce";
3
+ import { destroy } from "./Destroy";
4
+ export function resolveCollision(p1, p2, delta, pixelRatio) {
5
+ switch (p1.options.collisions.mode) {
6
+ case "absorb": {
7
+ absorb(p1, p2, delta, pixelRatio);
8
+ break;
9
+ }
10
+ case "bounce": {
11
+ bounce(p1, p2);
12
+ break;
13
+ }
14
+ case "destroy": {
15
+ destroy(p1, p2);
16
+ break;
17
+ }
18
+ }
19
+ }
package/esm/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import { Collider } from "./Collider";
2
+ export async function loadParticlesCollisionsInteraction(engine) {
3
+ await engine.addInteractor("particlesCollisions", (container) => new Collider(container));
4
+ }
package/package.json ADDED
@@ -0,0 +1,82 @@
1
+ {
2
+ "name": "@tsparticles/interaction-particles-collisions",
3
+ "version": "3.0.0-alpha.0",
4
+ "description": "tsParticles collisions particles interaction",
5
+ "homepage": "https://particles.js.org",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/matteobruni/tsparticles.git",
9
+ "directory": "interactions/particles/collisions"
10
+ },
11
+ "keywords": [
12
+ "front-end",
13
+ "frontend",
14
+ "tsparticles",
15
+ "particles.js",
16
+ "particlesjs",
17
+ "particles",
18
+ "particle",
19
+ "canvas",
20
+ "jsparticles",
21
+ "xparticles",
22
+ "particles-js",
23
+ "particles-bg",
24
+ "particles-bg-vue",
25
+ "particles-ts",
26
+ "particles.ts",
27
+ "react-particles-js",
28
+ "react-particles.js",
29
+ "react-particles",
30
+ "react",
31
+ "reactjs",
32
+ "vue-particles",
33
+ "ngx-particles",
34
+ "angular-particles",
35
+ "particleground",
36
+ "vue",
37
+ "vuejs",
38
+ "preact",
39
+ "preactjs",
40
+ "jquery",
41
+ "angularjs",
42
+ "angular",
43
+ "typescript",
44
+ "javascript",
45
+ "animation",
46
+ "web",
47
+ "html5",
48
+ "web-design",
49
+ "webdesign",
50
+ "css",
51
+ "html",
52
+ "css3",
53
+ "animated",
54
+ "background",
55
+ "confetti",
56
+ "canvas",
57
+ "fireworks",
58
+ "fireworks-js",
59
+ "confetti-js",
60
+ "confettijs",
61
+ "fireworksjs",
62
+ "canvas-confetti",
63
+ "@tsparticles/plugin",
64
+ "@tsparticles/interaction"
65
+ ],
66
+ "author": "Matteo Bruni <matteo.bruni@me.com>",
67
+ "license": "MIT",
68
+ "bugs": {
69
+ "url": "https://github.com/matteobruni/tsparticles/issues"
70
+ },
71
+ "main": "cjs/index.js",
72
+ "jsdelivr": "tsparticles.interaction.particles.collisions.min.js",
73
+ "unpkg": "tsparticles.interaction.particles.collisions.min.js",
74
+ "module": "esm/index.js",
75
+ "types": "types/index.d.ts",
76
+ "publishConfig": {
77
+ "access": "public"
78
+ },
79
+ "dependencies": {
80
+ "@tsparticles/engine": "^3.0.0-alpha.0"
81
+ }
82
+ }