summaryrefslogtreecommitdiff
path: root/components/Graph/drawLabels.ts
diff options
context:
space:
mode:
authorThomas F. K. Jorna <[email protected]>2021-10-16 00:37:11 +0200
committerThomas F. K. Jorna <[email protected]>2021-10-16 00:37:11 +0200
commit0ae9d628fabab8664c38d15bd6b5084a32c8eca2 (patch)
tree47ea4b690a2c65bde76146369355db3e03ad718c /components/Graph/drawLabels.ts
parent10b8fd40921b9e315edc19f8f2a07ce575930d18 (diff)
feat(labels): more sensible label positions
Diffstat (limited to 'components/Graph/drawLabels.ts')
-rw-r--r--components/Graph/drawLabels.ts27
1 files changed, 24 insertions, 3 deletions
diff --git a/components/Graph/drawLabels.ts b/components/Graph/drawLabels.ts
index 9314008..f559d3c 100644
--- a/components/Graph/drawLabels.ts
+++ b/components/Graph/drawLabels.ts
@@ -16,6 +16,9 @@ export interface drawLabelsProps {
opacity: number
nodeSize: (node: NodeObject) => number
filteredLinksByNodeId: LinksByNodeId
+ nodeRel: number
+ hoverNode: NodeObject
+ lastHoverNode: OrgRoamNode
}
export const getLabelOpacity = (
@@ -43,6 +46,9 @@ export function drawLabels(props: drawLabelsProps) {
opacity,
nodeSize,
filteredLinksByNodeId,
+ nodeRel,
+ hoverNode,
+ lastHoverNode,
} = props
if (!node) {
@@ -52,6 +58,8 @@ export function drawLabels(props: drawLabelsProps) {
if (!visuals.labels) {
return
}
+ const hoverId = hoverNode?.id ?? ''
+ const lastHoverId = lastHoverNode?.id ?? ''
const links = filteredLinksByNodeId[(node as OrgRoamNode).id] ?? []
const isHighlighty = !!(highlightedNodes[node.id!] || previouslyHighlightedNodes[node.id!])
@@ -64,7 +72,13 @@ export function drawLabels(props: drawLabelsProps) {
const label = nodeTitle.substring(0, visuals.labelLength)
- const fontSize = visuals.labelFontSize / (0.75 * Math.min(Math.max(0.5, globalScale), 3))
+ const nodeS = Math.cbrt(
+ (visuals.nodeRel * nodeSize(node)) / Math.pow(globalScale, visuals.nodeZoomSize),
+ )
+ const fontSize =
+ hoverId == (node as OrgRoamNode).id
+ ? Math.max((visuals.labelFontSize * nodeS) / 2, (visuals.labelFontSize * nodeS) / 3)
+ : (visuals.labelFontSize * nodeS) / 3
const textWidth = ctx.measureText(label).width
const bckgDimensions = [textWidth * 1.1, fontSize].map((n) => n + fontSize * 0.5) as [
@@ -74,7 +88,6 @@ export function drawLabels(props: drawLabelsProps) {
// draw label background
const textOpacity = getLabelOpacity(fadeFactor, visuals, globalScale, opacity, isHighlighty)
- const nodeS = 8 * Math.cbrt(nodeSize(node) * visuals.nodeRel)
if (visuals.labelBackgroundColor && visuals.labelBackgroundOpacity) {
const backgroundOpacity = textOpacity * visuals.labelBackgroundOpacity
const labelBackground = hexToRGBA(labelBackgroundColor, backgroundOpacity)
@@ -98,7 +111,15 @@ export function drawLabels(props: drawLabelsProps) {
nodeTitle.length > visuals.labelLength
? [...wordsArray.slice(0, -1), `${wordsArray.slice(-1)}...`]
: wordsArray
+
+ const highlightedNodeOffset = [hoverId, lastHoverId].includes((node as OrgRoamNode).id)
+ ? 1 + 0.3 * opacity
+ : 1
truncatedWords.forEach((word, index) => {
- ctx.fillText(word, node.x!, node.y! + nodeS + visuals.labelLineSpace * fontSize * index)
+ ctx.fillText(
+ word,
+ node.x! - 2,
+ node.y! + highlightedNodeOffset * nodeS * 8 + visuals.labelLineSpace * fontSize * index,
+ )
})
}