diff --git a/src/App.css b/src/App.css index b0b2f1f..9e55b38 100644 --- a/src/App.css +++ b/src/App.css @@ -1,25 +1,9 @@ body { background-color: #ccc; } - - -@media (prefers-reduced-motion: no-preference) { - .App-logo { - animation: App-logo-spin infinite 20s linear; - } -} - - - -.App-link { - color: #61dafb; -} - -@keyframes App-logo-spin { - from { - transform: rotate(0deg); - } - to { - transform: rotate(360deg); - } -} +#tooltip{ + background-color: #fff; + padding: 5px; + border-radius: 2px; + opacity: 90%; +} \ No newline at end of file diff --git a/src/App.js b/src/App.js index 4530789..ae236db 100644 --- a/src/App.js +++ b/src/App.js @@ -43,6 +43,7 @@ function TreeCanvas({ ny, }) { const treeCanvas = useRef() + const [istooltipOn, setTooltip] = useState(false) const nodesWithHandles = nodes.filter( node => !ancestorCollapsed[node] && nodeChildren[node].length @@ -55,7 +56,7 @@ function TreeCanvas({ }, [nodeHandleRadius, nx, ny] ) - + // useEffect+useRef is a conventional way to draw to // canvas using React. the ref is a "reference to the canvas DOM element" // and the useEffect makes sure to update it when the ref changes and/or @@ -143,6 +144,37 @@ function TreeCanvas({ nodeClicked(clickedNode) } }} + onMouseMove={evt => { + const { clientX, clientY } = evt + const mouseX = clientX - treeCanvas.current.getBoundingClientRect().left + const mouseY = clientY - treeCanvas.current.getBoundingClientRect().top + const ctx = treeCanvas.current.getContext('2d') + let FoundNode = null + nodesWithHandles.forEach(node => { + makeNodeHandlePath(node, ctx) + if (ctx.isPointInPath(mouseX, mouseY)) { + FoundNode = node + } + }) + if (FoundNode && !istooltipOn) { + setTooltip(true) + let elem = document.createElement('div') + elem.id = 'tooltip' + elem.style.position = 'absolute' + elem.style.left = mouseX+10+'px' + elem.style.top = mouseY+10+'px' + elem.textContent = FoundNode + document.body.appendChild(elem) + }else{ + try{ + setTooltip(false) + let elem = document.getElementById('tooltip') + elem.parentNode.removeChild(elem) + }catch{ + setTooltip(false) + } + } + }} width={width} height={height} style={{ width, height }}