使用javascript解析SVG变换属性

时间:2013-07-24 02:14:43

标签: javascript regex svg

假设我们有一个SVG转换字符串:

transform = "translate(6,5),scale(3,3)";

我们可以使用一个灵活的正则表达式函数将其解析成可用的东西吗?

6 个答案:

答案 0 :(得分:14)

这里有一段很棒的代码片段,可以为您提供所需的代码,如果您打算解析的字符串具有不同数量的参数,它应涵盖大多数情况:

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;
}

运行此

parse('translate(6,5),scale(3,3.5),a(1,1),b(2,23,-34),c(300)');

会导致这个:

{
    translate: [ '6', '5' ],
    scale: [ '3', '3.5' ],
    a: [ '1', '1' ],
    b: [ '2', '23', '-34' ],
    c: [ '300' ]
}

答案 1 :(得分:4)

假设您在浏览器中运行,您还可以使用SVG DOM来解析和解码转换字符串。



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.length; i++)
  {
    var transform = transformList[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)");
&#13;
&#13;
&#13;

答案 2 :(得分:3)

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}}

非常粗暴,但@icedwater让我自己完成所有工作......

答案 3 :(得分:3)

改编自@ chernjie的解决方案:

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;
}

答案 4 :(得分:1)

我将对此进行一次尝试,并表示您希望将其解析为包含每个属性作为列表中条目的JSON对象。这是我到目前为止最接近的:

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;
}

将采取

transform = "translate(6,5),scale(3,3)";

并返回

[
    {name: "translate", x: "6", y: "5"}, 
    {name: "scale", x: "3", y: "3"}
]

并且应该适用于任意数量的转换。一点点黑客,但它现在会做。

答案 5 :(得分:0)

即使是一个老问题,今天我也有同样的需求,并最终得到了这个正则表达式:

const regex = /((\w+)\((,?\s*-?\d*\.?\d+(px|in|px|cm|mm|pt|pc|em|ex|ch|rem|deg|rad|grad|turn)?)+\)\s*)/gi;

很长,但是它也考虑了度量单位。

您可以在这里进行测试:https://regex101.com/r/M8rRjo/1/