Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit play area aspect-ratio #109

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 43 additions & 10 deletions src/utils/pileon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,56 @@ export const fillerStacks = (
return { length: i * j - 15 };
};

export const calculateFontSize = (
const calculateTableEmSize = (
size: ICardSize,
columns: number,
rows: number,
): [number, number] => {
const tableWidthEm =
stackWidthEm(4, size) * columns +
(size !== "small" ? 0.666 : 0.333) * columns * 2;
const tableHeightEm =
getCardDimensionsEm(size).height * rows +
(size !== "small" ? 0.5 : 0.25) * rows * 2;

return [tableWidthEm, tableHeightEm];
};

interface TableDimensions {
fontSize: number;
columns: number;
rows: number;
tableWidthEm: number;
tableHeightEm: number;
}

export const calculateDimensions = (
size: ICardSize,
mainWidth: number,
mainHeight: number,
innerHeight: number,
): number => {
const [i, j] = getStacksGrid(mainWidth, innerHeight);
const tableWidthEm =
stackWidthEm(4, size) * i + (size !== "small" ? 0.666 : 0.333) * 10;
const fontSizeW = (mainWidth * 0.95) / tableWidthEm;

const tableHeightEm =
getCardDimensionsEm(size).height * j + (size !== "small" ? 0.5 : 0.25) * 6;
): TableDimensions => {
// Limit play area aspect ratio to 16:9
const tableHeight = mainHeight > 600 ? mainHeight : innerHeight;
const tableWidth = Math.min(mainWidth, tableHeight * (16 / 9));

const [columns, rows] = getStacksGrid(mainWidth, innerHeight);
const [tableWidthEm, tableHeightEm] = calculateTableEmSize(
size,
columns,
rows,
);

const fontSizeW = (tableWidth * 0.95) / tableWidthEm;
const fontSizeH = (tableHeight * 0.95) / tableHeightEm;

return Math.min(fontSizeW, fontSizeH);
return {
fontSize: Math.min(fontSizeW, fontSizeH),
columns,
rows,
tableWidthEm,
tableHeightEm,
};
};

export type Piles = Card[][];
Expand Down
15 changes: 10 additions & 5 deletions src/views/Pileon/Pileon.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
drop,
autoMove,
getEqualValues,
calculateFontSize,
calculateDimensions,
fillerStacks,
} from "../../utils/pileon";
import { getStackDataTransfer } from "../../utils/stack";
Expand Down Expand Up @@ -137,8 +137,7 @@
};

$: size = $appearance.size;
$: fontSize = calculateFontSize(size, mainWidth, mainHeight, innerHeight);
$: style = `font-size: ${fontSize}px`;
$: d = calculateDimensions(size, mainWidth, mainHeight, innerHeight);

onMount(() => {
actions.update((prev) => ({ ...prev, help, shuffle, undo }));
Expand All @@ -160,7 +159,8 @@
bind:clientHeight={mainHeight}
bind:clientWidth={mainWidth}
class="pileon"
{style}
style:font-size="{d.fontSize}px"
style:width="{d.tableWidthEm * 1.25}em"
>
{#each piles as pile, index}
<div class="pile" class:small={size === "small"}>
Expand Down Expand Up @@ -196,12 +196,17 @@
flex-wrap: wrap
font-size: 0.7em
display: flex
align-content: center
align-items: center
justify-content: center
max-width: 100vw
row-gap: 0.5em
margin: auto

@media (max-aspect-ratio: 9/17)
row-gap: 1em

.pile
margin: 0.5em 0.666em
flex-basis: 16.7% /* Just above 1/6 */

&.small
Expand Down
Loading