Skip to content

Commit

Permalink
build: working on fixing warped links
Browse files Browse the repository at this point in the history
  • Loading branch information
matteobruni committed Dec 3, 2022
1 parent 5c88649 commit 0d9bd93
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 97 deletions.
12 changes: 9 additions & 3 deletions engine/src/Options/Classes/Options.ts
Expand Up @@ -228,11 +228,17 @@ export class Options implements IOptions, IOptionLoader<IOptions> {

if (data.themes !== undefined) {
for (const theme of data.themes) {
const optTheme = new Theme();
const existingTheme = this.themes.find((t) => t.name === theme.name);

optTheme.load(theme);
if (!existingTheme) {
const optTheme = new Theme();

this.themes.push(optTheme);
optTheme.load(theme);

this.themes.push(optTheme);
} else {
existingTheme.load(theme);
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions interactions/particles/links/src/LinkInstance.ts
Expand Up @@ -41,12 +41,12 @@ function setLinkFrequency(particles: LinkParticle[], dictionary: Map<string, num
}

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

constructor(private readonly container: LinkContainer) {
container.offsets = getOffsets(container.canvas.size);

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

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

particleCreated(particle: LinkParticle): void {
Expand Down Expand Up @@ -263,10 +263,10 @@ export class LinkInstance implements IContainerPlugin {
}

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

private getTriangleFrequency(p1: LinkParticle, p2: LinkParticle, p3: LinkParticle): number {
return setLinkFrequency([p1, p2, p3], this._freqs.triangles);
return setLinkFrequency([p1, p2, p3], this._frequencies.triangles);
}
}
63 changes: 26 additions & 37 deletions interactions/particles/links/src/Utils.ts
Expand Up @@ -115,43 +115,32 @@ export function getIntermediatePoints(
return [];
}

for (const innerOffset of offsets) {
const px = { x: innerOffset.x, y: m * innerOffset.x + q },
py = {
x: Number.isFinite(m) ? innerOffset.y - q / m : q,
y: Number.isFinite(m) ? innerOffset.y : offset.y,
};

if (isPointBetweenPoints(px, beginPos, endPos)) {
const db = getDistance(beginPos, px),
de = getDistance(endPos, px);

const xi = offset.x - px.x,
yi = m * xi + q;

if (db <= de) {
pi1 = { x: xi, y: yi };
pi2 = px;
} else {
pi1 = px;
pi2 = { x: xi, y: yi };
}
} else if (isPointBetweenPoints(py, beginPos, endPos)) {
const db = getDistance(beginPos, py),
de = getDistance(endPos, py),
yi = offset.y - py.y,
xi = (yi - q) / m;

if (yi >= 0 && xi >= 0) {
if (db <= de) {
pi1 = { x: xi, y: yi };
pi2 = py;
} else {
pi1 = py;
pi2 = { x: xi, y: yi };
}
}
}
if (beginPos.x > canvasSize.width) {
pi1 = { x: canvasSize.width, y: m * canvasSize.width + q };
pi2 = { x: 0, y: q };
} else if (beginPos.x < 0) {
pi1 = { x: 0, y: q };
pi2 = { x: canvasSize.width, y: m * canvasSize.width + q };
} else if (beginPos.y > canvasSize.height) {
pi1 = { x: (canvasSize.height - q) / m, y: canvasSize.height };
pi2 = { x: -q / m, y: 0 };
} else if (beginPos.y < 0) {
pi1 = { x: -q / m, y: 0 };
pi2 = { x: (canvasSize.height - q) / m, y: canvasSize.height };
}

if (endPos.x > canvasSize.width) {
pi1 = { x: canvasSize.width, y: m * canvasSize.width + q };
pi2 = { x: 0, y: q };
} else if (endPos.x < 0) {
pi1 = { x: 0, y: q };
pi2 = { x: canvasSize.width, y: m * canvasSize.width + q };
} else if (endPos.y > canvasSize.height) {
pi1 = { x: (canvasSize.height - q) / m, y: canvasSize.height };
pi2 = { x: -q / m, y: 0 };
} else if (endPos.y < 0) {
pi1 = { x: -q / m, y: 0 };
pi2 = { x: (canvasSize.height - q) / m, y: canvasSize.height };
}

if (pi1 && pi2) {
Expand Down
6 changes: 5 additions & 1 deletion interactions/particles/links/tests/Fixture/utils.ts
@@ -1,4 +1,4 @@
import { ICoordinates, IDimension } from "tsparticles-engine";
import { /*getDistance,*/ ICoordinates, IDimension } from "tsparticles-engine";
import { getLinkPoints } from "../../src/Utils";
import { expect } from "chai";

Expand All @@ -24,6 +24,10 @@ export function checkIntermediatePointsTests(
expect(point).to.be.not.empty;

if (midPoints.length) {
//const midDistance = getDistance(point.begin, point.end);

//expect(midDistance).to.be.greaterThanOrEqual(0).and.lessThan(distance);

expect(
midPoints.find(
t =>
Expand Down

0 comments on commit 0d9bd93

Please sign in to comment.