mirror of
https://github.com/tcsenpai/obsidiangarden_netlify.git
synced 2025-06-07 05:05:20 +00:00
implement focus on hovered and neigbors
This commit is contained in:
parent
616a2a9a0f
commit
cfe44308d0
@ -51,6 +51,12 @@
|
|||||||
var Graph;
|
var Graph;
|
||||||
|
|
||||||
function renderGraph(graphData, id, width, height, delay) {
|
function renderGraph(graphData, id, width, height, delay) {
|
||||||
|
|
||||||
|
const highlightNodes = new Set();
|
||||||
|
let hoverNode = null;
|
||||||
|
const color = getCssVar("--text-accent");
|
||||||
|
const mutedColor = getCssVar("--text-faint");
|
||||||
|
|
||||||
let Graph = ForceGraph()
|
let Graph = ForceGraph()
|
||||||
(document.getElementById(id))
|
(document.getElementById(id))
|
||||||
.graphData(graphData)
|
.graphData(graphData)
|
||||||
@ -63,15 +69,34 @@
|
|||||||
.height(height)
|
.height(height)
|
||||||
.linkDirectionalArrowLength(2)
|
.linkDirectionalArrowLength(2)
|
||||||
.linkDirectionalArrowRelPos(0.5)
|
.linkDirectionalArrowRelPos(0.5)
|
||||||
.linkColor(() => getCssVar("--text-muted") || getCssVar("--text-normal"))
|
.autoPauseRedraw(false)
|
||||||
|
.linkColor((link) => {
|
||||||
|
if (hoverNode == null) {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
if (link.source.id == hoverNode.id || link.target.id == hoverNode.id) {
|
||||||
|
return color;
|
||||||
|
} else {
|
||||||
|
return mutedColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
.nodeCanvasObject((node, ctx) => {
|
.nodeCanvasObject((node, ctx) => {
|
||||||
const color = getCssVar("--text-accent");
|
|
||||||
const numberOfNeighbours = (node.neighbors && node.neighbors.length) || 2;
|
const numberOfNeighbours = (node.neighbors && node.neighbors.length) || 2;
|
||||||
const nodeR = Math.min(7, Math.max(numberOfNeighbours / 2, 2));
|
const nodeR = Math.min(7, Math.max(numberOfNeighbours / 2, 2));
|
||||||
|
|
||||||
ctx.beginPath();
|
ctx.beginPath();
|
||||||
ctx.arc(node.x, node.y, nodeR, 0, 2 * Math.PI, false);
|
ctx.arc(node.x, node.y, nodeR, 0, 2 * Math.PI, false);
|
||||||
|
if (hoverNode == null) {
|
||||||
ctx.fillStyle = color;
|
ctx.fillStyle = color;
|
||||||
|
} else {
|
||||||
|
if (node == hoverNode || highlightNodes.has(node.url)) {
|
||||||
|
ctx.fillStyle = color;
|
||||||
|
} else {
|
||||||
|
ctx.fillStyle = mutedColor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ctx.fill();
|
ctx.fill();
|
||||||
|
|
||||||
if (node.current) {
|
if (node.current) {
|
||||||
@ -92,8 +117,17 @@
|
|||||||
})
|
})
|
||||||
.onNodeClick(node => {
|
.onNodeClick(node => {
|
||||||
window.location = node.url;
|
window.location = node.url;
|
||||||
|
})
|
||||||
|
.onNodeHover(node => {
|
||||||
|
highlightNodes.clear();
|
||||||
|
if (node) {
|
||||||
|
highlightNodes.add(node);
|
||||||
|
node.neighbors.forEach(neighbor => highlightNodes.add(neighbor));
|
||||||
|
}
|
||||||
|
hoverNode = node || null;
|
||||||
|
|
||||||
});
|
});
|
||||||
if (delay != null && graphData.nodes.length > 2) {
|
if (delay != null && graphData.nodes.length > 4) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
Graph.zoomToFit(5, 75);
|
Graph.zoomToFit(5, 75);
|
||||||
}, delay);
|
}, delay);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user