Skip to content

Commit

Permalink
Fix aspect ratio calculation on fit()
Browse files Browse the repository at this point in the history
  • Loading branch information
samwho committed Mar 19, 2024
1 parent 10a1edc commit c1ac221
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 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.

Binary file added screenshots/tube-center-fit.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/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.
4 changes: 2 additions & 2 deletions src/Leaf.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Rectangle, Container } from "pixi.js-legacy";
import { Rectangle, Container } from "pixi.js";
import Positioner from "./Positioner";
import { getDimension } from "./utils";

Expand Down Expand Up @@ -174,7 +174,7 @@ export class LeafComponent extends Container implements Positioner {
width = space.height * aspectRatio;
height = space.height;
} else {
height = space.width * aspectRatio;
height = space.width / aspectRatio;
width = space.width;
}
break;
Expand Down
33 changes: 33 additions & 0 deletions test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,39 @@ export function rect({
return rect;
}

export function tube({
x,
y,
width,
height,
center,
}: {
x?: number;
y?: number;
width?: number;
height?: number;
center?: boolean;
} = {}): PIXI.Graphics {
x = x ?? 0;
y = y ?? 0;
width = width ?? 100;
height = height ?? 50;
center = center ?? true;

let tube = new PIXI.Graphics();

if (center) {
tube.pivot.x = width / 2;
tube.pivot.y = height / 2;
}
tube.beginFill(0xff0000);
tube.drawRect(x, y, width, height);
tube.drawCircle(x, y + height / 2, height / 2);
tube.drawCircle(x + width, y + height / 2, height / 2);
tube.endFill();
return tube;
}

function innerTest(name: string, cb: (app: PIXI.Application) => void) {
let canvas = document.createElement("canvas");
canvas.width = 800;
Expand Down
10 changes: 9 additions & 1 deletion tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Container } from "pixi.js-legacy";
import { Grid, HStack, Stack, VStack } from "./src";
import { componentTest, circle, appTest, rect } from "./test-utils";
import { componentTest, circle, appTest, rect, tube } from "./test-utils";

componentTest("1x2-hstack", () =>
HStack(new Container(), new Container()).debug(),
Expand Down Expand Up @@ -326,3 +326,11 @@ componentTest("complex-debug", () =>
.debug()
.leaves((leaf) => leaf.fit().padding("10%")),
);

componentTest("tube-center", () =>
HStack(tube()).leaves((leaf) => leaf.center()),
);

componentTest("tube-center-fit", () =>
HStack(tube()).leaves((leaf) => leaf.center().fit()),
);

0 comments on commit c1ac221

Please sign in to comment.