在SVG图像中更改路径标记的高度和宽度

时间:2013-01-02 06:52:12

标签: jquery svg d3.js jquery-svg

Slider SVG Image

我正在尝试更改灰色区域的宽度,并根据1到100之间的某个值移动圆圈图层。

我正在使用带有SVG图像的D3.js。下面是svg图像的代码,但我无法更改宽度,因为路径标记没有任何直接属性来执行此操作。

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Creator: CorelDRAW -->
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="2480px" height="3508px" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd"
viewBox="0 0 2480 3507.43">
 <defs>
  <style type="text/css">
   <![CDATA[
    .str0 {stroke:#989B9F;stroke-width:2.08354}
    .str1 {stroke:#989B9F;stroke-width:2.08354}
    .str2 {stroke:#989B9F;stroke-width:2.08354}
    .fil0 {fill:url(#id0)}
    .fil1 {fill:url(#id1)}
    .fil2 {fill:url(#id2)}
   ]]>
  </style>
  <linearGradient id="id0" gradientUnits="userSpaceOnUse" x1="301.84" y1="146.253" x2="301.84" y2="122.891">
   <stop offset="0" style="stop-color:#D7D9DC"/>
   <stop offset="1" style="stop-color:white"/>
  </linearGradient>
  <linearGradient id="id1" gradientUnits="userSpaceOnUse" x1="191.679" y1="122.891" x2="191.679" y2="146.253">
   <stop offset="0" style="stop-color:#D7D9DC"/>
   <stop offset="1" style="stop-color:#42494C"/>
  </linearGradient>
  <linearGradient id="id2" gradientUnits="userSpaceOnUse" x1="259.354" y1="155.487" x2="259.354" y2="113.657">
    <stop offset="0" style="stop-color:#D7D9DC"/>
   <stop offset="1" style="stop-color:white"/>
  </linearGradient>
 </defs>
 <g id="Layer_x0020_1">
  <metadata id="CorelCorpID_0Corel-Layer"/>
  <path class="fil0 str0" d="M470.747 146.253l-337.814 0c-6.42438,0 -11.6808,-5.25642 -11.6808,-11.6808l0 0c0,-6.42438 5.25642,-11.6808 11.6808,-11.6808l337.814 0c6.42438,0 11.6808,5.25642 11.6808,11.6808l0 0c0,6.42438 -5.25642,11.6808 -11.6808,11.6808z"/>
 </g>
 <g id="Layer_x0020_2">
  <metadata id="CorelCorpID_1Corel-Layer"/>
  <path class="fil1 str1" d="M132.933 122.891l117.492 0c6.42438,0 11.6808,5.25642 11.6808,11.6808l0 0c0,6.42438 -5.25642,11.6808 -11.6808,11.6808l-117.492 0c-6.42438,0 -11.6808,-5.25642 -11.6808,-11.6808l0 0c0,-6.42438 5.25642,-11.6808 11.6808,-11.6808z"/>
 </g>
 <g id="Layer_x0020_3">
  <metadata id="CorelCorpID_2Corel-Layer"/>
  <circle class="fil2 str2"  cx="259.353" cy="134.572" r="20.9144"/>
 </g>
</svg>

2 个答案:

答案 0 :(得分:1)

我建议使用rects而不是滑块的路径。如果您创建需要动态创建的SVG,那么通常手动创建它而不是使用可视化编辑器更好(特别是如果它像在这种情况下一样简单)。可视化编辑器不仅创建了许多不需要的代码,而且还选择了几乎随机的坐标值。

Here's a significantly simplified example,使用rects代替路径,使推子从坐标0到100水平跨越,以便更轻松地分配值:

<svg xmlns="http://www.w3.org/2000/svg" width="300" height="50" viewBox="-10 -5 110 20">
  <defs>
    <style type="text/css">
      .fader           {stroke:#989B9F}
      .faderBackground,
      .faderKnob       {fill:url(#lightFaderGradient)}
      .faderForeground {fill:url(#darkFaderGradient)}
    </style>
    <linearGradient id="darkFaderGradient" gradientUnits="objectBoundingBox" x2="0" y2="1">
      <stop offset="0" stop-color="#D7D9DC"/>
      <stop offset="1" stop-color="#42494C"/>
    </linearGradient>
    <linearGradient id="lightFaderGradient" gradientUnits="objectBoundingBox" x2="0" y2="1">
      <stop offset="0" stop-color="white"/>
      <stop offset="1" stop-color="#D7D9DC"/>
    </linearGradient>
  </defs>
  <g id="myFader" class="fader">
    <rect class="faderBackground" width="100" height="10" rx="5"/>
    <rect class="faderForeground" width="50" height="10" rx="5"/>
    <circle class="faderKnob" cy="5" cx="50" r="7"/>
  </g>
</svg>

我使用标准DOM操作添加了一些示例JS代码,但当然您可以使用d3.js来获得更加便捷的代码:

var faderX = parseFloat(prompt("Enter a value from 0 to 100"))

var fader = document.getElementById("myFader")
fader.getElementsByClassName("faderKnob")[0]
  .setAttribute("cx",faderX)
fader.getElementsByClassName("faderForeground")[0]
  .setAttribute("width",faderX)

答案 1 :(得分:-3)

<html lang="en"><head>
    <meta charset="UTF-8">


    <style>



    </style>
</head>
<body>
    <svg
   width="100"
   height="100"
   id="slider"
   version="1.1"
   onload="init(evt)"
   onmouseup="released()"
       onmousemove="drag(evt)">
    <defs xmlns="http://www.w3.org/2000/svg">
        <!-- Symbol for Slider -->
        <symbol id="sliderSymbol" overflow="visible">
            <line x1="0" y1="-10" x2="0" y2="10" stroke="dimgray" stroke-width="9" pointer-events="none"/>
        </symbol>
    </defs>

 <script>
 <![CDATA[

      var isGripped=false

      var grabY   // mousedown coordinate
      var grabX
      var monitor // text reading 0-100
      var y       // the slider's dynamic coordinate

      var T_PI=2*Math.PI;

      function init(evt){

         var svgns = "http://www.w3.org/2000/svg";
         SVGDocument = evt.target.ownerDocument;

         monitor= SVGDocument.getElementById('monitor');
         handle=SVGDocument.getElementById('handle');

     // set dimensions according to the sliderFrame node below

         var sliderFrame= SVGDocument.getElementById('sliderFrame')

     //  y0 is the center of the slider, s is the range about the center

         s=0.5*parseFloat(sliderFrame.getAttribute('width'));
         y0=parseFloat(sliderFrame.getAttribute('y'))+s-0.5*parseFloat(handle.getAttribute('height'));
         y=y0

         handle.setAttribute("x",y0);
     }



      function grip(evt) {  

            isGripped=true;
            grabX=evt.clientX-y;
      }


      function drag(evt){

            if (!isGripped) return;
            X=(evt.clientX);

       // observe bounding box constraint

           y=Math.min(Math.max(X-grabX,y0-s),y0+s);       

           handle.setAttribute("x",y);
           monitor.textContent=Math.round(50*(1-(y-y0)/s));
      }


      function released(){
         isGripped=false;
      }


 ]]></script>





          <path id="sliderpathL" d="M 30 90 l 50 -25 l 0 50 l -50 -25" stroke="black" stroke-width="1" fill="red" onclick="drag(evt)">
          </path>





               <line stroke-linecap="square" stroke-width="15" stroke="cyan" y2="90" x2="325" y1="90" x1="90"/>
               <line fill="none" stroke-width="2" stroke="#000000" id="svg_1" y2="102" x2="89.5" y1="96" x1="89.5"/>
               <line fill="none" stroke-width="2" stroke="#000000" id="svg_2" y2="102" x2="145.5" y1="96" x1="145.5"/>
               <line fill="none" stroke-width="2" stroke="#000000" id="svg_3" y2="105" x2="209.5" y1="96" x1="209.5"/>
               <line fill="none" stroke-width="2" stroke="#000000" id="svg_4" y2="101" x2="267.5" y1="96" x1="267.5"/>
                  <line fill="none" stroke-width="2" stroke="#000000" id="svg_4" y2="101" x2="317.5" y1="96" x1="317.5"/>

          <rect   id="sliderFrame" 
          x="85" 
          y="90" 
          width="245" 
          height="4" 
          stroke="dimgray" 
          stroke-width="0" 
        onmousemove="drag(evt)"  />

         <rect
           onmousedown="grip(evt)"
           height="20"
           width="10"
           x="85"
           y="80"
           id="handle"
           style="fill:#0000ff" />  
      <path id="sliderpathR" d="M 383 90 l -50 -25 l 0 50 l 50 -25" stroke="black" stroke-width="1" fill="red"></path>

        <g id="textbox">
       <rect x="390" y="70" width="100" height="40" rx=3 ry=3 style="stroke:#006600; fill: white"/>
       <text id='monitor' x="400" y="88">50</text>
        </g>




    </svg>



</body>
</html>
相关问题