Skip to content

Commit

Permalink
Expo matrix values (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon authored Sep 13, 2022
1 parent fb3b69c commit eb37899
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
26 changes: 13 additions & 13 deletions example/src/Examples/API/Transform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,19 @@ export const Transform = () => {
const pers0 = 0;
const pers1 = 0;
const pers2 = 1;
canvas.concat(
Skia.Matrix([
scaleX,
skewX,
transX,
skewY,
scaleY,
transY,
pers0,
pers1,
pers2,
])
);
const m = Skia.Matrix([
scaleX,
skewX,
transX,
skewY,
scaleY,
transY,
pers0,
pers1,
pers2,
]);
//console.log({ matrix: m.get() });
canvas.concat(m);

canvas.drawImageRect(image, imgRect, rect, paint);
canvas.restore();
Expand Down
9 changes: 9 additions & 0 deletions package/cpp/api/JsiSkMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ class JsiSkMatrix : public JsiSkWrappingSharedPtrHostObject<SkMatrix> {
return jsi::Value::undefined();
}

JSI_HOST_FUNCTION(get) {
auto values = jsi::Array(runtime, 9);
for (auto i = 0; i < 9; i++) {
values.setValueAtIndex(runtime, i, getObject()->get(i));
}
return values;
}

JSI_EXPORT_PROPERTY_GETTERS(JSI_EXPORT_PROP_GET(JsiSkMatrix, __typename__))

JSI_EXPORT_FUNCTIONS(
Expand All @@ -97,6 +105,7 @@ class JsiSkMatrix : public JsiSkWrappingSharedPtrHostObject<SkMatrix> {
JSI_EXPORT_FUNC(JsiSkMatrix, skew),
JSI_EXPORT_FUNC(JsiSkMatrix, rotate),
JSI_EXPORT_FUNC(JsiSkMatrix, identity),
JSI_EXPORT_FUNC(JsiSkMatrix, get),
)

/**
Expand Down
1 change: 1 addition & 0 deletions package/src/renderer/__tests__/Transform.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe("Renderer", () => {
matrix.translate(origin.x, origin.y);
matrix.scale(0.5);
matrix.translate(-origin.x, -origin.y);
expect(matrix.get()).toStrictEqual([0.5, 0, 192, 0, 0.5, 192, 0, 0, 1]);
const surface = drawOnNode(
<Group matrix={matrix}>
<Rect x={0} y={0} width={size} height={size} color="lightblue" />
Expand Down
1 change: 1 addition & 0 deletions package/src/skia/types/Matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface SkMatrix extends SkJSIInstance<"Matrix"> {
skew: (x: number, y: number) => void;
rotate: (theta: number) => void;
identity: () => void;
get: () => number[];
}

type Transform2dName =
Expand Down
4 changes: 4 additions & 0 deletions package/src/skia/web/JsiSkMatrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ export class JsiSkMatrix
identity() {
this.ref.set(this.CanvasKit.Matrix.identity());
}

get() {
return Array.from(this.ref);
}
}

0 comments on commit eb37899

Please sign in to comment.