Resizing SVG in html?

后端 未结 8 1411
慢半拍i
慢半拍i 2020-12-04 05:25

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.

I know with a jpeg o

相关标签:
8条回答
  • 2020-12-04 06:27

    you can resize it by displaying svg in image tag and size image tag i.e.

    <img width="200px" src="lion.svg"></img>
    
    0 讨论(0)
  • 2020-12-04 06:31

    Here is an example of getting the bounds using svg.getBox(): https://gist.github.com/john-doherty/2ad94360771902b16f459f590b833d44

    At the end you get numbers that you can plug into the svg to set the viewbox properly. Then use any css on the parent div and you're done.

     // 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);
    
    0 讨论(0)
提交回复
热议问题