以设计尺寸显示缩放的 SVG

发布于 2024-10-31 00:22:02 字数 582 浏览 1 评论 0原文

我有一个适合其父 尺寸的 SVG 图像。通过更改其 currentScale 和 currentTranslate 属性来缩放图像。如何缩放图像以使其按预期大小显示并将其在视口中居中?

示例代码:

<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" ... >
</object>

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" ... >
</object>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤千羽 2024-11-07 00:22:02

好的,我找到了解决方案。为此,我从宽度/高度/视图框(取决于可用的)中获取了预期的大小,使视图框等于可用视口的大小(即包含 标签)并且更改了 viewBox 属性的 x/y 值,使图像看起来位于视口的中心,并删除了宽度和高度属性(如果存在)。

我在我的Opera SVG Viewer widget中使用它,你可以看到我的SVG缩放库此处

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.

禾厶谷欠 2024-11-07 00:22:02

设置 viewBox 属性以包含要显示的内容区域(以本地 SVG 坐标表示),但设置 heightwidth 属性将 SVG 的像素数设置为您希望其在网页上显示的像素数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文