首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用类型记录解析SVG转换属性

用类型记录解析SVG转换属性
EN

Stack Overflow用户
提问于 2019-09-16 10:43:00
回答 1查看 1.3K关注 0票数 0

如何使用类型记录解析svg元素的transform属性?

也就是说,如何在svg.g.transform解析字符串中的所有数字和操作:

代码语言:javascript
复制
<svg viewBox="-40 0 150 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <g fill="grey"
     transform="rotate(-10 50 100)
                translate(-36 45.5)
                skewX(40)
                scale(1 0.5)">
    <path id="heart" d="M 10,30 A 20,20 0,0,1 50,30 A 20,20 0,0,1 90,30 Q 90,60 50,90 Q 10,60 10,30 z" />
  </g>

  <use xlink:href="#heart" fill="none" stroke="red"/>
</svg>
EN

回答 1

Stack Overflow用户

发布于 2019-09-16 10:43:00

使用https://developer.mozilla.org/en-US/docs/Web/API/SVGGraphicsElement aka。SVGLocatableSVGTransformable接口/API,由本机DOM元素实现。

这些元素具有一个与transform属性相对应的.transform属性。该属性的类型为https://developer.mozilla.org/en-US/docs/Web/API/SVGAnimatedTransformList,您希望查看静态定义的baseVal。

转换列表有一个属性numberOfItems和一个getItem方法。它可能具有.length属性和[]数组访问器,并且可以在浏览器中进行迭代,但不要指望它。

每个项目的类型为https://developer.mozilla.org/en-US/docs/Web/API/SVGTransform

.type属性告诉您使用了哪条指令。

因此,下面是如何再次解析并手动合成transform属性:

