Skip to content

Commit

Permalink
Handle 90 degree rotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
samwho committed Mar 21, 2024
1 parent 8444cc4 commit b98099d
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pixijs-layout",
"version": "0.1.18",
"version": "0.1.20",
"description": "",
"main": "./out/index.js",
"types": "./out/index.d.ts",
Expand Down
Binary file added screenshots/rotated-tube-center.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/rotated-tube-fit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 48 additions & 4 deletions src/Leaf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,40 @@ enum Align {
None,
}

function getRotatedDimensions(width: number, height: number, rotation: number) {
const sinAngle = Math.abs(Math.sin(rotation));
const cosAngle = Math.abs(Math.cos(rotation));
return {
width: width * cosAngle + height * sinAngle,
height: width * sinAngle + height * cosAngle,
};
}

function getOriginalDimensions(
rotatedWidth: number,
rotatedHeight: number,
rotation: number,
) {
const sinAngle = Math.abs(Math.sin(rotation));
const cosAngle = Math.abs(Math.cos(rotation));

const originalWidth =
(rotatedWidth * cosAngle + rotatedHeight * sinAngle) /
(cosAngle + sinAngle);
const originalHeight =
(rotatedHeight * cosAngle + rotatedWidth * sinAngle) /
(cosAngle + sinAngle);

// const originalWidth =
// (rotatedWidth * cosAngle + rotatedHeight * sinAngle) /
// (cosAngle * cosAngle + sinAngle * sinAngle);
// const originalHeight =
// (rotatedHeight * cosAngle + rotatedWidth * sinAngle) /
// (cosAngle * cosAngle + sinAngle * sinAngle);

return { width: originalWidth, height: originalHeight };
}

export function Leaf(child: Container): LeafComponent {
return new LeafComponent(child);
}
Expand Down Expand Up @@ -161,8 +195,11 @@ export class LeafComponent extends Container implements Positioner {

let x = this._child.x;
let y = this._child.y;
let width = this._child.width;
let height = this._child.height;
let { width, height } = getRotatedDimensions(
this._child.width,
this._child.height,
this._child.rotation,
);
let containerAspectRatio = space.width / space.height;
let aspectRatio = width / height;

Expand Down Expand Up @@ -229,8 +266,15 @@ export class LeafComponent extends Container implements Positioner {

this._child.x = x;
this._child.y = y;
this._child.width = width;
this._child.height = height;

const finalDimensions = getOriginalDimensions(
width,
height,
this._child.rotation,
);

this._child.width = finalDimensions.width;
this._child.height = finalDimensions.height;

if ("arrange" in this._child) {
let child = this._child as Positioner;
Expand Down
4 changes: 4 additions & 0 deletions test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ export function tube({
width,
height,
center,
angle,
}: {
x?: number;
y?: number;
width?: number;
height?: number;
center?: boolean;
angle?: number;
} = {}): PIXI.Graphics {
x = x ?? 0;
y = y ?? 0;
Expand All @@ -80,6 +82,8 @@ export function tube({
tube.drawCircle(x, y + height / 2, height / 2);
tube.drawCircle(x + width, y + height / 2, height / 2);
tube.endFill();

tube.angle = angle ?? 0;
return tube;
}

Expand Down
18 changes: 18 additions & 0 deletions tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,21 @@ componentTest("grid-of-tube-fit", () =>
tube(),
).leaves((leaf) => leaf.center().fit().padding(5)),
);

componentTest("rotated-tube-center", () =>
HStack(tube({ angle: 90 })).leaves((leaf) => leaf.center()),
);

componentTest("rotated-tube-fit", () =>
HStack(tube({ angle: 90 })).leaves((leaf) => leaf.fit()),
);

// Can't yet handle non-90 degree rotations
// componentTest("rotated-tube-fit-45", () =>
// HStack(tube({ angle: 45 })).leaves((leaf) => leaf.fit()),
// );
//
// Can't yet handle non-90 degree rotations
// componentTest("rotated-tube-fit-10", () =>
// HStack(tube({ angle: 10 })).leaves((leaf) => leaf.fit()),
// );

0 comments on commit b98099d

Please sign in to comment.