前端连线

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>获取坐标点位置,并连接点</title>
        <style>
        *{margin:0 ;padding:0;}
        .svgcontainer,.container{position:absolute;top:50%;left:50%;margin-left:-400px;margin-top:-250px;width:800px;height:500px;}
        .container{border:1px solid #ccc;cursor: pointer;}
        .dot{width:10px;height:10px;border-radius:50%;position:absolute;background:#0078B6;margin-top:-5px;margin-left:-5px;}
        .initdot{top:0;left:0;}
        </style>
    </head>
    <body>
        <div class="svgcontainer" id="svgcontainer"></div>
        <div class="container" id="container">
            <div class="dot initdot"></div>
        </div>
    </body>
</html>
<script>
    var container=document.getElementById("container");
    var svgcontainer=document.getElementById("svgcontainer");
    //创建svg 注:动态创建svg ie8及以下不支持该方法
    var svgns="http://www.w3.org/2000/svg";
    var svger=document.createElementNS(svgns,"svg");
    svger.setAttribute("width",container.clientWidth);
    svger.setAttribute("height",container.clientHeight);
    svger.setAttribute("viewBox","0 0 "+container.clientWidth+" "+container.clientHeight);
    svgcontainer.appendChild(svger);
     
    container.onclick=function(e){
        var e=e||window.event;//事件对象
        //获取点击地鼠标位置
        var mousePosition=mousePos(e);
        //新增点
        creatdot(mousePosition.x,mousePosition.y);
        //连接点
        var dots=container.children;
        linedot(dots[dots.length-2],dots[dots.length-1]);
    }
     
    //位置像素 数值化
    function intpixel(str){
        return str.substring(0,str.length-2)*1;
    }
     
    //获取鼠标坐标
    function mousePos(e){
        if(e.pageX){
            //IE9及以上支持pageX属性 相对文档
            return {x:e.pageX,y:e.pageY}
        }else{
            return {x:e.clientX+document.body.scrollLeft-document.body.clientLeft,
                    y:e.clientY+document.body.scrollTop-document.body.clientTop}
        }
    }
    //新增点
    function creatdot(posX,posY){
        //相对container坐标
        var newposX=posX-container.offsetLeft;
        var newposY=posY-container.offsetTop;
        var dot=document.createElement("div");
        dot.setAttribute("class","dot");
        //定位
        dot.style.left=newposX+"px";
        dot.style.top=newposY+"px";
        container.appendChild(dot);
    }
    //连接点
    function linedot(dot1,dot2){
        var start={x:intpixel(dot1.style.left),y:intpixel(dot1.style.top)};
        var end={x:intpixel(dot2.style.left),y:intpixel(dot2.style.top)};
        var current={x:start.x,y:start.y};
        //创建直线
        var line=document.createElementNS(svgns,"line");
        line.setAttribute("x1",dot1.style.left);
        line.setAttribute("y1",dot1.style.top);
        line.setAttribute("x2",dot1.style.left);
        line.setAttribute("y2",dot1.style.top);
        line.setAttribute("stroke","red");
        line.setAttribute("fill","none");
        svger.appendChild(line);
        //角度
        var tangle={
                        sin:(end.y-start.y)/Math.sqrt((end.y-start.y)*(end.y-start.y)+(end.x-start.x)*(end.x-start.x)),
                        cos:(end.x-start.x)/Math.sqrt((end.y-start.y)*(end.y-start.y)+(end.x-start.x)*(end.x-start.x))
                        };
        //动画
        var step=function(){
            //定义每帧移动位移大小为10
            if(Math.abs(current.x-end.x)<10&&Math.abs(current.y-end.y)<10){
                current.x=end.x;
                current.y=end.y;
            }else{
                current.x+=10*tangle.cos;
                current.y+=10*tangle.sin;
                var timepath=setTimeout(step,17);//浏览器重绘速度为60帧每秒
            }
            line.setAttribute("x2",current.x+"px");
            line.setAttribute("y2",current.y+"px");
        }
        step();
    }
     
</script>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
实现连线的方法有很多种,以下是一种基于 HTML5 Canvas 和 jQuery 的实现方式: 1. 在 HTML 中添加两个 Canvas 标签,一个用于绘制节点,一个用于绘制连线。 2. 使用 jQuery 将节点拖拽到 Canvas 上,并在节点上添加事件监听器,记录节点的位置信息。 3. 在连线 Canvas 上使用 JavaScript 绘制连线,根据节点位置信息计算连线起点和终点的坐标。 4. 如果需要实现动态连线,可以在节点上添加事件监听器,在节点拖拽时实时更新连线位置。 下面是一个简单的示例代码,仅供参考: HTML 代码: ``` <canvas id="node-canvas"></canvas> <canvas id="line-canvas"></canvas> ``` JavaScript 代码: ``` var nodeCanvas = document.getElementById("node-canvas"); var lineCanvas = document.getElementById("line-canvas"); var nodeCtx = nodeCanvas.getContext("2d"); var lineCtx = lineCanvas.getContext("2d"); var nodes = []; // 保存节点信息的数组 // 监听节点的mousedown事件,记录节点位置信息 $("#node-canvas").on("mousedown", function(e) { var offsetX = e.offsetX; var offsetY = e.offsetY; var newNode = { x: offsetX, y: offsetY }; nodes.push(newNode); }); // 监听节点的mouseup事件,绘制连线 $("#node-canvas").on("mouseup", function(e) { var offsetX = e.offsetX; var offsetY = e.offsetY; var lastNode = nodes[nodes.length - 1]; lineCtx.beginPath(); lineCtx.moveTo(lastNode.x, lastNode.y); lineCtx.lineTo(offsetX, offsetY); lineCtx.stroke(); }); // 监听节点的drag事件,实时更新连线位置 $("#node-canvas").on("drag", function(e) { var offsetX = e.offsetX; var offsetY = e.offsetY; var lastNode = nodes[nodes.length - 1]; lineCtx.clearRect(0, 0, lineCanvas.width, lineCanvas.height); lineCtx.beginPath(); lineCtx.moveTo(lastNode.x, lastNode.y); lineCtx.lineTo(offsetX, offsetY); lineCtx.stroke(); }); ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值