开发者

Show a zoomed SVG at designed size

I have an SVG image which fits its parent <object> dimensions. The image is zoomed by changing its currentScale and currentTranslate properties. How can I scale the image to be shown at its intended size and center it in the viewport?

Example code:

<object width="500" height="300" data="pic.svg">
    <!-- The image will scale to be 300px heigh, 
         I need it to be shown at 32x32 and centered in the viewport,
         but could be zoomed to any size.
         Setting width and height attributes will crop the viewable area to 32x32,
         so we can't use this solution.
     -->
    <svg viewBox="0 0 32 32" ... >
</ob开发者_如何学Cject>


Ok, I found the solution. To do this I got the intended size from width/height/viewbox (depending on which is available), made the viewBox equal the size of the available viewport (i.e. the containing <object> tag) and changed the x/y values of the viewBox attribute so that the image appears to be in the center of the viewport, and removed width and height attributes if they were present.

I use this in my Opera SVG Viewer widget, you can see my SVG zooming library here.


Set the viewBox attribute to encompass the area of the content (in local SVG coordinates) you want to display, but set the height and width attributes of the SVG to the number of pixels you want it to be displayed as on the web page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