Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  83] [ 2]  / answers: 1 / hits: 15916  / 11 Years ago, thu, november 7, 2013, 12:00:00

I want to replace the coordinates within a SVG path with variables. (with javascript). While svg path could be of many types it would be of great help to get some support on a concrete example:



d = M27,0C27,21,4,34,-13,23C22,18,-27,9,-27,0;


I want this SVG path to be transformed into



var x = [];
var x[0] = 27; x[1] = ...

d = M + x[0] + , + y[0]
+ C
+ x[0] + , + y[0] + ,
+ x[1] + , + y[1] + ,
+ x[2] + , + y[2] + ,
+ C
+ x[3] + , + y[3] + ,
+ x[4] + , + y[4] + ,
+ x[5] + , + y[5];


So my problem is to find the proper javascript RegExp to extract all the variables and by use of it generate the SVG path as given.



What I actually do is creating a Javascript object representing a given svg and I want to able to set the coordinates individually.



Any help highly appreciated



thx, martin



UPDATE:
next to the accepted answer there is also a very handy method in the wonderful raphael.js library to analyze a SVG path



http://raphaeljs.com/reference.html#Raphael.parsePathString


More From » regex

 Answers
46

Just use the SVG DOM to parse it there are more details in this question/answer but basically you do



var segments = path.pathSegList;


and that gives you an array of segments and values you can read or write e.g.



segments.getItem(0).y = -10;

[#74440] Wednesday, November 6, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
taliac

Total Points: 84
Total Questions: 114
Total Answers: 114

Location: Morocco
Member since Fri, May 22, 2020
4 Years ago
;