您好,我有一个脚本将事件源中的字符串记录到控制台。
因此,我以该字符串为例。(来自EventSource。它将在控制台中以这种方式记录...)
[[34,448],[33,444],[32,441],[32,439]]
所以每对都是X和Y值。
配对示例:[34,448]
(上面折线中的第一个X和Y对。)
因此,假设示例中有34,448
对。在这对之后,您可以看到33,444
。看看它如何立即在Y坐标上跳到448。无论如何,我可以让它包括该Y值之前的每个点吗?因此它将记录如下内容:
[[34,448],[34,447],[34,446],[34,445],[33,444],[32,443],[32,442],[32,441],[32,440],[32,439]]
那么增加点数吗? (这些点基本上构成了POLYLINE SVG。)
我的脚本:
xhr = new XMLHttpRequest();
xhr.open("GET", "http://MYSVG.com/svgtest.svg");
xhr.addEventListener("load", function() {
const xmlDoc = new DOMParser().parseFromString(
this.responseText.trim(),
"image/svg+xml"
);
// Get all polylines as strings
let lines = xmlDoc.getElementsByTagName('polyline');
// Loop over all lines
for(let line of lines) {
// --- OPTIONAL START ---
//(rgb() as output)
let stroke = line.style.stroke;
let size = line.style['stroke-width'];
let opacity = line.style.opacity;
//let size = line.style.strokewidth; -- Disabled
// --- OR ---
// Loop over all styles of this line (output same as input [hex])
for(let style of line.getAttribute('style').split(';')) {
// Get name & value
let valueOffset = style.indexOf(':');
// Check if name equal to 'stroke'
if(style.substr(0, valueOffset).trim() === 'stroke') {
// Save stroke value
stroke = style.substr(valueOffset + 1).trim();
// Break out of the loop (we don't have to search further)
var str = stroke;
var color = str.replace("#", "");
break;
}
}
console.log((JSON.stringify(line.getAttribute('points').split(' ').map(pair =>
pair.split(',').map(Number)))))
}
});
xhr.send();
console.log("Pars3 m0d3")