Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filtering/reconnecting #18

Merged
merged 18 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions js/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Link } from "./objects.js";

export function reconnect(
criteriaFunction,
parentLinks,
childrenLinks,
particles
) {
const filteredParticles = [];
const newParentLinks = [];
const newChildrenLinks = [];

for (const particle of particles) {
if (!criteriaFunction(particle)) {
const parentParticles = [];
const childrenParticles = [];

for (const parent of particle.parents) {
if (criteriaFunction(particles[parent])) {
parentParticles.push(parent);
}
}

for (const child of particle.children) {
if (criteriaFunction(particles[child])) {
childrenParticles.push(child);
}
}

for (const parent of parentParticles) {
for (const child of childrenParticles) {
const link = new Link(newParentLinks.length, parent, child);
newParentLinks.push(link);
}
}
} else {
filteredParticles.push(particle);

for (const parentLinkId of particle.parentLinks) {
const parentLink = parentLinks[parentLinkId];
const parent = particles[parentLink.from];
if (criteriaFunction(parent)) {
newParentLinks.push(
new Link(newParentLinks.length, parentLink.from, parentLink.to)
);
}
}

for (const childrenLinkId of particle.childrenLinks) {
const childrenLink = childrenLinks[childrenLinkId];
const child = particles[childrenLink.to];
if (criteriaFunction(child)) {
newChildrenLinks.push(
new Link(
newChildrenLinks.length,
childrenLink.from,
childrenLink.to
)
);
}
}
}
}

return [filteredParticles, newParentLinks, newChildrenLinks];
}
18 changes: 12 additions & 6 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const mouseUp = function (event) {
isDragging = false;

// console.time("drawAll");
drawAll();
drawAll(parentLinks, childrenLinks, infoBoxes);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stupid question from my side, where do these inputs come from?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry, these inputs come from main.js.

// console.timeEnd("drawAll");
};

Expand Down Expand Up @@ -81,7 +81,7 @@ const mouseMove = function (event) {
infoBox.y += dy;

// console.time("drawVisible");
drawVisible();
drawVisible(visibleParentLinks, visibleChildrenLinks, visibleBoxes);
// console.timeEnd("drawVisible");

prevMouseX = mouseX;
Expand Down Expand Up @@ -170,7 +170,11 @@ const getVisible = function () {
*/
};

const drawVisible = function () {
const drawVisible = function (
visibleParentLinks,
visibleChildrenLinks,
visibleBoxes
) {
const boundigClientRect = canvas.getBoundingClientRect();
ctx.clearRect(
0 - boundigClientRect.x,
Expand All @@ -189,7 +193,7 @@ const drawVisible = function () {
}
};

const drawAll = function () {
const drawAll = function (parentLinks, childrenLinks, infoBoxes) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// console.time("drawParentLinks");
for (const link of parentLinks) {
Expand Down Expand Up @@ -273,7 +277,7 @@ function start() {
}
}

drawAll();
drawAll(parentLinks, childrenLinks, infoBoxes);

getVisible();
}
Expand Down Expand Up @@ -338,5 +342,7 @@ document
start();
hideInputModal();
window.scroll((canvas.width - window.innerWidth) / 2, 0);
toggle.init(infoBoxes, drawAll);
toggle.init(infoBoxes, () => {
drawAll(parentLinks, childrenLinks, infoBoxes);
});
});