Drawing arrows on an HTML page to visualize semantic links between textual spans

前端 未结 10 1512
轮回少年
轮回少年 2020-11-30 18:56

I have an HTML page with some textual spans marked up something like this:

...
p50
...


        
相关标签:
10条回答
  • 2020-11-30 19:04

    If you don't need curved arrows, you could use absolutely positioned divs above or below the list. You could then use css to style those divs plus a couple of images that make up the arrow head. Below is an example using the icon set from the jQuery UI project (sorry about the long URL).

    Here's the CSS to get things started:

    <style>
     .below{
         border-bottom:1px solid #000;
         border-left:1px solid #000;
         border-right:1px solid #000;
     }
     .below span{
        background-position:0px -16px;
        top:-8px;
     }
     .above{
         border-top:1px solid #000;
         border-left:1px solid #000;
         border-right:1px solid #000;
     }
     .above span{
        background-position:-64px -16px;
        bottom:-8px;
     }
    
     .arrow{
        position:absolute;
        display:block;
        background-image:url(http://jquery-ui.googlecode.com/svn/trunk/themes/base/images/ui-icons_454545_256x240.png);
        width:16px;
        height:16px;
        margin:0;
        padding:0;
     }
    
    .left{left:-8px;}
    
    .right{right:-9px;}
    
    </style>
    

    Now we can start to assemble arrow divs. For instance, to style the arrow from "requires" to "promoter" in your example above, you could do left,bottom, and right borders on the div with and upward facing arrow graphic in the top left of the div.

    <div class='below' style="position:absolute;top:30px;left:30px;width:100px;height:16px">
       <span class='arrow left'></span>
    </div>
    

    The inline styles would be need to be applied by script after you figured out the locations of the things you would need to connect. Let's say that your list looks like this:

    <span id="promoter">Promoter</span><span>Something Else</span><span id="requires">Requires</span>
    

    Then the following script will position your arrow:

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> 
    <script>
    $(function(){
     var promoterPos=$("#promoter").offset();
     var requiresPos=$("#requires").offset();
     $("<div class='below'><span class='arrow left'></span></div>")
     .css({position:"absolute",left:promoterPos.left,right:promoterPos.top+$("#promoter").height()})
     .width(requiresPos.left-promoterPos.left)
     .height(16)
     .appendTo("body");
    });
    </script>
    

    Go ahead and paste the examples above into a blank html page. It's kind of neat.

    0 讨论(0)
  • 2020-11-30 19:04

    I needed a similar solution, and I was looking into RaphaelJS JavaScript Library. For example you can draw a straight arrow from (x1,y1) to (x2,y2) with:

    Raphael.fn.arrow = function (x1, y1, x2, y2, size) {
      var angle = Math.atan2(x1-x2,y2-y1);
      angle = (angle / (2 * Math.PI)) * 360;
      var arrowPath = this.path(“M” + x2 + ” ” + y2 + ” L” + (x2 - size) + ” ” + (y2 - size) + ” L” + (x2 - size) + ” ” + (y2 + size) + ” L” + x2 + ” ” + y2 ).attr(“fill”,”black”).rotate((90+angle),x2,y2);
      var linePath = this.path(“M” + x1 + ” ” + y1 + ” L” + x2 + ” ” + y2);
      return [linePath,arrowPath];
    }
    

    I haven't figure out how to draw a curved arrow, but I'm sure it's possible.

    0 讨论(0)
  • 2020-11-30 19:05

    You could try this JavaScript Vector Graphics Library - it's very clever stuff, hope it helps.

    EDIT: As this link is dead, here is another link from Archive.org.

    0 讨论(0)
  • 2020-11-30 19:07

    I try to go with open web technologies wherever possible but the truth is that HTML & JavaScript (or jQuery) aren't the tools for this particular job (sad but true), especially as the diagrams you're drawing increase in complexity.

    On the other hand, Flash was made for this. Significantly less ActionScript 3.0 code would be required to parse that XML, layout your text (with more control over fonts & super/subscripts) and render the curves (see the flash.display.Graphics class methods like curveTo). Overall you'll be looking at less code, better maintainability, fewer hacks, wider compatibility and more stable drawing libraries.

    Good luck with the project.

    0 讨论(0)
  • 2020-11-30 19:14

    You could get the curved arrow ends using a handful of position:absolute divs with background-image set to transparent GIFs... a set for beginning (top and bottom)... a bacground:repeat div for expandible middle, and another pair for the ends (top and bottom).

    0 讨论(0)
  • 2020-11-30 19:15

    You have a couple options: svg or canvas.

    From the looks of it you don't need these arrows to have any particular mathematical form, you just need them to go between elements.

    Try WireIt. Have a look at this WireIt Demo (which has been deprecated). It uses a canvas tag for each individual wire between the floating dialog divs, then sizes and positions each canvas element to give the appearance of a connecting line at just the right spot. You may have to implement an additional rotating arrowhead, unless you don't mind the arrows coming in to each element at the same angle.

    Edit: the demo has been deprecated.

    Edit: Ignore this answer, @Phil H nailed it

    0 讨论(0)
提交回复
热议问题