html - 与矢量效果 : non-scaling-stroke 结合使用时,内联 SVG <circle> 在 Firefox 中中断

标签 html css svg

我维护的网站中有许多内联 SVG,但它们在 Firefox 中已损坏。当应用 vector-effect: non-scaling-stroke 时,每个都被破坏。它们仍然呈现,但最后一个/第一个点似乎被拉伸(stretch)到页面的左上角。

例如https://drive.google.com/file/d/1gY-SO2fbtDRb9AGfeLXvJg3oCKosdrKd/view?usp=sharing 例如https://drive.google.com/file/d/1lpua-qZyiDlcfFKjLfXWgP8wOa_tZh69/view?usp=sharing

如果我将标签更改为椭圆,这个问题似乎只适用于圆圈 例如

<ellipse class="stroke-blue stroke-2 no-fill stroke-rounded non-scaling-stroke" cx="69.9" cy="60.1" rx="40.8" ry="40.8"/>

问题消失了,如果我删除矢量效果也是如此

e.g. <circle class="stroke-blue stroke-2 no-fill stroke-rounded" cx="69.9" cy="60.1" r="40.8"/>

将鼠标光标移到 SVG 上有时也可以解决问题。

代码笔:https://codepen.io/anon/pen/WWQRqg

HTML:

  <svg version="1.1" id="parents-icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
       viewBox="0 0 200 200" style="enable-background:new 0 0 200 200;" xml:space="preserve">
    <path class="stroke-blue stroke-2 no-fill non-scaling-stroke" d="M116.5,137.6c0,0-17.5-22.6-46.7-22.6c-32,0-52,24.1-58.9,43.9c0,0,25.8,19.8,56.9,19.8c20.2,0,32.4-5,32.4-5"/>
    <path class="stroke-blue stroke-2 no-fill grey-stroke-hover non-scaling-stroke" d="M109.8,166.3c0,0,15.3,13.5,39.5,13.5s39.8-13.5,39.8-13.5S178.9,137,149.5,137S109.8,166.3,109.8,166.3z"/>
    <circle class="stroke-blue stroke-2 no-fill stroke-rounded grey-stroke-hover non-scaling-stroke" cx="149.5" cy="100.4" r="27.1"/>
    <circle class="stroke-blue stroke-2 no-fill stroke-rounded non-scaling-stroke" cx="69.9" cy="60.1" r="40.8"/>
  </svg>

CSS:

.non-scaling-stroke {
  vector-effect: non-scaling-stroke;
}

.stroke-2 {
    stroke-width: 2;
    stroke-miterlimit: 0;
}

.stroke-blue {
    stroke: #009bdf;
}

.no-fill {
    fill: none;
}

.stroke-rounded {
    stroke-linecap: round;
    stroke-linejoin: round;
}

最佳答案

显然,如果您使用路径而不是圆,则不会发生这种情况。此外,由于这是您此时的代码,因此您有一个重复的 id 用于 svg 元素。

在下一个示例中,我将使用一个函数来创建一个圆作为路径。如果您不想使用 javascript,您可以从检查器中获取路径的 d 属性。希望对您有所帮助。

const SVG_NS = "http://www.w3.org/2000/svg";



function drawCircle(cx, cy, r, parent) {
  let circle_path = document.createElementNS(SVG_NS, "path");
  let d = `M${cx + r},${cy} A${r},${r} 0 0 0 ${cx -
    r},${cy}  A${r},${r} 0 0 0 ${cx + r},${cy}z`;
  circle_path.setAttributeNS(null, "d", d);
  parent.appendChild(circle_path);
  return circle_path;
}


let circles = []

circles.push(drawCircle(149.5, 100.4, 27.1, circles1));
circles.push(drawCircle(69.9, 60.1, 40.8, circles1));
circles.push(drawCircle(149.5, 100.4, 27.1, circles2));
circles.push(drawCircle(69.9, 60.1, 40.8, circles2));

circles.map(c=>{
  c.setAttribute("class", "stroke-blue stroke-2 no-fill stroke-rounded grey-stroke-hover non-scaling-stroke")
})
/* Start the magic css for locking down svg stroke width */

.non-scaling-stroke {
  vector-effect: non-scaling-stroke;
}

/* End magic */

.blue-fill {
    fill: #009bdf;
}

.stroke-2 {
    stroke-width: 2;
    stroke-miterlimit: 0;
}
.stroke-blue {
    stroke: #009bdf;
}
.no-fill {
    fill: none;
}
.stroke-rounded {
    stroke-linecap: round;
    stroke-linejoin: round;
}
<div style="width: 200px">
  
                        <svg version="1.1" id="parents_icon1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
                             viewBox="0 0 200 200" style="enable-background:new 0 0 200 200;" xml:space="preserve">
                          <path class="stroke-blue stroke-2 no-fill non-scaling-stroke" d="M116.5,137.6c0,0-17.5-22.6-46.7-22.6c-32,0-52,24.1-58.9,43.9c0,0,25.8,19.8,56.9,19.8c20.2,0,32.4-5,32.4-5"/>
                          <path class="stroke-blue stroke-2 no-fill grey-stroke-hover non-scaling-stroke" d="M109.8,166.3c0,0,15.3,13.5,39.5,13.5s39.8-13.5,39.8-13.5S178.9,137,149.5,137S109.8,166.3,109.8,166.3z"/>
                          <!--<circle class="stroke-blue stroke-2 no-fill stroke-rounded grey-stroke-hover non-scaling-stroke" cx="149.5" cy="100.4" r="27.1"/>
                          <circle class="stroke-blue stroke-2 no-fill stroke-rounded non-scaling-stroke" cx="69.9" cy="60.1" r="40.8"/>-->
                        
  
<g id="circles1">  
</g>  
  
  
  
  </svg>
  
</div><div style="width: 200px">
  
                        <svg version="1.1" id="parents_icon2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
                             viewBox="0 0 200 200" style="enable-background:new 0 0 200 200;" xml:space="preserve">
                          <path class="stroke-blue stroke-2 no-fill non-scaling-stroke" d="M116.5,137.6c0,0-17.5-22.6-46.7-22.6c-32,0-52,24.1-58.9,43.9c0,0,25.8,19.8,56.9,19.8c20.2,0,32.4-5,32.4-5"/>
                          <path class="stroke-blue stroke-2 no-fill grey-stroke-hover non-scaling-stroke" d="M109.8,166.3c0,0,15.3,13.5,39.5,13.5s39.8-13.5,39.8-13.5S178.9,137,149.5,137S109.8,166.3,109.8,166.3z"/>
                          <!--<circle class="stroke-blue stroke-2 no-fill stroke-rounded grey-stroke-hover non-scaling-stroke" cx="149.5" cy="100.4" r="27.1"/>
                          <circle class="stroke-blue stroke-2 no-fill stroke-rounded non-scaling-stroke" cx="69.9" cy="60.1" r="40.8"/>-->
                        
  
<g id="circles2">  
</g>    
  </svg>
  
</div>

关于html - 与矢量效果 : non-scaling-stroke 结合使用时,内联 SVG <circle> 在 Firefox 中中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55488585/

相关文章:

jquery - 将元素覆盖在 CSS 中的副本上

html - Bootstrap 列中同一行的元素

html - 定位absolute并停留documentflow

使用 d3.js 的 javascript 极坐标散点图

javascript - Vue MultiSelect改变指针类显示

javascript - 将每次 slideToggle() 调用的边距更改 x%

CSS:文本输入边框的过渡效果

css - 多行文本使 svg 缩小

javascript - 图表未显示在 D3 中

html - IE 特定样式表打击其他版本的 IE