Home > Blockchain >  How can I build slider-functionality within an SVG file?
How can I build slider-functionality within an SVG file?

Time:07-03

I have an SVG image of a slider with a colored bar and a handle (see code snippet below). I want to make this SVG image interactive, so the viewer can move the handle to the eleven different positions along the slider. Everything needs to be within the <svg></svg> tags; I can't use external HTML or scripts. The SVG file will be added to an HTML webpage in an <object> tag to preserve interactivity.

Included in the SVG file are eleven invisible <rect> elements to act as hitboxes for each of the eleven slider positions, and a transform="translate(0 0)" tag on the handle group. Clicking in one of the hitboxes should move the handle to the corresponding position by updating the transform parameter's x-value; with the mouse button held down, moving the mouse from side to side should move the handle to the position corresponding to the x-coordinate of the mouse, even if the mouse moves vertically out of range of the hitboxes.

I know you can put JavaScript inside SVG files, but I'm not very familiar with JS so I don't know how to approach this. Is JS the best way? If so, how do I use it to get the desired functionality?

<svg id="Slider-Image" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="240" height="84" viewBox="0 0 240 84">
    <defs>
        <style>
            .hitbox {
                cursor: pointer;
                opacity: 0;
            }
        </style>
    </defs>
    <g id="Slider-BG">
        <rect id="BG-Fill" width="240" height="84" fill="#999"/>
        <polygon id="Bevel-Top" points="9 29 14 35 226 35 231 29 9 29" fill="#887"/>
        <polygon id="Bevel-Right" points="226 35 226 63 231 69 231 29 226 35" fill="#aba"/>
        <polygon id="Bevel-Left" points="9 29 9 69 14 63 14 35 9 29" fill="#baa"/>
        <polygon id="Bevel-Bottom" points="9 69 231 69 226 63 14 63 9 69" fill="#cdd"/>
    </g>
    <g id="Slider-Bar">
        <rect id="Slider-Bar-BG" x="14" y="35" width="212" height="28"/>
        <rect id="Slider-Bar-Red" x="15" y="38" width="18" height="19" fill="#f22"/>
        <rect id="Slider-Bar-Yellow" x="35" y="38" width="18" height="19" fill="#ec0"/>
        <rect id="Slider-Bar-Green" x="53" y="38" width="171" height="19" fill="#9e1"/>
        <line id="Slider-Bar-Divider-1" x1="53.5" y1="38" x2="53.5" y2="57" stroke="#000" opacity="0.5"/>
        <line id="Slider-Bar-Divider-2" x1="72.5" y1="38" x2="72.5" y2="57" stroke="#000" opacity="0.5"/>
        <line id="Slider-Bar-Divider-3" x1="91.5" y1="38" x2="91.5" y2="57" stroke="#000" opacity="0.5"/>
        <line id="Slider-Bar-Divider-4" x1="110.5" y1="38" x2="110.5" y2="57" stroke="#000" opacity="0.5"/>
        <line id="Slider-Bar-Divider-5" x1="129.5" y1="38" x2="129.5" y2="57" stroke="#000" opacity="0.5"/>
        <line id="Slider-Bar-Divider-6" x1="148.5" y1="38" x2="148.5" y2="57" stroke="#000" opacity="0.5"/>
        <line id="Slider-Bar-Divider-7" x1="167.5" y1="38" x2="167.5" y2="57" stroke="#000" opacity="0.5"/>
        <line id="Slider-Bar-Divider-8" x1="186.5" y1="38" x2="186.5" y2="57" stroke="#000" opacity="0.5"/>
        <line id="Slider-Bar-Divider-9" x1="205.5" y1="38" x2="205.5" y2="57" stroke="#000" opacity="0.5"/>
    </g>
    <g id="Slider-Handle" transform="translate(0 0)">
        <polygon id="Handle-Body" points="31 66 57 66 57 46 46 35 42 35 31 46 31 66" fill="#aaa"/>
        <rect id="Handle-Center" x="42" y="35" width="4" height="31" fill="#ddd"/>
        <rect id="Handle-Pointer" x="43" y="34" width="2" height="16" fill="#111"/>
    </g>
    <g id="Slider-Positions">
        <rect id="Slider-Position-Minus-20"  x="15" y="29" width="19" height="40"/>
        <rect id="Slider-Position-Plus-0"  x="34" y="29" width="19" height="40"/>
        <rect id="Slider-Position-Plus-19"  x="53" y="29" width="19" height="40"/>
        <rect id="Slider-Position-Plus-38"  x="72" y="29" width="19" height="40"/>
        <rect id="Slider-Position-Plus-57"  x="91" y="29" width="19" height="40"/>
        <rect id="Slider-Position-Plus-76"  x="110" y="29" width="19" height="40"/>
        <rect id="Slider-Position-Plus-95"  x="129" y="29" width="19" height="40"/>
        <rect id="Slider-Position-Plus-114"  x="148" y="29" width="19" height="40"/>
        <rect id="Slider-Position-Plus-133"  x="167" y="29" width="19" height="40"/>
        <rect id="Slider-Position-Plus-152"  x="186" y="29" width="19" height="40"/>
        <rect id="Slider-Position-Plus-171"  x="205" y="29" width="19" height="40"/>
    </g>
