zoukankan      html  css  js  c++  java
  • svg 折线鼠标绘制

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            #div1{ 780px;height: 370px;background: white; background: url(./bakground.jpeg) no-repeat;margin:20px auto; overflow: hidden;}
            body{background: gray;}
        </style>
    </head>
    <body>
        <div id="div1"> 
            <svg id = "svg1" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" >
                <!-- 折线 -->
               <!--  <polyline points = "50,50,200,300,230,300,250,200" fill = "none" stroke = "black" stroke-width="5"></polyline>  -->
                <!-- 闭合 -->
                <!-- <polygon points = "50,50,200,300,230,300,250,200" fill = "none" stroke = "black" stroke-width="5"></polygon>   -->
            </svg> 
            
        </div>
    </body>
    </html>
    <script>
        window.onload = function()
        {
            var svgNS = 'http://www.w3.org/2000/svg';
            var oParent = document.getElementById('div1');
            var oSvg = document.getElementById("svg1");
            var oPolyLine = null;
            var pointsNum = "";
    
         /* 封装一个创建标签的函数 */
          function createTag(tag,objAttr)
         {
           var oTag = document.createElementNS(svgNS,tag);
        
           for(var attr in objAttr)
           {
               oTag.setAttribute(attr,objAttr[attr]);
           }
           return oTag;
         }
    
         oSvg.onmousedown = function(ev)
           {
               var ev = ev||window.event;
               if(!oPolyLine)
               {
                oPolyLine = createTag('polyline',{'fill':'none','stroke':'red','stroke-width':'2'})
                oSvg.appendChild(oPolyLine);
               }
               oPolyLine = createTag('polyline',{'fill':'none','stroke':'red','stroke-width':'2'})
               oSvg.appendChild(oPolyLine);
    
               var x = ev.clientX-oParent.offsetLeft;
               var y = ev.clientY-oParent.offsetTop;
    
               if(pointsNum == "")
               {
                pointsNum = x + ',' + y;
               }
               else{
                pointsNum += ','+x+','+y;  
               }
    
               oPolyLine.setAttribute('points',pointsNum);
               var oCircle = createTag('circle',{'cx':x,'cy':y,'r':'5','fill':'white','stroke':'red','stroke-width':'3'});
               oSvg.append(oCircle)
           }
    
           oSvg.onmousemove = function(ev)
           {
               var ev = ev ||window.event;
               if(oPolyLine)
               {
                var x = ev.clientX-oParent.offsetLeft;
                var y = ev.clientY-oParent.offsetTop;
                oPolyLine.setAttribute('points',pointsNum+','+x+','+y);
    
               }
           }
    
           oSvg.oncontextmenu = function()
           {
               oSvg.onmousemove = null;
               return false;
           }
    
        }
    
        
    
    
    </script>
  • 相关阅读:
    C#中使用My实现单例应用程序
    喝着啤酒学Python(2):第一个HelloWorld
    再读《精通css》04:盒模型和空白边叠加
    再读《精通css》07:圆角
    再读《精通css》08:阴影
    @ResponseBody 乱码
    再读《精通css》05:定位、浮动与清理
    关于javascript面向对象的一点思考
    再读《精通css》06:背景图片
    【求解释】关于第三方接口调用中安全的疑问
  • 原文地址:https://www.cnblogs.com/Aaron-Lee/p/13855706.html
Copyright © 2011-2022 走看看