Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html>
<head>
  <title>05. Edges with arrows.</title>
</head>
<body onload='main()'>
</body>
</html>
              
            
!

CSS

              
                html, body, svg { width: 100%; height: 100%;}
.node-label {
   position: absolute;
   pointer-events: none;
   color: white;
   z-index: 10;
 }


              
            
!

JS

              
                function main () {
  
  let graph = Viva.Graph.graph();
  
  graph.addLink('a57c2a427dfa591b1243343c8413c249faac3e5df2fe4fa1fc93dca3d904f3c7', 
                'ddd587d54b693a9bc9bda2218c6f5e17979f6ac53755c5c1f668f3fa728e472d',
               {
                'linkTo': 'https://blockstream.info/tx/a57c2a427dfa591b1243343c8413c249faac3e5df2fe4fa1fc93dca3d904f3c7?output:0',
                'linkFrom': 'https://blockstream.info/tx/ddd587d54b693a9bc9bda2218c6f5e17979f6ac53755c5c1f668f3fa728e472d'
                });
  let graphics = Viva.Graph.View.svgGraphics();
  let nodeSize = 24;
  graphics.node(function(node){
     var url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/46/Bitcoin.svg/1024px-Bitcoin.svg.png';
      
    var ui = Viva.Graph.svg('g'),
                  // Create SVG text element with user id as content
                  svgText = Viva.Graph.svg('text').attr('y', '-4px').text(node.id),
                  img = Viva.Graph.svg('image')
                     .attr('width', nodeSize)
                     .attr('height', nodeSize)
                     .link(url);
              ui.append(svgText);
              ui.append(img);
              return ui;
  }).placeNode(function(nodeUI, pos) {
    nodeUI.attr('transform',
                            'translate(' +
                                  (pos.x - nodeSize/2) + ',' + (pos.y - nodeSize/2) +
                            ')')
  });
  
   let createMarker = function(id) {
        return Viva.Graph.svg('marker')
                               .attr('id', id)
                               .attr('viewBox', "0 0 10 10")
                               .attr('refX', "10")
                               .attr('refY', "5")
                               .attr('markerUnits', "strokeWidth")
                               .attr('markerWidth', "10")
                               .attr('markerHeight', "5")
                               .attr('orient', "auto");
                },
            marker = createMarker('Triangle');
            marker.append('path').attr('d', 'M 0 0 L 10 5 L 0 10 z');
            // Marker should be defined only once in <defs> child element of root <svg> element:
            let defs = graphics.getSvgRoot().append('defs');
            defs.append(marker);
            let geom = Viva.Graph.geom();
            graphics.link(function(link){
                // Notice the Triangle marker-end attribe:
                return Viva.Graph.svg('path')
                           .attr('stroke', 'gray')
                           .attr('marker-end', 'url(#Triangle)');
            }).placeLink(function(linkUI, fromPos, toPos) {
                // Here we should take care about
                //  "Links should start/stop at node's bounding box, not at the node center."
                // For rectangular nodes Viva.Graph.geom() provides efficient way to find
                // an intersection point between segment and rectangle
                let toNodeSize = nodeSize,
                    fromNodeSize = nodeSize;
                let from = geom.intersectRect(
                        // rectangle:
                                fromPos.x - fromNodeSize / 2, // left
                                fromPos.y - fromNodeSize / 2, // top
                                fromPos.x + fromNodeSize / 2, // right
                                fromPos.y + fromNodeSize / 2, // bottom
                        // segment:
                                fromPos.x, fromPos.y, toPos.x, toPos.y)
                           || fromPos; // if no intersection found - return center of the node
                let to = geom.intersectRect(
                        // rectangle:
                                toPos.x - toNodeSize / 2, // left
                                toPos.y - toNodeSize / 2, // top
                                toPos.x + toNodeSize / 2, // right
                                toPos.y + toNodeSize / 2, // bottom
                        // segment:
                                toPos.x, toPos.y, fromPos.x, fromPos.y)
                            || toPos; // if no intersection found - return center of the node
                let data = 'M' + from.x + ',' + from.y +
                           'L' + to.x + ',' + to.y;
                linkUI.attr("d", data);
            });
  
  let renderer = Viva.Graph.View.renderer(graph, {
                    graphics : graphics
                });
  renderer.run();
 
 }
              
            
!
999px

Console