D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
redblobgames
Full window
Github gist
Outside the box
<!DOCTYPE html> <meta charset="utf-8"> <title>Outside the box</title> <script src="//d3js.org/d3.v4.min.js"></script> <style> @import url("//bl.ocks.org/style.css"); body { margin-left: auto; margin-right: auto; max-width: 35em; } cite { border-bottom: 2px dotted blue; cursor: pointer; } #more-arrows, figcaption { text-align: center; } #diagram { border: 1px solid hsl(0,10%,80%); position: relative; z-index: 1; overflow: visible; } svg.highlighted, span.highlighted { outline: 2px dotted hsl(0,50%,50%); } .arrow { fill: none; stroke: hsl(0,50%,50%); stroke-width: 6.5; marker-end: url(#arrowhead); filter: url(#outline); } text { text-anchor: middle; } </style> <html> <p> (this is the draft page; I posted the final version here: <a href="https://simblob.blogspot.com/2016/10/outside-box.html">https://simblob.blogspot.com/2016/10/outside-box.html</a>) </p> <p> I write interactive explanations; I'm always looking for simpler ways to explain things. </p> <p> Think back to every technical paper you've read. The text connects to diagrams by using text such as "see figure 4". Maybe it's more specific and says "in figure 4 see circle 24". Maybe it's a hypertext link. However if I look at how I take notes on paper, I don't do that! I just <cite id="interact-arrow">draw an arrow</cite><span id="anchor-1"></span> to the thing I want to point to. </p> <figure> <svg id="diagram" width="450" height="100"> <defs> <marker id="arrowhead" viewBox="0 0 10 10" refX="7" refY="5" markerUnits="strokeWidth" markerWidth="4" markerHeight="3" orient="auto"> <path d="M 0 0 L 10 5 L 0 10 z" fill="hsl(0,50%,50%)"/> </marker> <filter id="outline" x="-100%" y="-100%" width="300%" height="300%"> <!-- filter size is overkill but I don't know how to make it 3 pixels wider than the natural size --> <feMorphology result="outline" in="SourceGraphic" operator="dilate" radius="1"/> <feColorMatrix type="matrix" in="outline" result="black-outline" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0"/> <feBlend in="SourceGraphic" in2="black-outline" mode="normal"/> </filter> </defs> </svg> <figcaption>Figure 4. Sea of circles.</figcaption> </figure> <p id="more-arrows"></p> <p> From an early age we have <em>invisible boxes</em> around text and diagrams, keeping them apart. It doesn't have to be this way. Pointing is the simplest way to direct attention to something. Why don't we do it more? I don't know, but these are the kinds of things I'm exploring on the web. </p> <p> How did I implement this? The first guess would be that I'm using a giant SVG overlay that covers the area between the pointer and the target. I'm not! <span id="anchor-2"></span><cite id="interact-show-svg">That's the only SVG element</cite> on the page. What's the trick? By default, the <code>overflow</code> for an SVG element is set to <code>hidden</code>; in some versions of IE it was <code>visible</code> and I had to set it back to hidden. That keeps the content inside its bounding box. But the IE situation made me wonder — if I set it to <code>visible</code>, what happens? It turns out you can draw outside the box! This seems to work across the browsers I've tried. </p> <p> The second thing I need to do is construct an arrow path. The source and target of the arrow may be in different SVG elements, or they might not be SVG at all. For this page I use <cite id="interact-show-anchor">an invisible span</cite><span id="anchor-3"></span> in the text as the anchor. I use <code>getBoundingClientRect()</code> to get the coordinates on the page. Then I pick the midpoint of one side of the rectangle as the arrow source or target. I need to translate all the coordinates into the coordinate space of the SVG I'm drawing into. </p> <p> For this demo I hard-coded the size, curvature, and directions of the arrows. I don't adjust them when the page is resized. For a reusable library, I think it'd be cleaner to have one svg per arrow anchor. </p> <script src="arrows.js"></script> </html>
https://d3js.org/d3.v4.min.js