Skip to content

Commit be178bd

Browse files
committed
🔧
1 parent e2c923e commit be178bd

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

package/cpp/rnskia/dom/props/TransformProp.h

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@ static PropId PropNameSkewX = JsiPropId::get("skewX");
1919
static PropId PropNameSkewY = JsiPropId::get("skewY");
2020
static PropId PropNameRotate = JsiPropId::get("rotate");
2121
static PropId PropNameRotateZ = JsiPropId::get("rotateZ");
22-
23-
/*
24-
| "perspective"
25-
| "rotateX"
26-
| "rotateY"
27-
| "matrix"
28-
*/
22+
static PropId PropNameRotateX = JsiPropId::get("rotateX");
23+
static PropId PropNameRotateY = JsiPropId::get("rotateY");
24+
static PropId PropNamePerspective = JsiPropId::get("perspective");
25+
static PropId PropNameMatrix4 = JsiPropId::get("matrix");
2926

3027
class TransformProp : public DerivedProp<SkMatrix> {
3128
public:
@@ -66,6 +63,20 @@ class TransformProp : public DerivedProp<SkMatrix> {
6663
auto z = el.getValue(key).getAsNumber();
6764
SkM44 trZ(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, z, 0, 0, 0, 1);
6865
m4.preConcat(trZ);
66+
} else if (key == PropNameTranslate) {
67+
auto arr = el.getValue(key).getAsArray();
68+
double x = 0, y = 0, z = 0;
69+
for (size_t i = 0; i < arr.size(); ++i) {
70+
if (i == 0) {
71+
x = arr[i].getAsNumber();
72+
} else if (i == 1) {
73+
y = arr[i].getAsNumber();
74+
} else if (i == 2) {
75+
z = arr[i].getAsNumber();
76+
}
77+
}
78+
SkM44 tr(1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1);
79+
m4.preConcat(tr);
6980
} else if (key == PropNameScale) {
7081
auto s = el.getValue(key).getAsNumber();
7182
SkM44 scale(s, 0, 0, 0, 0, s, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
@@ -89,8 +100,30 @@ class TransformProp : public DerivedProp<SkMatrix> {
89100
} else if (key == PropNameRotate || key == PropNameRotateZ) {
90101
auto angle = el.getValue(key).getAsNumber();
91102
SkM44 rotate;
92-
rotate.setRotateUnit({ 0, 0, 1}, angle);
103+
rotate.setRotateUnit({ 0, 0, 1 }, angle);
104+
m4.preConcat(rotate);
105+
} else if (key == PropNameRotateY) {
106+
auto angle = el.getValue(key).getAsNumber();
107+
SkM44 rotate;
108+
rotate.setRotateUnit({ 0, 1, 0 }, angle);
109+
m4.preConcat(rotate);
110+
} else if (key == PropNameRotateX) {
111+
auto angle = el.getValue(key).getAsNumber();
112+
SkM44 rotate;
113+
rotate.setRotateUnit({ 1, 0, 0 }, angle);
93114
m4.preConcat(rotate);
115+
} else if (key == PropNamePerspective) {
116+
auto p = el.getValue(key).getAsNumber();
117+
SkM44 perspective(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, -1 / p, 1);
118+
m4.preConcat(perspective);
119+
} else if (key == PropNameMatrix4) {
120+
auto arr = el.getValue(key).getAsArray();
121+
SkM44 m44;
122+
for (size_t i = 0; i < arr.size(); ++i) {
123+
auto obj = arr[i];
124+
m44.setRC(i / 4, i % 4, obj.getAsNumber());
125+
}
126+
m4.preConcat(m44);
94127
} else {
95128
throw std::runtime_error(
96129
"Unknown key in transform. Expected translateX, translateY, "

package/src/renderer/__tests__/e2e/Matrix4.spec.tsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe("Matrix4", () => {
9595
])
9696
).toEqual(toMatrix3(m4));
9797
});
98-
it("Should do a perspective transformation", async () => {
98+
it("Should do a perspective transformation (1)", async () => {
9999
const { width, height } = surface;
100100
const pad = 32;
101101
const rct = {
@@ -118,4 +118,31 @@ describe("Matrix4", () => {
118118
);
119119
checkImage(image, "snapshots/matrix4/perspective.png");
120120
});
121+
it("Should do a perspective transformation (2)", async () => {
122+
const { width, height } = surface;
123+
const pad = 32;
124+
const rct = {
125+
x: pad,
126+
y: pad,
127+
width: width - pad * 2,
128+
height: height - pad * 2,
129+
};
130+
const image = await surface.draw(
131+
<Group>
132+
<Rect rect={rct} color="magenta" />
133+
<Rect
134+
rect={rct}
135+
color="cyan"
136+
opacity={0.5}
137+
transform={[
138+
{ translate: [width / 2, height / 2] },
139+
{ perspective: 300 },
140+
{ rotateX: 1 },
141+
{ translate: [-width / 2, -height / 2] },
142+
]}
143+
/>
144+
</Group>
145+
);
146+
checkImage(image, "snapshots/matrix4/perspective.png");
147+
});
121148
});

0 commit comments

Comments
 (0)