简体   繁体   English

将SVG与背景图像对齐

[英]Align SVG with background image

I have a full page background image and an SVG on top of it . 我有一个整页的背景图片和一个SVG The image is a mountain and I want my SVG to follow the crease of the mountain. 图像是一座山,我希望我的SVG跟随山的折痕。

However, when I resize the page, the SVG shifts away from the mountain creast. 但是,当我调整页面大小时,SVG远离山峰。

What I want to achieve (even when I resize): 我想要达到的目标(即使我调整大小): 图片

SNIPPET: 片段:

 body, header, svg { width: 100%; height: 600px; } header { background: url(http://i68.tinypic.com/2wnnriv.jpg) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; position: relative; } svg { position: absolute; } 
 <header> <svg version="1.1" id="map_line_svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1600 1000" enable-background="new 0 0 1600 1000" xml:space="preserve"> <polyline fill="none" stroke="#FFFFFF" stroke-width="3" stroke-miterlimit="10" points="999.775,503.558 1015.275,469.194 1007.525,438.194 934.275,399.194 918.775,402.194 896.775,371.694 867.775,362.694 814.275,274.194 789.275,258.694 781.775,248.194 "/> <circle fill="#FFFFFF" cx="1015.275" cy="469.054" r="3.703"/> <circle fill="#FFFFFF" cx="1007.369" cy="438.304" r="3.703"/> <circle fill="#FFFFFF" cx="934.182" cy="399.491" r="3.703"/> <circle fill="#FFFFFF" cx="918.807" cy="402.054" r="3.703"/> <circle fill="#FFFFFF" cx="896.557" cy="372.173" r="3.703"/> <circle fill="#FFFFFF" cx="867.557" cy="362.423" r="3.703"/> <circle fill="#FFFFFF" cx="814.057" cy="273.673" r="3.703"/> <circle fill="#FFFFFF" cx="788.557" cy="257.673" r="3.703"/> <circle fill="#FFFFFF" cx="781.775" cy="248.194" r="3.703"/> <circle fill="#FFFFFF" cx="999.942" cy="503.558" r="3.703"/> </svg> </header> 

In case resize is not possible in the above snippet: CODEPEN https://codepen.io/SimeriaIonut/pen/RxzMRe 如果以上代码段中无法调整大小: CODEPEN https://codepen.io/SimeriaIonut/pen/RxzMRe

What I tried is to make both the background and the SVG the same size, so when I resize the image they would both behave the same (doesn't work). 我想做的是使背景和SVG的大小相同,因此当我调整图像大小时,它们的行为相同(不起作用)。

I also tried to apply object-fit: contain to both of them but with no success. 我还尝试将object-fit: contain应用于两者,但均未成功。

I know this is possible, I just don't really know how to. 我知道这是可能的,我只是真的不知道该怎么做。 I looked on other topics but none of them seem to answer my question. 我查看了其他主题,但似乎没有一个回答我的问题。

Thank you! 谢谢!

In order to avoid disagreements between the coordinates of the path and fragments of the image of the mountain when the scale is changed, it is necessary that they are in the same coordinate system SVG 为了避免在更改比例时路径的坐标和山峰图像的片段之间存在分歧,有必要使它们在同一坐标系SVG中

Therefore, we add the image of the mountain with the help of the tag <image> inside svg 因此,我们借助svg中的标签<image>添加山峰的<image>

<image xlink:href="https://i.stack.imgur.com/045d7.jpg" width="1600px" height="1100px" y="0" x="0"/>

 <svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="0 0 1600 1100" > <image xlink:href="https://i.stack.imgur.com/045d7.jpg" width="1600px" height="1100px" y="0" x="0"/> <g id="g3373" transform="translate(0,46)"> <polyline points="999.775 503.558 1015.275 469.194 1007.525 438.194 934.275 399.194 918.775 402.194 896.775 371.694 867.775 362.694 814.275 274.194 789.275 258.694 781.775 248.194 " stroke-miterlimit="10" style="fill:none;stroke-width:3;stroke:crimson"/> <circle fill="#FFFFFF" cx="1015.275" cy="469.054" r="3.703"/> <circle fill="#FFFFFF" cx="1007.369" cy="438.304" r="3.703"/> <circle fill="#FFFFFF" cx="934.182" cy="399.491" r="3.703"/> <circle fill="#FFFFFF" cx="918.807" cy="402.054" r="3.703"/> <circle fill="#FFFFFF" cx="896.557" cy="372.173" r="3.703"/> <circle fill="#FFFFFF" cx="867.557" cy="362.423" r="3.703"/> <circle fill="#FFFFFF" cx="814.057" cy="273.673" r="3.703"/> <circle fill="#FFFFFF" cx="788.557" cy="257.673" r="3.703"/> <circle fill="#FFFFFF" cx="781.775" cy="248.194" r="3.703"/> <circle fill="#FFFFFF" cx="999.942" cy="503.558" r="3.703"/> </g> </svg> 

Now, when zooming in, the route seems to be glued to the mountain. 现在,放大时,这条路线似乎被粘在了山上。

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

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