英文:
How can I prevent warping/skewing on a rotated shape with react-native-svg?
问题
I'm currently making an application in react-native
and using react-native-svg
to render shapes, i.e. rectangles, ellipses, etc.
I'm containing these shapes in an SVG container component with a width to height ratio of 2:1:
- The width of the shape is a percentage of the width of the container.
- The height of the shape is a percentage of the height of the container.
I'm currently encountering an issue that I haven't been able to resolve. Every time I rotate a shape, it distorts and warps in odd ways.
Here is a rectangle with 0 degree rotation and width of 60% and height of 30%:
And here is the same rectangle with a 71 degree rotation (width and height are the same as previous):
I have the rectangles enveloped in an SVG component as such:
<Svg width="100%" height="100%" viewBox="0 0 100 100" preserveAspectRatio="none">
I attempted to tinker with the preserveAspectRatio
prop of the component but any selection with it seems to distort the widths and heights of the shapes.
All my SVG shape components use approximately the same format:
<Rect ... width={ attributes.width } height={ attributes.height } transform={ "rotate(" + attributes.rotation + " " + attributes.x + " " + attributes.y + ")" } />
I've programmed it in such a way that the rotate()
string results in, for example: rotate(71, 30, 55)
.
Any ideas?
英文:
I'm currently making an application in react-native
and using react-native-svg
to render in shapes, i.e. rectangles, ellipses, etc.
I'm containing these shapes in an SVG container component with a width to height ratio of 2:1:
- The width of the shape is a percentage of the width of the container.
- The height of the shape is a percentage of the height of the container.
I'm currently encountering an issue that I haven't been able to resolve. Every time I rotate a shape, it distorts and warps in odd ways.
Here is a rectangle with 0 degree rotation and width of 60% and height of 30%:
And here is the same rectangle with a 71 degree rotation (width and height are the same as previous):
I have the rectangles enveloped in an SVG component as such:
<Svg width="100%" height="100%" viewBox="0 0 100 100" preserveAspectRatio="none">
I attempted to tinker with the preserveAspectRatio
prop of the component but any selection with it seems to distort the widths and heights of the shapes.
All my SVG shape components use approximately the same format:
<Rect ... width={ attributes.width } height={ attributes.height } transform={ "rotate(" + attributes.rotation + " " + attributes.x + " " + attributes.y + ")" } />
I've programmed it in such a way that the rotate()
string results in, for example: rotate(71, 30, 55)
.
Any ideas?
答案1
得分: 2
问题在于 preserveAspectRatio="none"
。它告诉浏览器将SVG拉伸以适应您指定的宽度和高度。
SVG的viewBox
具有正方形的纵横比(宽度和高度都是100)。但似乎SVG的父容器不是如此。因此,您的SVG被水平拉伸以适应。
删除 preserveAspectRatio="none"
,它将不再拉伸/变形。
英文:
The problem is the preserveAspectRatio="none"
. It tells the browser to stretch the SVG to fit the width and height that you have specified.
The viewBox
of the SVG has a square aspect ratio (the width and height are both 100). But it looks like the parent container of the SVG does not. So your SVG is being stretched horizontally to fit.
Remove the preserveAspectRatio="none"
, and it will no longer stretch/warp.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论