viewbox

Find svg viewbox that trim whitespace around

前提是你 提交于 2019-11-26 17:47:44
问题 Assuming I have an svg that draws some paths, what tools should I use to find a viewbox that perfectly fits those paths so that all redundant space around is trimmed? 回答1: You can simply set your svg's viewBox to its Bounding Box. function setViewbox(svg) { var bB = svg.getBBox(); svg.setAttribute('viewBox', bB.x + ',' + bB.y + ',' + bB.width + ',' + bB.height); } document.querySelector('button').addEventListener('click', function() { setViewbox(document.querySelector('svg')); }); svg {