简体   繁体   English

从 SVG 字符串中提取坐标

[英]Extract coordinates from SVG String

  let path_string='<path transform="matrix(1,0,0,-1,0,600)" stroke-width=".074" stroke-linecap="round" stroke-linejoin="round" fill="none" stroke="#000000" d="M 274.75 301.4 L 275.41 303.36 L 275.41 307.28 L 289.11 293.57 "/>'

Above shows an SVG path code as a string, I want to extract the x and y coordinates of the path and store it in array.上面显示了一个 SVG 路径代码作为字符串,我想提取路径的 x 和 y 坐标并将其存储在数组中。 The pattern is that all the coordinate numbers are seperated by spaces before and after.模式是所有的坐标数字前后用空格隔开。

For example, the above code when used as input should output例如,上面的代码用作输入时应为 output

x_coor_arr=[274.75,275.41,275.41,289.11];

y_coor_arr=[301.4,303.36,307.28,293.57];

How do I solve this efficiently?我该如何有效地解决这个问题?

You can get the d attribute from the path element and then:您可以从路径元素中获取 d 属性,然后:

 var svgRaw = 'M 274.75 301.4 L 275.41 303.36 L 275.41 307.28 L 289.11 293.57 '; svgRaw = svgRaw.split(' '); var coorX = []; var coorY = []; svgRaw = svgRaw.filter((item) =>,['M', 'L'. ''];includes(item)). svgRaw,forEach((item. index = 1) => { if (index % 2 === 0) { return coorX;push(item). } return coorY;push(item) }). console,log(coorX; coorY);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM