Skip to content

Build your personal knowledge base with Trilium Notes

feat(client): fade the note map into and out of what a hover asks of it

Elian Doran34a0c6a9f98c
diff --git a/apps/client/src/widgets/note_map/rendering.ts b/apps/client/src/widgets/note_map/rendering.ts
index a842ad21e4..58ec2cec45 100644
--- a/apps/client/src/widgets/note_map/rendering.ts
+++ b/apps/client/src/widgets/note_map/rendering.ts
@@ -2,7 +2,7 @@ import type ForceGraph from "force-graph";
import { NoteMapLinkObject, NoteMapNodeObject, NotesAndRelationsData } from "./data";
import { LinkObject, NodeObject } from "force-graph";
import { ICON_FONT_FAMILY } from "./icons";
-import { generateColorFromString, getFitPadding, isLightColor, isRootedAtCurrentNote, MapType, NoteMapWidgetMode } from "./utils";
+import { generateColorFromString, getFitPadding, isLightColor, isRootedAtCurrentNote, MapType, mixColors, NoteMapWidgetMode, withAlpha } from "./utils";
import { escapeHtml } from "../../services/utils";
import FNote from "../../entities/fnote";
@@ -18,8 +18,12 @@ const MIN_ICON_RADIUS = 7;
/** How much is left of what the pointer is not resting on, while it rests on a note of the map. */
const DIMMED_ALPHA = 0.15;
-/** The same, for a colour that can only be dimmed by being asked for with it (see {@link getLinkColor}). */
-const DIMMED_ALPHA_HEX = Math.round(DIMMED_ALPHA * 255).toString(16).padStart(2, "0");
+/** How long the map takes to answer the pointer coming to rest on a note, or leaving one. */
+const HOVER_FADE_MS = 160;
+
+/** What a relation is drawn with, and what one of the hovered note's own grows to. */
+const LINK_WIDTH = 0.4;
+const HIGHLIGHT_LINK_WIDTH = 3;
/** How large a note's label is drawn on screen where the map is neither zoomed in nor out. */
const LABEL_FONT_PX = 11;
@@ -70,19 +74,37 @@ export function setupRendering(graph: ForceGraph<NoteMapNodeObject, NoteMapLinkO
let zoomLevel: number;
/** Titles as they are drawn, cut to the width a label is allowed — see {@link getLabel}. */
const labelsByNoteId = new Map<string, string>();
- // A relation cannot be faded back the way a note is (the graph is given its colour rather than
- // drawing it), so it is asked for in a colour that is already faint. Only a plain `#rrggbb` takes
- // the two digits that say by how much; a theme whose muted colour resolves to anything else keeps
- // what it has rather than being handed something a canvas cannot read.
- const dimmedLinkColor = /^#[0-9a-f]{6}$/i.test(cssData.mutedTextColor)
- ? `${cssData.mutedTextColor}${DIMMED_ALPHA_HEX}`
- : cssData.mutedTextColor;
+ /**
+ * What the hovered note's own relations are drawn in. Remembered rather than asked of the hovered
+ * note as it is drawn, so that they have a colour to fade back from once the pointer has left.
+ */
+ let highlightColor = cssData.textColor;
+ /**
+ * Where each note and each relation stands between the two things a hover asks of it: -1 faded
+ * back behind what is hovered, 0 as the map stands with nothing hovered, 1 lit as one of its own.
+ */
+ const nodeFocus = createFade(notesAndRelations.nodes, (node) => (hoverNode && !isInHoveredNeighbourhood(node) ? -1 : 0));
+ const linkFocus = createFade<NoteMapLinkObject>(notesAndRelations.links, (link) => {
+ if (!hoverNode) {
+ return 0;
+ }
+ return highlightLinks.has(link) ? 1 : -1;
+ });
+ let fadeTimer: ReturnType<typeof setTimeout> | undefined;
+ /** A canvas of no consequence, kept for what it makes of a colour — see {@link normalizeColor}. */
+ const colorResolver = document.createElement("canvas").getContext("2d");
/**
* Takes note of what is hovered and of what the map is to show of it: the notes a relation runs
* between it and, and the relations themselves.
*/
function setHoveredNode(node: NoteMapNodeObject | null) {
+ // Where everything stands is taken down before any of it changes, that being what the fade
+ // about to start reads from.
+ nodeFocus.restart();
+ linkFocus.restart();
+ keepPaintingThroughFade();
+
hoverNode = node;
neighbours.clear();
highlightLinks.clear();
@@ -91,6 +113,8 @@ export function setupRendering(graph: ForceGraph<NoteMapNodeObject, NoteMapLinkO
return;
}
+ highlightColor = normalizeColor(getColorForNode(hoverNode));
+
for (const link of notesAndRelations.links) {
const { source, target } = link;
// Both ends are notes of the map by the time it is drawn, the graph having looked up what
@@ -115,17 +139,51 @@ export function setupRendering(graph: ForceGraph<NoteMapNodeObject, NoteMapLinkO
}
/**
- * What a relation is drawn in: the colour of the note being hovered where it is one of that
- * note's own, so that what leads to and from it is read as belonging to it — arrowheads included,
- * which take the colour of the line they end. The rest of the map's relations fade back with the
- * notes they run between.
+ * What a relation is drawn in, where it stands between the map's own muted colour and the colour
+ * of the note being hovered — so that what leads to and from that note is read as belonging to it,
+ * arrowheads included, which take the colour of the line they end. The rest of the map's relations
+ * fade back with the notes they run between.
+ *
+ * A relation cannot be faded the way a note is, the graph being handed its colour rather than
+ * drawing it, so what it is faded by is asked for as part of the colour.
*/
function getLinkColor(link: NoteMapLinkObject) {
- if (highlightLinks.has(link)) {
- return hoverNode ? getColorForNode(hoverNode) : cssData.textColor;
+ const focus = linkFocus.get(link);
+ const lit = Math.max(0, focus);
+ const color = lit <= 0 ? cssData.mutedTextColor
+ : lit >= 1 ? highlightColor
+ : mixColors(cssData.mutedTextColor, highlightColor, lit);
+
+ return withAlpha(color, getFadedAlpha(focus));
+ }
+
+ /** How much of a note or a relation is drawn, from all of it to {@link DIMMED_ALPHA} faded back. */
+ function getFadedAlpha(focus: number) {
+ return DIMMED_ALPHA + (1 - DIMMED_ALPHA) * Math.min(1, 1 + focus);
+ }
+
+ /**
+ * The colour as a canvas keeps it — `#rrggbb` — whatever it was written as, a note being free to
+ * ask for any colour CSS knows by name. Only that form can be faded or mixed with another.
+ */
+ function normalizeColor(color: string) {
+ if (!colorResolver) {
+ return color;
}
- return hoverNode ? dimmedLinkColor : cssData.mutedTextColor;
+ colorResolver.fillStyle = "#000000";
+ colorResolver.fillStyle = color;
+ return typeof colorResolver.fillStyle === "string" ? colorResolver.fillStyle : color;
+ }
+
+ /**
+ * Keeps the canvas painting for as long as a fade runs. It is otherwise only repainted once
+ * something has changed — and a fade is not a change, it is what follows one.
+ */
+ function keepPaintingThroughFade() {
+ graph.autoPauseRedraw(false);
+ clearTimeout(fadeTimer);
+ fadeTimer = setTimeout(() => graph.autoPauseRedraw(true), HOVER_FADE_MS + 100);
}
function getColorForNode(node: NoteMapNodeObject) {
@@ -311,10 +369,7 @@ export function setupRendering(graph: ForceGraph<NoteMapNodeObject, NoteMapLinkO
// Every note keeps its own colour while one of them is hovered; what tells the note's
// own from the rest of the map is that the rest of it is faded back behind them.
ctx.save();
- if (!isInHoveredNeighbourhood(node)) {
- ctx.globalAlpha = DIMMED_ALPHA;
- }
-
+ ctx.globalAlpha = getFadedAlpha(nodeFocus.get(node));
paintNode(node, getColorForNode(node), ctx);
ctx.restore();
})
@@ -335,7 +390,7 @@ export function setupRendering(graph: ForceGraph<NoteMapNodeObject, NoteMapLinkO
.onZoom((zoom) => zoomLevel = zoom.k);
graph
- .linkWidth((link) => (highlightLinks.has(link) ? 3 : 0.4))
+ .linkWidth((link) => LINK_WIDTH + (HIGHLIGHT_LINK_WIDTH - LINK_WIDTH) * Math.max(0, linkFocus.get(link)))
.linkColor((link) => getLinkColor(link))
.linkDirectionalArrowLength(4)
.linkDirectionalArrowRelPos(0.95);
@@ -361,7 +416,47 @@ export function setupRendering(graph: ForceGraph<NoteMapNodeObject, NoteMapLinkO
graph.d3Force("charge")?.strength(boundedCharge);
graph.d3Force("charge")?.distanceMax(1000);
- return setupFraming(graph, container, { note, noteId, widgetMode, notesAndRelations });
+ const stopFraming = setupFraming(graph, container, { note, noteId, widgetMode, notesAndRelations });
+
+ return () => {
+ clearTimeout(fadeTimer);
+ stopFraming();
+ };
+}
+
+/**
+ * Values that make their way to where a change puts them over {@link HOVER_FADE_MS}, rather than
+ * being there the moment it is made.
+ *
+ * Each is read from where it stood when the change was made and where it is to end up, so a pointer
+ * carried straight from one note to another takes each of them from wherever the last change had left
+ * it — rather than from where a fade of the whole map would have them all start together.
+ *
+ * @param getTarget where the value should end up, as things stand now.
+ */
+function createFade<T>(elements: T[], getTarget: (element: T) => number) {
+ const startingPoints = new Map<T, number>();
+ let startedAt = 0;
+
+ function get(element: T) {
+ const target = getTarget(element);
+ const from = startingPoints.get(element) ?? target;
+ const progress = Math.min(1, (performance.now() - startedAt) / HOVER_FADE_MS);
+
+ return from + (target - from) * progress;
+ }
+
+ return {
+ get,
+ /** Takes down where every value stands, for the fade the caller is about to bring about to start from. */
+ restart() {
+ for (const element of elements) {
+ startingPoints.set(element, get(element));
+ }
+
+ startedAt = performance.now();
+ }
+ };
}
/**
diff --git a/apps/client/src/widgets/note_map/utils.spec.ts b/apps/client/src/widgets/note_map/utils.spec.ts
index 698d19c892..f2379b427b 100644
--- a/apps/client/src/widgets/note_map/utils.spec.ts
+++ b/apps/client/src/widgets/note_map/utils.spec.ts
@@ -1,6 +1,33 @@
import { describe, expect, it } from "vitest";
-import { getFitPadding, isLightColor, toMapType } from "./utils";
+import { getFitPadding, isLightColor, mixColors, toMapType, withAlpha } from "./utils";
+
+describe("mixColors", () => {
+ it("takes a colour the given part of the way to another, and gives up on what it cannot read", () => {
+ expect(mixColors("#000000", "#ffffff", 0)).toBe("#000000");
+ expect(mixColors("#000000", "#ffffff", 1)).toBe("#ffffff");
+ expect(mixColors("#000000", "#ffffff", 0.5)).toBe("#808080");
+ expect(mixColors("#ff0000", "#0000ff", 0.5)).toBe("#800080");
+
+ // Anything but a plain `#rrggbb` is past mixing: the colour being headed for is answered outright.
+ expect(mixColors("red", "#0000ff", 0.5)).toBe("#0000ff");
+ expect(mixColors("#000000", "rgb(0, 0, 255)", 0.5)).toBe("rgb(0, 0, 255)");
+ });
+});
+
+describe("withAlpha", () => {
+ it("asks for a colour at an opacity, and leaves alone what cannot carry one", () => {
+ expect(withAlpha("#123456", 1)).toBe("#123456ff");
+ expect(withAlpha("#123456", 0)).toBe("#12345600");
+ expect(withAlpha("#123456", 0.15)).toBe("#12345626");
+
+ // Beyond either end is still one end or the other, rather than a colour a canvas cannot read.
+ expect(withAlpha("#123456", 2)).toBe("#123456ff");
+ expect(withAlpha("#123456", -1)).toBe("#12345600");
+
+ expect(withAlpha("red", 0.5)).toBe("red");
+ });
+});
describe("toMapType", () => {
it("only reads the tree map out of the label, the link map standing for anything else", () => {
diff --git a/apps/client/src/widgets/note_map/utils.ts b/apps/client/src/widgets/note_map/utils.ts
index 1ce84a7826..7703392a9a 100644
--- a/apps/client/src/widgets/note_map/utils.ts
+++ b/apps/client/src/widgets/note_map/utils.ts
@@ -80,6 +80,46 @@ export function isLightColor(color: string) {
return (red * 0.299 + green * 0.587 + blue * 0.114) > 150;
}
+/**
+ * The colour a given part of the way from one to the other, for fading between two of them.
+ *
+ * @param ratio 0 for the first colour, 1 for the second. Both are expected as `#rrggbb`, and anything
+ * else is taken to be past fading — the colour being headed for is answered outright.
+ */
+export function mixColors(from: string, to: string, ratio: number) {
+ const fromChannels = toChannels(from);
+ const toChannels_ = toChannels(to);
+
+ if (!fromChannels || !toChannels_) {
+ return to;
+ }
+
+ const mixed = fromChannels.map((channel, i) => Math.round(channel + (toChannels_[i] - channel) * ratio));
+ return `#${mixed.map((channel) => channel.toString(16).padStart(2, "0")).join("")}`;
+}
+
+/**
+ * The colour asked for at the given opacity, as a canvas takes it.
+ *
+ * @param alpha 0 for nothing at all, 1 for the colour as it stands.
+ */
+export function withAlpha(color: string, alpha: number) {
+ if (!/^#[0-9a-f]{6}$/i.test(color)) {
+ // Only a plain `#rrggbb` takes the two digits that say by how much; anything else keeps what
+ // it has rather than being handed something a canvas cannot read.
+ return color;
+ }
+
+ return `${color}${Math.round(Math.max(0, Math.min(1, alpha)) * 255).toString(16).padStart(2, "0")}`;
+}
+
+/** The red, green and blue of a `#rrggbb`, or nothing where it is not one. */
+function toChannels(color: string) {
+ const channels = color.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);
+
+ return channels ? channels.slice(1, 4).map((channel) => parseInt(channel, 16)) : null;
+}
+
export function generateColorFromString(str: string, themeStyle: "light" | "dark") {
if (themeStyle === "dark") {
str = `0${str}`; // magic lightning modifier