代码语言:javascript
复制
// javascript js equivalent declaration:
// function getAttributeTransform_js(nativeSVGElement) {
// typescript ts declaration
function getAttributeTransform_ts(nativeSVGElement: SVGGraphicsElement) {
  // this definition works in ts and js
  const tl = nativeSVGElement.transform.baseVal;
  const st = [];
  for (let i = 0; i < tl.numberOfItems; i++) {
    const t/*: SVGTransform*/ = tl.getItem(i);
    switch (t.type) {
      case SVGTransform.SVG_TRANSFORM_UNKNOWN: break;
      case SVGTransform.SVG_TRANSFORM_MATRIX: {
        // A matrix(…) transformation
        // Note: this is the most general transformation, capable of representing more transformations than the other combined.
        // For SVG_TRANSFORM_MATRIX, the matrix contains the a, b, c, d, e, f values supplied by the user.
        //
        // Note: instead of comma (,), whitespace separation would also be allowed
        st.push(`matrix(${t.matrix.a}, ${t.matrix.b}, ${t.matrix.c}, ${t.matrix.d}, ${t.matrix.e}, ${t.matrix.f})`);
        break;
      }
      case SVGTransform.SVG_TRANSFORM_TRANSLATE: {
        // A translate(…) transformation
        // For SVG_TRANSFORM_TRANSLATE, e and f represent the translation amounts (a=1, b=0, c=0 and d=1).
        st.push(`translate(${t.matrix.e}, ${t.matrix.f})`);
        break;
      }
      case SVGTransform.SVG_TRANSFORM_SCALE: {
        // A scale(…) transformation
        // For SVG_TRANSFORM_SCALE, a and d represent the scale amounts (b=0, c=0, e=0 and f=0).
        st.push(`scale(${t.matrix.a}, ${t.matrix.d})`);
        break;
      }
      case SVGTransform.SVG_TRANSFORM_ROTATE: {
        // A rotate(…) transformation
        // For SVG_TRANSFORM_ROTATE, a, b, c, d, e and f together represent the matrix which will result in the given rotation.
        // When the rotation is around the center point (0, 0), e and f will be zero.
        /*
        angle   float   A convenience attribute for SVG_TRANSFORM_ROTATE, SVG_TRANSFORM_SKEWX and SVG_TRANSFORM_SKEWY. It holds the angle that was specified.

        For SVG_TRANSFORM_MATRIX, SVG_TRANSFORM_TRANSLATE and SVG_TRANSFORM_SCALE, angle will be zero.
        */
        /*
        This is the hardest case since the origin information is lost!
        We need to recompute it from the matrix.
        from https://math.stackexchange.com/questions/2093314/rotation-matrix-of-rotation-around-a-point-other-than-the-origin

        matrix.a = cos_angle = c;
        matrix.b = sin_angle = s;
        Note that by the laws of geometry: c^2+s^2 = 1 (c and s are coordinates on the unit circle)
        matrix.e = -x*c + y*s + x;
        matrix.f = -x*s - y*c + y;

        Using Mathematica/Wolfram Language:
        "Assuming[c^2+s^2==1,Solve[e == -x*c + y*s + x&& f == -x*s - y*c + y,{x,y},Reals]//Simplify]//InputForm"
        (you can use WL for free here: https://develop.wolframcloud.com/objects/c26e16f7-44e7-4bb6-81b3-bc07782f9cc5)
        {{x -> (e + (f*s)/(-1 + c))/2, y -> (f - c*f + e*s)/(2 - 2*c)}}
        */
        const e = t.matrix.e, f = t.matrix.f, c = t.matrix.a, s = t.matrix.b;
        const originx = (e + (f*s)/(-1 + c))/2;
        const originy = (f - c*f + e*s)/(2 - 2*c);
        st.push(`rotate(${t.angle}, ${originx}, ${originy})`);
        break;
      }
      case SVGTransform.SVG_TRANSFORM_SKEWX: {
        // A skewx(…) transformation
        // For SVG_TRANSFORM_SKEWX and SVG_TRANSFORM_SKEWY, a, b, c and d represent the matrix which will result in the given skew (e=0 and f=0).
        /*
        angle   float   A convenience attribute for SVG_TRANSFORM_ROTATE, SVG_TRANSFORM_SKEWX and SVG_TRANSFORM_SKEWY. It holds the angle that was specified.

        For SVG_TRANSFORM_MATRIX, SVG_TRANSFORM_TRANSLATE and SVG_TRANSFORM_SCALE, angle will be zero.
        */
        st.push(`skewx(${t.angle})`);
        break;
      }
      case SVGTransform.SVG_TRANSFORM_SKEWY: {
        // A skewy(…) transformation
        // For SVG_TRANSFORM_SKEWX and SVG_TRANSFORM_SKEWY, a, b, c and d represent the matrix which will result in the given skew (e=0 and f=0).
        /*
        angle   float   A convenience attribute for SVG_TRANSFORM_ROTATE, SVG_TRANSFORM_SKEWX and SVG_TRANSFORM_SKEWY. It holds the angle that was specified.

        For SVG_TRANSFORM_MATRIX, SVG_TRANSFORM_TRANSLATE and SVG_TRANSFORM_SCALE, angle will be zero.
        */
        st.push(`skewy(${t.angle})`);
        break;
      }
    }
  }
  return st.join(','); // instead of comma (,), whitespace separation is also allowed
}
代码语言:javascript
复制
// example
const r = <SVGRectElement>document.createElementNS("http://www.w3.org/2000/svg", "rect");

// the parseable syntax for the transform attribute is pretty relaxed
r.setAttribute("transform", "translate(1, 0),rotate(0.5),   scale(1 2)");

// note that the browser may canonicalize your syntax
// EDGE canonicalizes the transform to read:
// 'translate(1) rotate(0.5) scale(1, 2)'
console.log(r.getAttribute("transform"));

// basically equivalent:
console.log(getAttributeTransform_ts(r));

你的例子:

代码语言:javascript
复制
function createElementFromHTML(htmlString) {
  var div = document.createElement('div');
  div.innerHTML = htmlString.trim();

  // Change this to div.childNodes to support multiple top-level nodes
  return div.firstChild; 
}

getAttributeTransform_ts(createElementFromHTML(`
<g fill="grey"
     transform="rotate(-10 50 100)
                translate(-36 45.5)
                skewX(40)
                scale(1 0.5)">
    <path id="heart" d="M 10,30 A 20,20 0,0,1 50,30 A 20,20 0,0,1 90,30 Q 90,60 50,90 Q 10,60 10,30 z" />
  </g>
`))

// gives
// 'rotate(-10, 49.99999999999982, 99.99999999999972),translate(-36, 45.5),skewx(40),scale(1, 0.5)'

请注意,您应该使用.getAttribute("transform")来让浏览器为您合成SVGTransformList的字符串形式,而不是使用上面的脚本!

请注意,我们不能完美地检索原始参数“旋转”,因为它没有API。它必须由2d齐次(旋转)矩阵计算。

灵感来源于:

另请参阅:

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57955159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档