简体   繁体   English

调整浏览器窗口大小时,防止缩放SVG元素

[英]Prevent a SVG element to be scaled when the browser window is resized

I have a SVG image which is basically a background with other points ( images ) on it. 我有一个SVG图像,该图像基本上是带有其他点( images )的背景。

Here is my code: 这是我的代码:

<div class="svg-background" style="width: 1920px;>
    <svg xml:space="preserve" enable-background="new 0 0 1600 640" viewBox="0 0 1600 640" y="0px" x="0px" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" version="1.1" id="svg" width="100%" height="100%">
        <!-- ...SVG code... -->
        <image id="point" href="point.png" width="49" height="49" alt="point" class="svg-point" x="100" y="200"/>
        <!-- and more points... -->
    </svg>
</div>

With jQuery I change the #svg-background div to fit the browser width, so I have a SVG background that fits the whole browser width, and it is scaling very well. 使用jQuery时,我将#svg-background div更改为适合浏览器宽度,因此我拥有一个适合整个浏览器宽度的SVG背景,并且缩放效果非常好。

My problem is that the points ( image elemnts) are scaling too and I need these points have a fixed width and height . 我的问题是这些点( image元素)也在缩放,因此我需要这些点具有固定的width和height

How can I prevent a SVG element to be scaled when the browser window is resized? 调整浏览器窗口大小时,如何防止SVG元素缩放?

If an element is inside the SVG, then it gets scaled when the SVG does. 如果元素在SVG内,则在SVG进行缩放时会缩放。 You can't prevent that. 您无法避免。

You have a couple of potential solutions: 您有几个潜在的解决方案:

  1. Should those non-resizing elements even be a part of the SVG? 这些非调整大小的元素是否应该成为SVG的一部分? Should they instead be in a separate SVG that is absolutely positioned on top of the other one? 是否应该将它们放置在绝对位于另一个顶部的单独的SVG中?

  2. If you want some elements to be scaled and others that aren't, then a work around would be to have an OnResize event handler that worked out what the element size needs to be in SVG coordinate space and applied a transform attribute to suit. 如果要缩放某些元素而不缩放某些元素,则需要解决的方法是使用OnResize事件处理程序,该事件处理程序可以确定SVG坐标空间中元素的大小,并应用适合的transform属性。 So if the window gets bigger, you apply a transform that scales the object down by an appropriate amount to compensate. 因此,如果窗口变大,则应用变换将对象缩小适当的数量以进行补偿。 Needless to say, it's a bit of a hacky workaround. 不用说,这是一个有点棘手的解决方法。

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

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