How to align a group (instead of text) along a path in svg?

前端 未结 3 790
半阙折子戏
半阙折子戏 2021-01-07 10:06

Lets assume I have programmatically created a circle and some text, that I want to align along the circle. I am able to do so usin

3条回答
  •  醉话见心
    2021-01-07 11:06

    As a starting point here is the workaround I mentioned. It uses some invisible placeholder text and extracts the transformation information with

    placeHolder.getBoundingClientRect();
    

    and

    placeHolder.getRotationOfChar(0);
    

    The result is only an approximation that depends on the font size and on the character of the placeholder.

    var svg = document.getElementById('svg');
    var text = document.createElement('text');
    text.setAttribute('fill','black');
    svg.appendChild(text);
    
    var placeHolder = document.getElementById('placeholder');
    var placeHolderBounds = placeHolder.getBoundingClientRect();
    var angle = placeHolder.getRotationOfChar(0);
    
    var group = document.getElementById('my-group');
    var groupBounds = group.getBoundingClientRect();
    var dx = 0; //groupBounds.width/2;   
    var dy = -groupBounds.height;   
    var x = placeHolderBounds.x;
    var y = placeHolderBounds.y;
    
    var transform = 'translate(' + x +','+ y +') '+ 
                     'rotate('+ angle +') '+ 
                     'translate('+ dx +','+ dy +')';
    group.setAttribute('transform', transform);
    
    
    
     
    
      
    
    
    
    
        
          Hello World-
        
      
    

提交回复
热议问题