简体   繁体   English

在 html 中调整 SVG 的大小?

[英]Resizing SVG in html?

So, I have an SVG file in HTML, and one of the things I've heard about the format is that it doesn't get all pixelated when you zoom in on it.所以,我有一个 HTML 格式的 SVG 文件,我听说过关于这种格式的一件事是,当你放大它时,它不会全部像素化。

I know with a jpeg or whatever I could have it stored as a 50 by 50 icon, then actually display it as a (rather pixelated) 100 by 100 thumbnail (or 10 by 10), by manually setting the height and width in the image_src tag.我知道使用 jpeg 或任何可以将它存储为 50 x 50 图标的东西,然后通过手动设置 image_src 中的高度和宽度,将其实际显示为(相当像素化)100 x 100 缩略图(或 10 x 10)标签。

However, SVG files seem to be used with object/embed tags, and changing the height or width of THOSE just results in more space being allocated for the picture.但是,SVG 文件似乎与 object/embed 标签一起使用,并且更改 THOSE 的高度或宽度只会导致为图片分配更多空间。

IS there any way to specify that you want an SVG image displayed smaller or larger than it actually is stored in the file system?有什么方法可以指定您希望显示的 SVG 图像比实际存储在文件系统中的尺寸更小或更大吗?

Open your .svg file with a text editor (it's just XML), and look for something like this at the top:使用文本编辑器(它只是 XML)打开.svg文件,然后在顶部查找类似的内容:

<svg ... width="50px" height="50px"...

Erase width and height attributes;擦除宽高属性; the defaults are 100%, so it should stretch to whatever the container allows it.默认值是 100%,所以它应该延伸到容器允许的任何地方。

Try these:试试这些:

  1. Set the missing viewbox and fill in the height and width values of the set height and height attributes in the svg tag设置缺失的viewbox并在svg标签中填写设置的高度和高度属性的高度和宽度值

  2. Then scale the picture simply by setting the height and width to the desired percent values.然后通过将高度和宽度设置为所需的百分比值来缩放图片。 Good luck.祝你好运。

  3. Set a fixed aspect ratio with preserveAspectRatio="X200Y200 meet (eg 200px), but it's not necessary使用preserveAspectRatio="X200Y200 meet (eg 200px) 设置固定纵横比,但这不是必需的

eg例如

 <svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:xlink="http://www.w3.org/1999/xlink"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   width="10%" 
   height="10%"
   preserveAspectRatio="x200Y200 meet"
   viewBox="0 0 350 350"
   id="svg2"
   version="1.1"
   inkscape:version="0.48.0 r9654"
   sodipodi:docname="namesvg.svg">

您可以通过在图像标签和大小图像标签中显示 svg 来调整它的大小,即

<img width="200px" src="lion.svg"></img>

I have found it best to add viewBox and preserveAspectRatio attributes to my SVGs.我发现最好将viewBoxpreserveAspectRatio属性添加到我的 SVG 中。 The viewbox should describe the full width and height of the SVG in the form 0 0 wh :视图框应该以0 0 wh的形式描述 SVG 的全宽和全高:

<svg preserveAspectRatio="xMidYMid meet" viewBox="0 0 700 550"></svg>

Use the following code:使用以下代码:

<g transform="scale(0.1)">
...
</g>

Changing the width of the container also fixes it rather than changing the width and height of source file.更改容器的宽度也会修复它,而不是更改源文件的宽度和高度。

.SvgImage img{ width:80%; }

This fixes my issue of re sizing svg .这解决了我调整 svg 大小的问题。 you can give any % based on your requirement.您可以根据您的要求提供任何百分比。

Here is an example of getting the bounds using svg.getBox() : https://gist.github.com/john-doherty/2ad94360771902b16f459f590b833d44这是使用svg.getBox()获取边界的示例: https : svg.getBox()

At the end you get numbers that you can plug into the svg to set the viewbox properly.最后,您将获得可以插入 svg 以正确设置视图框的数字。 Then use any css on the parent div and you're done.然后在父 div 上使用任何 css 就完成了。

 // get all SVG objects in the DOM
 var svgs = document.getElementsByTagName("svg");
 var svg = svgs[0],
    box = svg.getBBox(), // <- get the visual boundary required to view all children
    viewBox = [box.x, box.y, box.width, box.height].join(" ");

    // set viewable area based on value above
    svg.setAttribute("viewBox", viewBox);

I have an SVG file in HTML [....] IS there any way to specify that you want an SVG image displayed smaller or larger than it actually is stored in the file system?我有一个 HTML 格式的 SVG 文件 [....] 有什么方法可以指定您希望显示的 SVG 图像比实际存储在文件系统中的尺寸更小或更大?

SVG graphics, like other creative works, are protected under copyright law in most countries. SVG 图形与其他创意作品一样,在大多数国家/地区都受版权法保护。 Depending on jurisdiction, license of the work or whether or not you are the copyright holder you may not be able to modify the SVG without violating copyright law , believe it or not.根据管辖权、作品的许可或您是否是版权所有者,您可能无法在不违反版权法的情况下修改 SVG,信不信由你。

But laws are tricky topics and sometimes you just want to get shit done.但法律是棘手的话题,有时你只想把事情做好。 Therefore you may adjust the scale of the graphic without modifying the work itself using the img tag with width attribute within your HTML.因此,您可以在 HTML 中使用带有width属性的img标签调整图形的比例,而无需修改作品本身。

Using an external HTTP request to specify the size:使用外部 HTTP 请求指定大小:

<img width="96" src="/path/to/image.svg">

Specifying size in markup using a Data URI :使用数据 URI在标记中指定大小:

<img width="96" src="data:image/svg+xml,...">

SVGs can be Optimized for Data URIs to create SVG Favicon images suitable for any size:可以针对数据 URI 优化SVG以创建适合任何尺寸的SVG Favicon图像:

<link rel="icon" sizes="any" href="data:image/svg+xml,%3Csvg%20viewBox='0%200%2046%2045'%20xmlns='http://www.w3.org/2000/svg'%3E%3Ctitle%3EAfter%20Dark%3C/title%3E%3Cpath%20d='M.708%2045L23%20.416%2045.292%2045H.708zM35%2038L23%2019%2011%2038h24z'%20fill='%23000'/%3E%3C/svg%3E">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM