查找可修剪空白的svg视图框 [英] Find svg viewbox that trim whitespace around

查看:108
本文介绍了查找可修剪空白的svg视图框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个绘制某些路径的svg,我应该使用什么工具来找到一个完全适合这些路径的视图框,以便修剪掉周围的所有多余空间?

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?

推荐答案

您只需将svg的viewBox设置为其边界框即可.

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 {  border: 1px solid }

<svg version="1.1" id="svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 500 300" enable-background="new 0 0 500 300" xml:space="preserve" width="250" height="150">
  <path fill="none" stroke="#B2E230" d="M413.7,276.5c0,0,67-37,116,0c-24.1-33,16.4-53,0-34s-100-2-75-38" transform="translate(-75,-75)" />
</svg>
<button>setViewbox</button>

注意:它还将考虑路径的不可见锚点.

Note: It will also take into account the non-visible anchors of your pathes.

这篇关于查找可修剪空白的svg视图框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