</svg>

CodePudding user response:

It is something like this. The code should look a bit different when it is an independent document, but you get the idea.

console.log(Math.ceil((200 - 53) / 19) * 19);
<svg id="Slider-Image" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="240" height="84" viewBox="0 0 240 84">
  <defs>
    <style>
      #Slider-Hitbox {
        cursor: pointer;
        opacity: 0;
      }
      .divider {
        stroke: #000;
        opacity: 0.5;
      }
    </style>
  </defs>
  <g id="Slider-BG">
    <rect id="BG-Fill" width="240" height="84" fill="#999"/>
    <polygon id="Bevel-Top" points="9 29 14 35 226 35 231 29 9 29" fill="#887"/>
    <polygon id="Bevel-Right" points="226 35 226 63 231 69 231 29 226 35" fill="#aba"/>
    <polygon id="Bevel-Left" points="9 29 9 69 14 63 14 35 9 29" fill="#baa"/>
    <polygon id="Bevel-Bottom" points="9 69 231 69 226 63 14 63 9 69" fill="#cdd"/>
  </g>
  <g id="Slider-Bar">
    <rect id="Slider-Bar-BG" x="14" y="35" width="212" height="28"/>
    <rect id="Slider-Bar-Red" x="15" y="38" width="18" height="19" fill="#f22"/>
    <rect id="Slider-Bar-Yellow" x="35" y="38" width="18" height="19" fill="#ec0"/>
    <rect id="Slider-Bar-Green" x="53" y="38" width="171" height="19" fill="#9e1"/>
    <line id="Slider-Bar-Divider-1"  x1="53.5" y1="38" x2="53.5" y2="57"/>
    <line id="Slider-Bar-Divider-2"  x1="72.5" y1="38" x2="72.5" y2="57"/>
    <line id="Slider-Bar-Divider-3"  x1="91.5" y1="38" x2="91.5" y2="57"/>
    <line id="Slider-Bar-Divider-4"  x1="110.5" y1="38" x2="110.5" y2="57"/>
    <line id="Slider-Bar-Divider-5"  x1="129.5" y1="38" x2="129.5" y2="57"/>
    <line id="Slider-Bar-Divider-6"  x1="148.5" y1="38" x2="148.5" y2="57"/>
    <line id="Slider-Bar-Divider-7"  x1="167.5" y1="38" x2="167.5" y2="57"/>
    <line id="Slider-Bar-Divider-8"  x1="186.5" y1="38" x2="186.5" y2="57"/>
    <line id="Slider-Bar-Divider-9"  x1="205.5" y1="38" x2="205.5" y2="57"/>
  </g>
  <g id="Slider-Handle" transform="translate(0 0)">
    <polygon id="Handle-Body" points="31 66 57 66 57 46 46 35 42 35 31 46 31 66" fill="#aaa"/>
    <rect id="Handle-Center" x="42" y="35" width="4" height="31" fill="#ddd"/>
    <rect id="Handle-Pointer" x="43" y="34" width="2" height="16" fill="#111"/>
  </g>
  <rect id="Slider-Hitbox" x="13" y="34" width="214" height="30"/>
  <script>//<![CDATA[
    var down = false;
    var translateX = 0;
    const toSVGPoint = (svg, x, y) => {
      let p = new DOMPoint(x, y);
      return p.matrixTransform(svg.getScreenCTM().inverse());
    };
    document.getElementById("Slider-Hitbox").addEventListener("mousedown", e => {
      down = true;
      let p = toSVGPoint(document.getElementById("Slider-Image"), e.clientX, e.clientY);
      let translateX = Math.ceil((p.x-53) / 19) * 19;
      if(translateX < 0) translateX = -20;
      if(translateX > 171) translateX = 171;
      document.getElementById("Slider-Handle").setAttribute("transform", `translate(${translateX} 0)`);
    });
    document.getElementById("Slider-Image").addEventListener("mousemove", e => {
      if(down) {
        let p = toSVGPoint(document.getElementById("Slider-Image"), e.clientX, e.clientY);
        let translateX = Math.ceil((p.x-53) / 19) * 19;
        if(translateX < 0) translateX = -20;
        if(translateX > 171) translateX = 171;
        document.getElementById("Slider-Handle").setAttribute("transform", `translate(${translateX} 0)`);
      }
    });
    document.getElementById("Slider-Image").addEventListener("mouseup", e => {
      down = false;
    });
  //]]></script>
</svg>

This is the entire SVG with JavaScript running in an object element:

<object width="240" data="data:image/svg xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPHN2ZyBpZD0iU2xpZGVyLUltYWdlIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMj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