Skip to content

Commit

Permalink
build: restored links warp to previous version, delayed to future rel…
Browse files Browse the repository at this point in the history
…ease
  • Loading branch information
matteobruni committed Dec 5, 2022
1 parent 9b13c43 commit b3d5854
Show file tree
Hide file tree
Showing 17 changed files with 163 additions and 715 deletions.
8 changes: 0 additions & 8 deletions interactions/particles/links/.eslintrc.test.js

This file was deleted.

11 changes: 0 additions & 11 deletions interactions/particles/links/.mocharc.json

This file was deleted.

22 changes: 3 additions & 19 deletions interactions/particles/links/package.json
Expand Up @@ -4,20 +4,11 @@
"description": "tsParticles links particles interaction",
"homepage": "https://particles.js.org",
"scripts": {
"prettify:ci:tests": "prettier --check ./tests",
"prettify:ci:schema": "prettier --check ./schema/options.schema.json",
"prettify:tests": "prettier --write ./tests",
"lint:ci:tests": "echo eslint --no-eslintrc --config .eslintrc.test.js --ext .js,.jsx,.ts,.tsx tests",
"lint:tests": "echo eslint --no-eslintrc --config .eslintrc.test.js --ext .js,.jsx,.ts,.tsx --fix tests",
"slimbuild": "tsparticles-build",
"slimbuild:ci": "tsparticles-build --ci",
"build": "pnpm run slimbuild&& pnpm run lint:tests && pnpm run prettify:tests && pnpm run test",
"build:ci": "pnpm run slimbuild:ci&& pnpm run lint:ci:tests && pnpm run prettify:ci:tests && pnpm run test",
"build": "tsparticles-build",
"build:ci": "tsparticles-build --ci",
"build:docs": "rimraf -f docs && npx typedoc",
"preversion": "pnpm run test",
"version": "tsparticles-build -d && git add package.dist.json",
"prepack": "pnpm run build",
"test": "nyc mocha"
"prepack": "pnpm run build"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -103,24 +94,17 @@
"@tsparticles/eslint-config": "^1.3.2",
"@tsparticles/prettier-config": "^1.3.1",
"@tsparticles/tsconfig": "^1.4.0",
"@types/chai": "^4.3.4",
"@types/mocha": "^10.0.0",
"@types/webpack-env": "^1.18.0",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.43.0",
"babel-loader": "^9.1.0",
"browserslist": "^4.21.4",
"chai": "^4.3.7",
"copyfiles": "^2.4.1",
"eslint": "^8.27.0",
"eslint-config-prettier": "^8.5.0",
"mocha": "^10.1.0",
"nyc": "^15.1.0",
"prettier": "^2.7.1",
"rimraf": "^3.0.2",
"source-map-support": "^0.5.21",
"terser-webpack-plugin": "^5.3.6",
"ts-node": "^10.9.1",
"typescript": "^4.8.4",
"webpack": "^5.75.0",
"webpack-bundle-analyzer": "^4.7.0",
Expand Down
57 changes: 33 additions & 24 deletions interactions/particles/links/src/CircleWarp.ts
@@ -1,6 +1,5 @@
import { Circle, Rectangle } from "tsparticles-engine";
import type { ICoordinates, IDimension, Range } from "tsparticles-engine";
import { offsetsFactors } from "./Utils";

/**
* @category Utils
Expand Down Expand Up @@ -29,22 +28,30 @@ export class CircleWarp extends Circle {
return true;
}

for (const offsetFactor of offsetsFactors) {
const offset = {
x: offsetFactor.x * this.canvasSize.width,
y: offsetFactor.y * this.canvasSize.height,
},
pos = {
x: point.x + offset.x,
y: point.y + offset.y,
};
const posNE = {
x: point.x - this.canvasSize.width,
y: point.y,
};

if (super.contains(pos)) {
return true;
}
if (super.contains(posNE)) {
return true;
}

return false;
const posSE = {
x: point.x - this.canvasSize.width,
y: point.y - this.canvasSize.height,
};

if (super.contains(posSE)) {
return true;
}

const posSW = {
x: point.x,
y: point.y - this.canvasSize.height,
};

return super.contains(posSW);
}

/**
Expand All @@ -57,19 +64,21 @@ export class CircleWarp extends Circle {
return true;
}

const newPos = {
x: range.position.x - this.canvasSize.width,
y: range.position.y - this.canvasSize.height,
};
const rect = range as Rectangle,
circle = range as Circle,
newPos = {
x: range.position.x - this.canvasSize.width,
y: range.position.y - this.canvasSize.height,
};

if (range instanceof Circle) {
const circle = new Circle(newPos.x, newPos.y, range.radius * 2);
if (circle.radius !== undefined) {
const biggerCircle = new Circle(newPos.x, newPos.y, circle.radius * 2);

return super.intersects(circle);
} else if (range instanceof Rectangle) {
const rect = new Rectangle(newPos.x, newPos.y, range.size.width * 2, range.size.height * 2);
return super.intersects(biggerCircle);
} else if (rect.size !== undefined) {
const rectSW = new Rectangle(newPos.x, newPos.y, rect.size.width * 2, rect.size.height * 2);

return super.intersects(rect);
return super.intersects(rectSW);
}

return false;
Expand Down
8 changes: 8 additions & 0 deletions interactions/particles/links/src/ILink.ts
Expand Up @@ -7,3 +7,11 @@ export interface ILink {
destination: LinkParticle;
opacity: number;
}

/**
* @category Interfaces
*/
export interface ILinkTriangle {
opacity: number;
vertices: LinkParticle[];
}
16 changes: 6 additions & 10 deletions interactions/particles/links/src/LinkInstance.ts
Expand Up @@ -41,10 +41,10 @@ function setLinkFrequency(particles: LinkParticle[], dictionary: Map<string, num
}

export class LinkInstance implements IContainerPlugin {
private readonly _frequencies: IParticlesFrequencies;
private readonly _freqs: IParticlesFrequencies;

constructor(private readonly container: LinkContainer) {
this._frequencies = {
this._freqs = {
links: new Map<string, number>(),
triangles: new Map<string, number>(),
};
Expand All @@ -71,8 +71,8 @@ export class LinkInstance implements IContainerPlugin {
}

async init(): Promise<void> {
this._frequencies.links = new Map<string, number>();
this._frequencies.triangles = new Map<string, number>();
this._freqs.links = new Map<string, number>();
this._freqs.triangles = new Map<string, number>();
}

particleCreated(particle: LinkParticle): void {
Expand All @@ -92,10 +92,6 @@ export class LinkInstance implements IContainerPlugin {
particle.links = [];
}

resize(): void {
// do nothing
}

private drawLinkLine(p1: LinkParticle, link: ILink): void {
const container = this.container,
options = container.actualOptions,
Expand Down Expand Up @@ -260,10 +256,10 @@ export class LinkInstance implements IContainerPlugin {
}

private getLinkFrequency(p1: LinkParticle, p2: LinkParticle): number {
return setLinkFrequency([p1, p2], this._frequencies.links);
return setLinkFrequency([p1, p2], this._freqs.links);
}

private getTriangleFrequency(p1: LinkParticle, p2: LinkParticle, p3: LinkParticle): number {
return setLinkFrequency([p1, p2, p3], this._frequencies.triangles);
return setLinkFrequency([p1, p2, p3], this._freqs.triangles);
}
}
80 changes: 36 additions & 44 deletions interactions/particles/links/src/Linker.ts
@@ -1,62 +1,58 @@
import { Circle, ParticlesInteractorBase, getDistance, getLinkRandomColor } from "tsparticles-engine";
import type { Engine, ICoordinates, IDimension, IRgb, RecursivePartial } from "tsparticles-engine";
import type { ICoordinates, IDimension, IRgb, RecursivePartial } from "tsparticles-engine";
import { CircleWarp } from "./CircleWarp";
import type { IParticlesLinkOptions } from "./Options/Interfaces/IParticlesLinkOptions";
import type { LinkContainer } from "./LinkContainer";
import type { LinkParticle } from "./LinkParticle";
import { Links } from "./Options/Classes/Links";
import type { ParticlesLinkOptions } from "./Options/Classes/ParticlesLinkOptions";
import { offsetsFactors } from "./Utils";

export function findLink(p1: LinkParticle, p2: LinkParticle): boolean {
if (!p1.links || !p2.links) {
return false;
}

return !!p1.links.find((t) => t.destination === p2) || !!p2.links.find((t) => t.destination === p1);
}

export function getLinkDistance(
function getLinkDistance(
pos1: ICoordinates,
pos2: ICoordinates,
optDistance: number,
canvasSize: IDimension,
warp: boolean
): number | undefined {
const distance = getDistance(pos1, pos2);
): number {
let distance = getDistance(pos1, pos2);

if (distance <= optDistance) {
if (!warp || distance <= optDistance) {
return distance;
}

if (!warp) {
return;
const pos2NE = {
x: pos2.x - canvasSize.width,
y: pos2.y,
};

distance = getDistance(pos1, pos2NE);

if (distance <= optDistance) {
return distance;
}

for (const offsetFactor of offsetsFactors) {
const offset = { x: offsetFactor.x * canvasSize.width, y: offsetFactor.y * canvasSize.height },
pos1o = {
x: pos1.x + offset.x,
y: pos1.y + offset.y,
},
pos2o = {
x: pos2.x + offset.x,
y: pos2.y + offset.y,
},
d1 = getDistance(pos1o, pos2),
d2 = getDistance(pos1, pos2o);

if (d1 <= optDistance) {
return d1;
}
const pos2SE = {
x: pos2.x - canvasSize.width,
y: pos2.y - canvasSize.height,
};

if (d2 <= optDistance) {
return d2;
}
distance = getDistance(pos1, pos2SE);

if (distance <= optDistance) {
return distance;
}

const pos2SW = {
x: pos2.x,
y: pos2.y - canvasSize.height,
};

distance = getDistance(pos1, pos2SW);

return distance;
}

class Linker extends ParticlesInteractorBase {
export class Linker extends ParticlesInteractorBase {
linkContainer: LinkContainer;

constructor(container: LinkContainer) {
Expand Down Expand Up @@ -102,14 +98,14 @@ class Linker extends ParticlesInteractorBase {
const linkOpt2 = p2.options.links;

if (
!linkOpt2 ||
p1 === p2 ||
!linkOpt2.enable ||
!linkOpt2?.enable ||
linkOpt1.id !== linkOpt2.id ||
p2.spawning ||
p2.destroyed ||
!p2.links ||
findLink(p1, p2)
p1.links.map((t) => t.destination).indexOf(p2) !== -1 ||
p2.links.map((t) => t.destination).indexOf(p1) !== -1
) {
continue;
}
Expand All @@ -122,7 +118,7 @@ class Linker extends ParticlesInteractorBase {

const distance = getLinkDistance(pos1, pos2, optDistance, canvasSize, warp && linkOpt2.warp);

if (distance === undefined || distance > optDistance) {
if (distance > optDistance) {
return;
}

Expand Down Expand Up @@ -187,7 +183,3 @@ class Linker extends ParticlesInteractorBase {
}
}
}

export async function loadInteraction(engine: Engine): Promise<void> {
await engine.addInteractor("particlesLinks", (container) => new Linker(container as LinkContainer));
}

0 comments on commit b3d5854

Please sign in to comment.