Internet Explorer 10 doesn't respect SVG text dominant-baseline attribute?

后端 未结 2 1844
野性不改
野性不改 2021-01-08 00:48

The following SVG file:




        
2条回答
  •  感动是毒
    2021-01-08 01:14

    As rockpiper answered, it is not supported in IE nor in Edge at the moment. However there are workarounds.

    One concept would be to calculate the difference between the offset to the parent of the bounding client rectangle (getBoundingClientRect) and the attribute y (which is mostly used for positioning).

    Then you can set dy for adjusting the vertical alignement.

    var parentElem = mySvg; // Set it to the closest SVG (or G) parent tag
    
    var y = myText.hasAttribute('y') ? myText.getAttribute('y') : 0;
    var dy = myText.hasAttribute('dy') ? myText.getAttribute('dy') : 0; 
    
    dy += y - (myText.getBoundingClientRect().top - parentElem.getBoundingClientRect().top);
    
    // This would cause to align it similarly to CSS 'vertical-align: top'
    myText.setAttribute('dy', dy);
    

    Depending on needs you can subtract myText.getBoundingClientRect().height for achieving another vertical alignement.

提交回复
热议问题