如何在使用react-native-svg时防止旋转形状出现翘曲或倾斜?

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

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%:

如何在使用react-native-svg时防止旋转形状出现翘曲或倾斜?

And here is the same rectangle with a 71 degree rotation (width and height are the same as previous):

如何在使用react-native-svg时防止旋转形状出现翘曲或倾斜?

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%:

如何在使用react-native-svg时防止旋转形状出现翘曲或倾斜?

And here is the same rectangle with a 71 degree rotation (width and height are the same as previous):

如何在使用react-native-svg时防止旋转形状出现翘曲或倾斜?

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.

huangapple
  • 本文由 发表于 2020年1月6日 17:20:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609450.html
匿名

发表评论

匿名网友

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

确定