本节一个压缩SVG的工具:svgo(https://github.com/chalecao/svgo)。我们做前端的同学肯定都用过字体图标,有些特殊的字体图标需要视觉做个svg的矢量图,或者你也可以自己做个矢量图,需要学学adobe illustrator,绘制个矢量图,然后保存成svg格式就可以了。然后用记事本打开保存的svg文件,你会发现有许多没有用的svg标签,这时候你需要一个svg压缩工具,可以帮你做这些事情。
SVG系列教程
SVG压缩
其实我的好奇心来自于goole的这个图片,这个是DATA URI, 单独用浏览器打开,发现是个svg,编码如下:
- data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNzIgOTIiIHdpZHRoPSIyNzIiIGhlaWdodD0iOTIiPjxwYXRoIGZpbGw9IiNFQTQzMzUiIGQ9Ik0xMTUuNzUgNDcuMThjMCAxMi43Ny05Ljk5IDIyLjE4LTIyLjI1IDIyLjE4cy0yMi4yNS05LjQxLTIyLjI1LTIyLjE4QzcxLjI1IDM0LjMyIDgxLjI0IDI1IDkzLjUgMjVzMjIuMjUgOS4zMiAyMi4yNSAyMi4xOHptLTkuNzQgMGMwLTcuOTgtNS43OS0xMy40NC0xMi41MS0xMy40NFM4MC45OSAzOS4yIDgwLjk5IDQ3LjE4YzAgNy45IDUuNzkgMTMuNDQgMTIuNTEgMTMuNDRzMTIuNTEtNS41NSAxMi41MS0xMy40NHoiLz48cGF0aCBmaWxsPSIjRkJCQzA1IiBkPSJNMTYzLjc1IDQ3LjE4YzAgMTIuNzctOS45OSAyMi4xOC0yMi4yNSAyMi4xOHMtMjIuMjUtOS40MS0yMi4yNS0yMi4xOGMwLTEyLjg1IDkuOTktMjIuMTggMjIuMjUtMjIuMThzMjIuMjUgOS4zMiAyMi4yNSAyMi4xOHptLTkuNzQgMGMwLTcuOTgtNS43OS0xMy40NC0xMi41MS0xMy40NHMtMTIuNTEgNS40Ni0xMi41MSAxMy40NGMwIDcuOSA1Ljc5IDEzLjQ0IDEyLjUxIDEzLjQ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4yM2MtMS4wOS0yLjc3LTQuMzctNC43LTguMjMtNC43LTQuOTUgMC0xMS44NCA0LjM3LTExLjU5IDEyLjkzeiIvPjxwYXRoIGZpbGw9IiM0Mjg1RjQiIGQ9Ik0zNS4yOSA0MS40MVYzMkg2N2MuMzEgMS42NC40NyAzLjU4LjQ3IDUuNjggMCA3LjA2LTEuOTMgMTUuNzktOC4xNSAyMi4wMS02LjA1IDYuMy0xMy43OCA5LjY2LTI0LjAyIDkuNjZDMTYuMzIgNjkuMzUuMzYgNTMuODkuMzYgMzQuOTEuMzYgMTUuOTMgMTYuMzIuNDcgMzUuMy40N2MxMC41IDAgMTcuOTggNC4xMiAyMy42IDkuNDlsLTYuNjQgNi42NGMtNC4wMy0zLjc4LTkuNDktNi43Mi0xNi45Ny02LjcyLTEzLjg2IDAtMjQuNyAxMS4xNy0yNC43IDI1LjAzIDAgMTMuODYgMTAuODQgMjUuMDMgMjQuNyAyNS4wMyA4Ljk5IDAgMTQuMTEtMy42MSAxNy4zOS02Ljg5IDIuNjYtMi42NiA0LjQxLTYuNDYgNS4xLTExLjY1bC0yMi40OS4wMXoiLz48L3N2Zz4=
DATA-URI的格式为:
data: [][; base64],
例如:
一个红色五角星形状的内联图片可以定义为:
- < img alt = " red star " src = " data: image /gif; base64,
- R01GOD1hDAAMALMLAPN8ffBiYvWWlvrKy /FvcPewso9VVfajo +
- w6O/zl5estLv /8 /AAAAAAAAAAAAAAAACH5BAEAAAs
- ALAAAAAAMAAwAAAQzcElZyryTEHyTUgknHd9xGV + qKsYirKkwDYiKDBiatt2
- H1KBLQRFIJAIKywRgmhwAIlEEADs =" >
所以把后面的一长串用base64解码之后呢,就得到:
于是乎,我有了一个新的思路,以后用svg做字体图标就更方便了,只要把svg图标转一下成data uri,然后字体就可以用这个img图片或者背景图。不过需要考虑通用性,如果用图片,那么每个地方都写一遍这个dataURI,明显是不合适的。如果用背景图片,那么控制大小颜色就不太方便。所以这个方法还是有问题。呵呵,我又把自己打败了。
压缩svg
做这些事情之前呢,就是先用 illustrator制作矢量图,探后保存成svg,然后转成data uri。为了保证data uri不会太大,所以需要压缩处理一下svg格式的文件,去除没有用的数据。
本站欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明: 文章转载自:爱思资源网 http://www.aseoe.com/show-80-911-1.html
转载请注明: 文章转载自:爱思资源网 http://www.aseoe.com/show-80-911-1.html