Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically created SVG not working in Firefox, but works in Chrome

I am trying to figure out why this code of is not working in Firefox. It's supposed to create horizontal paths, but I cannot see them in Firefox. Chrome and IE showing them properly. What could be the issue?

https://jsfiddle.net/7a6qm371/

<div>
<svg width="100%" height="500" id="svgBottomWall">
    <g style="stroke: aqua; fill: none;" id="svgBottomWallGridGroup"></g>
</svg>

$(document).ready(function () {

var svgBottomWall = document.getElementById("svgBottomWall");
var rect = svgBottomWall.getBoundingClientRect();
var svgW = rect.width;



function createHorizontalLine(w, d) {
    var nline = document.createElementNS("http://www.w3.org/2000/svg", "path");
    nline.setAttribute("d", "M 0 " + d + ", L " + w + " " + d);
    nline.setAttribute("stroke-width", 1);
    nline.setAttribute("stroke", "#ffffff");
    document.getElementById("svgBottomWallGridGroup").appendChild(nline);
}
for (var i = 0; i <= svgW; i = i + 100) {
    createHorizontalLine(svgW, i);
}
});
like image 276
JSDriller Avatar asked Oct 20 '22 15:10

JSDriller


1 Answers

Your d path parameters are incorrectly formatted.

You have something like

d="M 0 100, L 1000 100"

whereas it should be

d="M 0,100 L 1000,100"

See https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d

The fix is

nline.setAttribute("d", "M 0," + d + " L " + w + "," + d);

JSFiddle

like image 151
Phil Avatar answered Oct 27 '22 09:10

Phil