HTML5 inline SVG autocrop whitespace

后端 未结 1 1614
盖世英雄少女心
盖世英雄少女心 2021-02-09 00:59

I\'ve got an SVG diagram (complicated a little) that is generated using php and some data from the database. In that case, I can\'t predict the length of labels on the svg and a

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-09 01:12

    Building off this answer and the above comments:

    var svg = document.getElementsByTagName("svg")[0];
    var bbox = svg.getBBox();
    var viewBox = [bbox.x, bbox.y, bbox.width, bbox.height].join(" ");
    svg.setAttribute("viewBox", viewBox);
    

    If you really need a non-JS solution: convert to PNG (or any other lossless format), then use trim to lop off the whitespace. You can do this from within PHP.

    0 讨论(0)
提交回复
热议问题