以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 SVG/GML/VRML/X3D/XAML 』  (http://bbs.xml.org.cn/list.asp?boardid=21)
----  [求助]SVG:用鼠标动态的画一条直线  (http://bbs.xml.org.cn/dispbbs.asp?boardid=21&rootid=&id=33176)


--  作者:daidaixiong
--  发布时间:5/27/2006 9:00:00 PM

--  [求助]SVG:用鼠标动态的画一条直线
我在学习svg有关的技术,现在结合javascript,想用鼠标动态的画一条直线,但是没有成功。哪位高手能给个简单的实例,万分感谢啦。我的邮箱 doudouxiong316@163.com
--  作者:zhou_gong
--  发布时间:5/27/2006 11:20:00 PM

--  
看一下我做的网站吧 http://www.9place.com:6677/svgmap/
--  作者:wanghai00
--  发布时间:5/28/2006 1:14:00 PM

--  
看看SVG - Learning By Coding上的这个例子。
drawlines.svg

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
  "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" [
  <!ATTLIST svg xmlns:xlink CDATA #FIXED "http://www.w3.org/1999/xlink">
]>
<!-- SVG - Learning By Coding - http://www.datenverdrahten.de/svglbc/ -->
<!--    Author: Dr. Thomas Meinike 10/03 - thomas@handmadecode.de     -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
  zoomAndPan="disable" onload="getSVGDoc(evt)">
  <title>SVG - Learning By Coding</title>
  <desc>SVG-Spezifikation in Beispielen</desc>
  <defs>
    <script type="text/javascript">
      <![CDATA[
      var svgdoc,svgroot,newline,posx,posy,posmin=50,drawing;
      var linecol="#000",linewidth="1px",check=false;
      var svgns="http://www.w3.org/2000/svg";
      function getSVGDoc(load_evt)
      {
        svgdoc=load_evt.target.ownerDocument;
        svgroot=svgdoc.rootElement;
        drawing=svgdoc.getElementById("drawing");
        svgroot.addEventListener("mousedown",MDown,false);
        svgroot.addEventListener("mousemove",MMove,false);
        svgroot.addEventListener("mouseup",MUp,false);
      }
      function MDown(mousedown_event)
      {
        Coords(mousedown_event);
        newline=svgdoc.createElementNS(svgns,"line");
        newline.setAttribute("x1",posx);
        newline.setAttribute("x2",posx);
        newline.setAttribute("y1",posy);
        newline.setAttribute("y2",posy);
        newline.setAttribute("stroke",linecol);
        newline.setAttribute("stroke-width",linewidth);
        check=true;
      }
      function MMove(mousemove_event)
      {
        if(check)
        {
          Coords(mousemove_event);
          newline.setAttribute("x2",posx);
          newline.setAttribute("y2",posy);
          drawing.appendChild(newline);
       }
      }
      function MUp()
      {
        check=false;
      }
      function Coords(mouse_event)
      {
        posx=mouse_event.clientX;
        posy=mouse_event.clientY;
        if(posx<posmin)posx=posmin;
        if(posy<posmin)posy=posmin;
        if(posx>640+posmin)posx=640+posmin;
        if(posy>480+posmin)posy=480+posmin;
      }
      function SetLineColor(mousedown_event)
      {
        linecol=mousedown_event.target.style.getPropertyValue("fill");
        svgdoc.getElementById("aktline").style.setProperty("stroke",linecol,"");
      }
      function SetLineWidth(mousedown_event)
      {
        linewidth=mousedown_event.target.style.getPropertyValue("stroke-width");
        svgdoc.getElementById("aktline").style.setProperty("stroke-width",linewidth,"");
      }
      ]]>
    </script>
  </defs>
  <text x="30" y="30" style="fill: #000; font-size: 24px">
    Linien zeichnen (mit Wahl von Farbe und Strichst&auml;rke)</text>
  <rect x="50" y="50" width="640" height="480" rx="5" ry="5"
    style="fill: #FFF; stroke: #000"/>
  <g onmousedown="SetLineColor(evt)">
    <rect x="30" y="60" width="10" height="10"
      style="fill: #FF0; stroke: #000"/>
    <rect x="30" y="80" width="10" height="10"
      style="fill: #F00; stroke: #000"/>
    <rect x="30" y="100" width="10" height="10"
      style="fill: #090; stroke: #000"/>
    <rect x="30" y="120" width="10" height="10"
      style="fill: #00C; stroke: #000"/>
    <rect x="30" y="140" width="10" height="10"
      style="fill: #000; stroke: #000"/>
  </g>
  <g onmousedown="SetLineWidth(evt)">
    <line x1="30" y1="180" x2="40" y2="180"
      style="stroke: #000; stroke-width: 1px"/>
    <line x1="30" y1="200" x2="40" y2="200"
      style="stroke: #000; stroke-width: 2px"/>
    <line x1="30" y1="220" x2="40" y2="220"
      style="stroke: #000; stroke-width: 3px"/>
    <line x1="30" y1="240" x2="40" y2="240"
      style="stroke: #000; stroke-width: 4px"/>
    <line x1="30" y1="260" x2="40" y2="260"
      style="stroke: #000; stroke-width: 5px"/>
  </g>
  <line id="aktline" x1="18" y1="58" x2="18" y2="262"
    style="stroke: #000; stroke-width: 1px"/>
  <!-- Ausgabe der Zeichnungsobjekte -->
  <g id="drawing"></g>
</svg>


[此贴子已经被作者于2006-5-28 14:36:23编辑过]

W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
58.594ms