JSFiddle - React, Tailwind, and code Playground

by vinodlouis

HTML

<body>
    <svg>
        <defs id="defs"></defs>
        <g id="paths"></g>
        <g id="rects"></g>
    </svg>        
	</body>

CSS

svg {
    background-color: #FFF;
    /*cursor: default;*/
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    -o-user-select: none;
    user-select: none;
}

svg:not(.active):not(.ctrl) {
    cursor: crosshair;
}

path.link {
    fill: none;
    stroke: black;
    stroke-width: 2px;
    cursor: default;
}

svg:not(.active):not(.ctrl) path.link {
    cursor: pointer;
}

path.link.selected {
    stroke-dasharray: 10,2;
    color: blue;
    stroke: blue;
}

path.link.dragline {
    pointer-events: none;
}

path.link.hidden {
    stroke-width: 0;
}

circle.node {
    stroke-width: 1.5px;
    cursor: pointer;
}

circle.node.reflexive {
    stroke: #000 !important;
    stroke-width: 2.5px;
}

rect.route-node {
    cursor: pointer;
    stroke-width: 1px;
}

text {
    font: 12px sans-serif;
    pointer-events: none;
}

text.id {
    text-anchor: middle;
    font-weight: bold;
}

input {
    width: 300px;
}

JavaScript

// set up SVG for D3
var width  = 960,
    height = 800;

var isWantToAddNode = false;
var svg = d3.select('body').select('svg')
    .attr('width', width)
    .attr('height', height);
var links = [{
    id:1,
    fromPoint: {x:5,y:5},
    toPoint: {x:50,y:50},
    params: 'android',
    color: '#000'
}];
var times = 0;
function initAllDef() {
    svg.select('#defs').append('svg:marker')
        .attr('id', 'arrow').attr("viewBox", "0 -5 10 10")
        .attr("refX", 9)
        .attr("refY", 0)
        .attr("markerWidth", 6)
        .attr("markerHeight", 6)
        .attr("orient", "auto")
        .append("path")
        .attr("d", "M0,-3L10,0L0,3")
        .attr('fill', '#000');
}

initAllDef();

// handles to link and node element groups
var paths = svg.select('#paths').selectAll('path');
var routeNodes = svg.select('#rects').selectAll('rects');


// update graph (called when needed)
function restart() {
    function rebuildLink() {
        var pathData = paths.data(links, function(d){return d.id;});
        pathData.enter()
            .append('svg:path')
            .attr('d', function(d) {
                var f = d.fromPoint, t = d.toPoint;
                return 'M' + (f.x + times * 10) + ',' + (f.y) + 'L' + t.x + ',' + t.y;
            })
            .attr('class', 'link')
            .style('marker-end', 'url(#arrow)');
        pathData.on('mousedown', function(d){
                times += 1;
                restart();
            });
            console.log(pathData)
        pathData.exit().remove();
    }

    rebuildLink();
}


restart();