拉伸SVG图像沿x轴方向。

huangapple go评论61阅读模式
英文:

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的背景。它是一个简单的条,水平占据中间的三分之一:

拉伸SVG图像沿x轴方向。

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不会占据三分之一,而是保持原始宽度:

拉伸SVG图像沿x轴方向。

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 :

拉伸SVG图像沿x轴方向。

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图像沿x轴方向。

Here is the React code :

function BackgroundSVG() {

    let outerDiv = {
        position:&quot;relative&quot;,
        border: &quot;3px solid red&quot;,
    }

    let svgStyle = {
        position:&quot;absolute&quot;,
        top:&quot;0px&quot;,
        border: &quot;3px solid pink&quot;,
        zIndex: -1,

        width: &quot;100%&quot;,
        height: &quot;200px&quot;
    }
    return (
        &lt;div style={outerDiv}&gt;
            &lt;div&gt;Lorem ipsum .......&lt;/div&gt;
            &lt;svg style={svgStyle}
                 viewBox={`0 0 ${600} ${200} `}
                 xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;
                &lt;path d={`M ${200} ${100} H ${400}`} strokeWidth={12} stroke=&quot;blue&quot;/&gt;
            &lt;/svg&gt;
        &lt;/div&gt;
    );
}

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=&quot;none&quot;

huangapple
  • 本文由 发表于 2023年2月14日 22:05:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定