Supposing we have an SVG transform string:
transform = "translate(6,5),scale(3,3)";
Is there a slick regex function we could use to parse that into something usable?
Supposing we have an SVG transform string:
transform = "translate(6,5),scale(3,3)";
Is there a slick regex function we could use to parse that into something usable?
Here's a nifty little snippet of code that might get you what you need, it should cover most scenario in case the string you intend to parse has different number of arguments:
function parse (a)
{
var b={};
for (var i in a = a.match(/(\w+\((\-?\d+\.?\d*e?\-?\d*,?)+\))+/g))
{
var c = a[i].match(/[\w\.\-]+/g);
b[c.shift()] = c;
}
return b;
}
Running this
parse('translate(6,5),scale(3,3.5),a(1,1),b(2,23,-34),c(300)');
Will result in this:
{
translate: [ '6', '5' ],
scale: [ '3', '3.5' ],
a: [ '1', '1' ],
b: [ '2', '23', '-34' ],
c: [ '300' ]
}
Assuming you are running in a browser, you could also use the SVG DOM to parse and decode the transform string.
var svgns = "http://www.w3.org/2000/svg";
function getTransformList(transformStr)
{
// Create a fake <rect> element to set the transform to
var rect = document.createElementNS(svgns, "rect");
rect.setAttribute("transform", transformStr);
// Use the DOM to get the list of decoded transforms
dumpTransform(rect.transform.baseVal);
}
function dumpTransform(transformList)
{
for (var i = 0; i < transformList.numberOfItems; i++)
{
var transform = transformList.getItem(i);
var m = transform.matrix;
switch (transform.type)
{
case 2:
console.log("translate("+m.e+","+m.f+")");
break;
case 3:
console.log("scale("+m.a+","+m.d+")");
break;
case 4:
console.log("rotate("+transform.angle+")");
// TODO need to also handle rotate(angle, x, y) form
break;
case 5:
// TODO skewX()
break;
case 6:
// TODO skewY(()
break;
case 1:
default:
console.log("matrix("+m.a+","+m.b+","+m.c+","+m.d+","+m.e+","+m.f+")");
break;
}
}
}
getTransformList("translate(6,5),scale(3,3)");
Adapted from @chernjie's solution:
function parse_transform(a) {
var b = {};
for (var i in a = a.match(/(\w+)\(([^,)]+),?([^)]+)?\)/gi)) {
var c = a[i].match(/[\w\.\-]+/g);
b[c.shift()] = c;
}
return b;
}
var transform = "translate(6,5),scale(3,3)";
var translate = /translate\(\s*([^\s,)]+)[ ,]([^\s,)]+)/.exec(transform);
var translateX = translate[1]
var translateY = translate[2]
var scale = /scale\(\s*([^\s,)]+)[ ,]([^\s,)]+)/.exec(transform);
var scaleX = translate[1]
var scaleY = translate[2]
transformValues = {translate:{x:translateX,y:translateY},
scale:{x:scaleX,y:scaleY}}
Pretty gross, but @icedwater was making me do all the work myself so...
I'll take a stab at this and say you want it to be parsed into a JSON object containing each of the properties as an entry in a list. This is the closest I have come so far:
function listIntoJSON(theTransformInQuestion) {
// let's work with transform = "translate(6,5),scale(3,3)" as specified.
var result = []; // empty list to be returned
var splitList = theTransformInQuestion.split(/\W/);
// now splitList is ["translate", "6", "5", "", "scale", "3", "3", ""]
for (var t = 0; t < splitList.length; t += 4) {
var op = {};
op.name = splitList[t];
op.x = splitList[t + 1];
op.y = splitList[t + 2];
result.push(op); // add op to the result list
}
return result;
}
which will take
transform = "translate(6,5),scale(3,3)";
and return
[
{name: "translate", x: "6", y: "5"},
{name: "scale", x: "3", y: "3"}
]
and should work for any number of transforms. Bit of a hack, but it will do for now.
even it's an old question, today I had the same need and ended up with this regex:
const regex = /((\w+)\((,?\s*-?\d*\.?\d+(px|in|px|cm|mm|pt|pc|em|ex|ch|rem|deg|rad|grad|turn)?)+\)\s*)/gi;
Quite long, but it takes into account unit of measure too.
You can test it here: https://regex101.com/r/M8rRjo/1/
It's not regex, but there is a similar question (Replacing d3.transform in D3 v4) with a very nice, comprehensive solution:
function getTransformation(transform) {
// Create a dummy g for calculation purposes only. This will never
// be appended to the DOM and will be discarded once this function
// returns.
var g = document.createElementNS("http://www.w3.org/2000/svg", "g");
// Set the transform attribute to the provided string value.
g.setAttributeNS(null, "transform", transform);
// consolidate the SVGTransformList containing all transformations
// to a single SVGTransform of type SVG_TRANSFORM_MATRIX and get
// its SVGMatrix.
var matrix = g.transform.baseVal.consolidate().matrix;
// Below calculations are taken and adapted from the private function
// transform/decompose.js of D3's module d3-interpolate.
var {a, b, c, d, e, f} = matrix; // ES6, if this doesn't work, use below assignment
// var a=matrix.a, b=matrix.b, c=matrix.c, d=matrix.d, e=matrix.e, f=matrix.f; // ES5
var scaleX, scaleY, skewX;
if (scaleX = Math.sqrt(a * a + b * b)) a /= scaleX, b /= scaleX;
if (skewX = a * c + b * d) c -= a * skewX, d -= b * skewX;
if (scaleY = Math.sqrt(c * c + d * d)) c /= scaleY, d /= scaleY, skewX /= scaleY;
if (a * d < b * c) a = -a, b = -b, skewX = -skewX, scaleX = -scaleX;
return {
translateX: e,
translateY: f,
rotate: Math.atan2(b, a) * 180 / Math.PI,
skewX: Math.atan(skewX) * 180 / Math.PI,
scaleX: scaleX,
scaleY: scaleY
};
}
console.log(getTransformation("translate(20,30)"));
console.log(getTransformation("rotate(45) skewX(20) translate(20,30) translate(-5,40)"));