Hi, I am using haxe to create SVGs in the JS target, and wanted to make sure I am not missing out on a better way to do this. Essentially I have this helper function that uses document.createElementNS of the type of SVG Element I am looking to create:
//https://stackoverflow.com/questions/20539196/creating-svg-elements-dynamically-with-javascript-inside-html
// using this to be able to create svg elements with code
static function createSVGElement(n):SVGElement
{
return cast(document.createElementNS("http://www.w3.org/2000/svg", n));
}
and using that method like this:
var svg:SVGElement = createSVGElement("svg");
svg.width.baseVal.valueAsString = "100px";
svg.height.baseVal.valueAsString = "100px";
var line:LineElement = cast(createSVGElement("line"));
line.x1.baseVal.valueAsString = "20%";
line.x2.baseVal.valueAsString = "20%";
line.y1.baseVal.valueAsString = "10%";
line.y2.baseVal.valueAsString = "35%";
line.classList.add("test-line-2");
svg.appendChild(line);
// some place doing an HTMLELEMENT.appendChild(svg);
This is working without issues, but making sure there isn’t a better way, specifically the “cast(createSVGElement(“line”))” bit