Существуют различные проблемы с вашим кодом, например, определение url
дважды.Вы, вероятно, хотите использовать fetch
API, чтобы получить его.Вам нужно будет запустить свой код на colorillo.com или переназначить файл на вашем собственном сервере, потому что они не настроили CORS, чтобы позволить вам получить доступ к файлу с другого сайта.
SVG - это диалектXML, а не JSON.Вам нужно использовать DOMParser:
// Create request to fetch the SVG file.
xhr=new XMLHttpRequest();
// Tell the request where the file is.
xhr.open("GET", "http://colorillo.com/bxys.inline.svg");
// Add event handler to process the file once it's been fetched.
xhr.addEventListener("load", function() {
// Once the text is available, create an XML parser
// and parse the text as an SVG image.
const xmlDoc = new DOMParser().parseFromString(
this.responseText.trim(),
"image/svg+xml"
);
// xmlDoc.getElements() returns something Array-like, but not an Array.
// This turns it into an Array.
const polylines = Array.from(xmlDoc.getElementsByTagName('polyline'));
console.log(polylines.map(
pl => [
// Parses each 'points' attribute into an array of pairs of numbers
pl.getAttribute('points').split(' ').map(
pair => pair.split(',').map(x=>+x)
),
// Various stroke information
// Convert rgb(R,G,B) to #RRGGBB
// Leading hash, then pull out the digits with a regex
'#' + pl.style.stroke.match(/rgb\((\d*), (\d*), (\d*)\)/)
// Throw away everything but the digits
.slice(1,4)
// Convert to a number, render in hex, uppercase, pad with 0s
.map(x=>(+x).toString(16).toUpperCase().padStart(2,'0'))
// Concatenate the hex digits
.join(''),
+pl.style.strokeWidth,
pl.style.strokeLinejoin,
pl.style.strokeLinecap
]
));
});
xhr.send();