Skip to content

Commit 11209a0

Browse files
fix: positioning element inside viewbox (#2758)
# Summary Fixes: #2078 Fixes: #1343 When `preserveAspectRatio !== "none"`, the initial `translateX` and `translateY` values should be calculated after adjusting `scaleX` and `scaleY` with `Math.min` or `Math.max` (depending on the `meetOrSlice` value). Previously `translateX` and `translateY` were computed before applying this adjustment using the raw `scaleX` and `scaleY`. This caused incorrect positioning of the content. ## Test Plan Run examples from issues: #2078 and #1343 ## Compatibility | OS | Implemented | | ------- | :---------: | | iOS | ✅ | | MacOS | ✅ | | Android | ✅ | | Web | ❌ |
1 parent 5b62c5d commit 11209a0

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

android/src/main/java/com/horcrux/svg/ViewBox.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ static Matrix getTransform(RectF vbRect, RectF eRect, String align, int meetOrSl
7272
scaleX = scaleY = Math.max(scaleX, scaleY);
7373
}
7474

75+
translateX = eX - (vbX * scaleX);
76+
translateY = eY - (vbY * scaleY);
77+
7578
// If align contains 'xMid', add (e-width - vb-width * scale-x) / 2 to translate-x.
7679
if (align.contains("xMid")) {
7780
translateX += (eWidth - vbWidth * scaleX) / 2.0d;

apple/Utils/RNSVGViewBox.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ + (CGAffineTransform)getTransform:(CGRect)vbRect
7474
scaleX = scaleY = fmax(scaleX, scaleY);
7575
}
7676

77+
translateX = eX - (vbX * scaleX);
78+
translateY = eY - (vbY * scaleY);
79+
7780
// If align contains 'xMid', add (e-width - vb-width * scale-x) / 2 to translate-x.
7881
if ([align containsString:@"xMid"]) {
7982
translateX += (eWidth - vbWidth * scaleX) / 2.0;

0 commit comments

Comments
 (0)