Skip to content

Commit 289dacf

Browse files
cortinicofacebook-github-bot
authored andcommitted
Correctly implement transform: matrix with 2d matrixes
Summary: Fixes #53639 I've realized that our docs and validation logic states that we do support 3x3 matrixes (for 2d transforms). However they're not properly processed as the values are just copied into a 4x4 matrix. This can be verified by applying the 3x3 identity matrix on any transform. I'm fixing it by correctly populating the 4x4 matrix getting the values from the 3x3 matrix in input. Changelog: [General] [Fixed] - 9-element (2d) transform matrix are not correctly working Differential Revision: D82836192
1 parent 2590cd7 commit 289dacf

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

packages/react-native/ReactCommon/react/renderer/components/view/conversions.h

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,34 @@ inline void fromRawValue(
614614
}
615615

616616
size_t i = 0;
617-
for (auto number : numbers) {
618-
transformMatrix.matrix[i++] = number;
617+
if (numbers.size() == 16) {
618+
for (auto number : numbers) {
619+
transformMatrix.matrix[i++] = number;
620+
}
621+
} else if (numbers.size() == 9) {
622+
// We need to convert the 2d transform matrix into a 3d one as such:
623+
// [
624+
// x01, x02, 0, x03
625+
// x10, x11, 0, x12
626+
// 0, 0, 1, 0
627+
// x20, x21, 0, x22
628+
// ]
629+
transformMatrix.matrix[0] = numbers[0];
630+
transformMatrix.matrix[1] = numbers[1];
631+
transformMatrix.matrix[2] = 0;
632+
transformMatrix.matrix[3] = numbers[2];
633+
transformMatrix.matrix[4] = numbers[3];
634+
transformMatrix.matrix[5] = numbers[4];
635+
transformMatrix.matrix[6] = 0;
636+
transformMatrix.matrix[7] = numbers[5];
637+
transformMatrix.matrix[8] = 0;
638+
transformMatrix.matrix[9] = 0;
639+
transformMatrix.matrix[10] = 1;
640+
transformMatrix.matrix[11] = 0;
641+
transformMatrix.matrix[12] = numbers[6];
642+
transformMatrix.matrix[13] = numbers[7];
643+
transformMatrix.matrix[14] = 0;
644+
transformMatrix.matrix[15] = numbers[8];
619645
}
620646
transformMatrix.operations.push_back(TransformOperation{
621647
TransformOperationType::Arbitrary, Zero, Zero, Zero});

packages/rn-tester/js/examples/Transform/TransformExample.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ function TranslatePercentage() {
144144
return <View style={styles.translatePercentageView} />;
145145
}
146146

147+
function TranslateMatrix2D() {
148+
return <View style={styles.translateMatrix2D} />;
149+
}
150+
function TranslateMatrix3D() {
151+
return <View style={styles.translateMatrix3D} />;
152+
}
153+
147154
const styles = StyleSheet.create({
148155
container: {
149156
height: 500,
@@ -288,6 +295,18 @@ const styles = StyleSheet.create({
288295
alignSelf: 'flex-start',
289296
backgroundColor: 'lightblue',
290297
},
298+
translateMatrix2D: {
299+
transform: [{matrix: [1, 0, 0, 0, 1, 0, 0, 0, 1]}],
300+
width: 50,
301+
height: 50,
302+
backgroundColor: 'red',
303+
},
304+
translateMatrix3D: {
305+
transform: [{matrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]}],
306+
height: 50,
307+
width: 50,
308+
backgroundColor: 'green',
309+
},
291310
});
292311

293312
exports.title = 'Transforms';
@@ -414,4 +433,19 @@ exports.examples = [
414433
return <TranslatePercentage />;
415434
},
416435
},
436+
{
437+
title: 'Transofrm Matrix 2D',
438+
description: "transform: 'matrix(1, 0, 0, 0, 1, 0, 0, 0, 1)'",
439+
render(): React.Node {
440+
return <TranslateMatrix2D />;
441+
},
442+
},
443+
{
444+
title: 'Transofrm Matrix 3D',
445+
description:
446+
"transform: 'matrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)'",
447+
render(): React.Node {
448+
return <TranslateMatrix3D />;
449+
},
450+
},
417451
] as Array<RNTesterModuleExample>;

0 commit comments

Comments
 (0)