英文:
Stretch a SVG image along the x-axis
问题
I'm looking to stretch my SVG image along the horizontal axis, which is acting as a background for some HTML divs. It is a simple bar that takes up the middle third horizontally :
我想将我的SVG图像沿水平轴拉伸,它作为一些HTML divs的背景。它是一个简单的条,水平占据中间的三分之一:
It must not move on the vertical, and when horizontal is stretched, the image also must stretch so that the bar takes up the middle third.
它在垂直方向不能移动,当水平拉伸时,图像也必须拉伸,以使该条占据中间的三分之一。
Currently if I stretch horizontally, it maintains its vertical position which is good, but the SVG does not take up a third but maintains its original width:
当前,如果我水平拉伸,它会保持垂直位置,这是好的,但SVG不会占据三分之一,而是保持原始宽度:
Here is the React code :
以下是React代码:
function BackgroundSVG() {
let outerDiv = {
position: "relative",
border: "3px solid red",
}
let svgStyle = {
position: "absolute",
top: "0px",
border: "3px solid pink",
zIndex: -1,
width: "100%",
height: "200px"
}
return (
<div style={outerDiv}>
<div>Lorem ipsum .......</div>
<svg style={svgStyle}
viewBox={`0 0 ${600} ${200}`}
xmlns="http://www.w3.org/2000/svg">
<path d={`M ${200} ${100} H ${400}`} strokeWidth={12} stroke="blue"/>
</svg>
</div>
);
}
How do I stretch the image along the x-axis, while preserving things on the y-axis?
我如何在保留y轴上的内容的同时拉伸图像沿x轴?
I have tried to play around with the widths and heights but that does not seem to work. I assume that if I keep width at 100% and height at 200px that this would solve the problem.
我尝试过调整宽度和高度,但似乎没有效果。我假设如果我将宽度保持在100%,高度保持在200px,这将解决问题。
英文:
I'm looking to stretch my SVG image along the horizontal axis, which is acting as a background for some HTML divs. It is a simple bar that takes up the middle third horizontally :
It must not move on the vertical, and when horizontal is stretched, the image also must stretch so that the bar takes up the middle third.
Currently if I stretch horizontally, it maintains its vertical position which is good, but the SVG does not take up a third but maintains its original width:
Here is the React code :
function BackgroundSVG() {
let outerDiv = {
position:"relative",
border: "3px solid red",
}
let svgStyle = {
position:"absolute",
top:"0px",
border: "3px solid pink",
zIndex: -1,
width: "100%",
height: "200px"
}
return (
<div style={outerDiv}>
<div>Lorem ipsum .......</div>
<svg style={svgStyle}
viewBox={`0 0 ${600} ${200} `}
xmlns="http://www.w3.org/2000/svg">
<path d={`M ${200} ${100} H ${400}`} strokeWidth={12} stroke="blue"/>
</svg>
</div>
);
}
Here is a code pen
https://codepen.io/oliverwatkins/pen/MWqWmQz
How do I stretch the image along the x-axis, while preserving things on the y-axis?
I have tried to play around with the widths and heights but that does not seem to work. I assume that if I keep width at 100% and height at a 200px that this would solve the problem.
答案1
得分: 1
默认情况下,preserveAspectRatio
属性的值是 xMidYMid meet
,这样浏览器会尝试保持图像的宽高比。
要拉伸 SVG 图像,我们需要设置 preserveAspectRatio="none"
。
英文:
Default preserveAspectRatio
attribute value is xMidYMid meet
so that browser try to maintain image aspect ratio.
For stretching SVG image, we need to set
preserveAspectRatio="none"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论