简体   繁体   English

使用JavaScript填充SVG内部的模式

[英]Fill pattern inside svg using javascript

I have a svg element a square consist of five element polygon inside. 我有一个svg元素,一个正方形,里面包含五个元素。 When its click I want to make its outer line get bolder and the stripes line appear (inside the bolder line). 单击它时,我要使其外线变粗并出现条纹线(在粗线内)。 I've try to make it using javascript. 我尝试使用javascript使其成功。 But only the bold outer line can appear and the stripes line can't appear. 但是只能显示粗体外线,而不能显示条纹线。 Here is the picture I aim to achieve 这是我要达到的目标 在此处输入图片说明

And here's my code 这是我的代码

// Find the group that contains the polygon that was clicked on
var group = evt.target.parentNode;
// Get the bounding box of the group
var bbox = group.getBBox();
var svgns = "http://www.w3.org/2000/svg";
var shape = document.createElementNS(svgns, "polygon");
var pattern = document.createElementNS(svgns, "pattern");
shape.setAttribute("points", "0,0, 0,20, 20,20, 20,0");
shape.setAttributeNS(null, "fill", "none");
shape.setAttributeNS(null, "stroke", "black");
shape.setAttributeNS(null, "stroke-width", 2.5);
var xPos = bbox.x ;
var yPos = bbox.y ;
shape.setAttribute("transform", "translate(" + xPos + "," + yPos + ")");
//group.appendChild(shape);
pattern.setAttribute('id','pattern');
pattern.setAttribute('width','8');
pattern.setAttribute('height','10');
pattern.setAttribute('patternUnits', 'userSpaceOnUse');
pattern.setAttribute('patternTransform', 'rotate(0 90 90)');
pattern.setAttribute(null, "stroke", "black");
pattern.setAttribute(null, "stroke-width", 1);
pattern.setAttribute("transform", "translate(" + xPos + "," + yPos + ")");
//shape.append(pattern);
shape.setAttribute('fill', 'url(pattern)');
group.append(shape).append(pattern);

https://jsfiddle.net/nanadia/kzy722b9/3/ https://jsfiddle.net/nanadia/kzy722b9/3/

Try to define the pattern within the svg element and change the visibility upon clicking (sample code toget you started): 尝试在svg元素中定义模式,并在单击时更改可见性(示例代码可帮助您入门):

        <div id="svgselect" style="width: 610px; height: 230px;">
          <!-- background-color:red -->
          <svg version="1.1" height="100%" width="100%">
            <defs>
                <pattern id="pat" x="0" y="0" width="3" height="20" patternUnits="userSpaceOnUse" transform="scale(1.5) translate(35,0)">
                    <rect x="0" y="0" width="0.5" height="20"  stroke="black" stroke-width="1" />
                </pattern>
            </defs>
            <g transform="scale(1.5)" id="gmain">
              <g id="P17" transform="translate(25,0)">
                <polygon class="line" points="5,5  15,5  15,15   5,15" fill="white" stroke="navy" stroke-width="0.5" id="C" opacity="1"></polygon>
                <polygon class="line" points="0,0  20,0  15,5  5,5" fill="white" stroke="navy" stroke-width="0.5" id="T" opacity="1"></polygon>
                <polygon class="line B17" points="5,15   15,15   20,20   0,20" fill="white" stroke="navy" stroke-width="0.5" id="B" opacity="1"></polygon>
                <polygon class="line" points="15,5   20,0  20,20   15,15" fill="white" stroke="navy" stroke-width="0.5" id="R" opacity="1"></polygon>
                <polygon class="line" points="0,0  5,5   5,15  0,20" fill="white" stroke="navy" stroke-width="0.5" id="L" opacity="1"></polygon>
                <polygon class="line" points="0,0  0,20  20,20 20,0" fill="url(#pat)" stroke="black" stroke-width="2.5" id="inv" opacity="1" visibility="hidden" >

                </polygon>
                <text x="6" y="30" stroke="navy" fill="navy" stroke-width="0.1" style="font-size: 6pt;font-weight:normal">17</text>
              </g>
              <g id="P16" transform="translate(50,0)">
                <polygon class="line" points="5,5  15,5  15,15   5,15" fill="white" stroke="navy" stroke-width="0.5" id="C16" opacity="1"></polygon>
                <polygon class="line" points="0,0  20,0  15,5  5,5" fill="white" stroke="navy" stroke-width="0.5" id="T16" opacity="1"></polygon>
                <polygon class="line B16" points="5,15   15,15   20,20   0,20" fill="white" stroke="navy" stroke-width="0.5" id="B16" opacity="1"></polygon>
                <polygon class="line" points="15,5   20,0  20,20   15,15" fill="white" stroke="navy" stroke-width="0.5" id="R16" opacity="1"></polygon>
                <polygon class="line" points="0,0  5,5   5,15  0,20" fill="white" stroke="navy" stroke-width="0.5" id="L16" opacity="1"></polygon>
                <text x="6" y="30" stroke="navy" fill="navy" stroke-width="0.1" style="font-size: 6pt;font-weight:normal">16</text>
              </g>
              <g id="P15" transform="translate(75,0)">
                <polygon class="line" points="5,5  15,5  15,15   5,15" fill="white" stroke="navy" stroke-width="0.5" id="C15" opacity="1"></polygon>
                <polygon class="line" points="0,0  20,0  15,5  5,5" fill="white" stroke="navy" stroke-width="0.5" id="T15" opacity="1"></polygon>
                <polygon class="line B15" points="5,15   15,15   20,20   0,20" fill="white" stroke="navy" stroke-width="0.5" id="B15" opacity="1"></polygon>
                <polygon class="line" points="15,5   20,0  20,20   15,15" fill="white" stroke="navy" stroke-width="0.5" id="R15" opacity="1"></polygon>
                <polygon class="line" points="0,0  5,5   5,15  0,20" fill="white" stroke="navy" stroke-width="0.5" id="L15" opacity="1"></polygon>
                <text x="6" y="30" stroke="navy" fill="navy" stroke-width="0.1" style="font-size: 6pt;font-weight:normal">15</text>

              </g>
            </g>
          </svg>
        </div>

        <script>
$('polygon').click(function(evt) {
    $("#inv").attr("visibility", "visible");
});
        </script>

Live demo ( first square only ) 现场演示 (仅第一广场)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM