From f9e4a2a7959773f0f7229988a13b10259be020d5 Mon Sep 17 00:00:00 2001 From: ryo-ma Date: Sat, 19 Sep 2020 22:32:04 +0900 Subject: [PATCH] Support margin option --- index.ts | 12 ++++++------ src/card.ts | 14 ++++++++------ src/utils.ts | 4 ++-- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/index.ts b/index.ts index 9176136..306c63e 100644 --- a/index.ts +++ b/index.ts @@ -17,13 +17,13 @@ export default async (req: ServerRequest) => { if (Object.keys(COLORS).includes(themeParam)) { theme = COLORS[themeParam] } - const paddingWidth = params.getNumberValue( - "padding-w", - CONSTANTS.DEFAULT_PADDING_W, + const marginWidth = params.getNumberValue( + "margin-w", + CONSTANTS.DEFAULT_MARGIN_W, ); const paddingHeight = params.getNumberValue( - "padding-h", - CONSTANTS.DEFAULT_PADDING_H, + "margin-h", + CONSTANTS.DEFAULT_MARGIN_H, ); const titles: Array = params.getAll("title").flatMap((r) => r.split(",") @@ -62,7 +62,7 @@ export default async (req: ServerRequest) => { column, row, CONSTANTS.DEFAULT_PANEL_SIZE, - paddingWidth, + marginWidth, paddingHeight, ).render(userInfo, theme), headers: new Headers( diff --git a/src/card.ts b/src/card.ts index df9fe2f..f52ec37 100644 --- a/src/card.ts +++ b/src/card.ts @@ -25,10 +25,10 @@ export class Card { private maxColumn: number, private maxRow: number, private panelSize: number, - private paddingWidth: number, - private paddingHight: number, + private marginWidth: number, + private marginHight: number, ) { - this.width = panelSize * this.maxColumn; + this.width = panelSize * this.maxColumn + this.marginWidth * (this.maxColumn - 1); } render( userInfo: UserInfo, @@ -83,13 +83,15 @@ export class Card { if (row > this.maxRow) { row = this.maxRow; } - this.height = this.panelSize * row; + this.height = this.panelSize * row + this.marginHight * (row - 1); // Join all trophy const renderedTrophy = trophyList.reduce( (sum: string, trophy: Trophy, i: number) => { - const x = this.panelSize * (i % this.maxColumn); - const y = this.panelSize * Math.floor(i / this.maxColumn); + const currentColumn = i % this.maxColumn; + const currentRow = Math.floor(i / this.maxColumn); + const x = this.panelSize * currentColumn + this.marginWidth * currentColumn; + const y = this.panelSize * currentRow + this.marginHight * currentRow; return sum + trophy.render(theme, x, y, this.panelSize); }, "", diff --git a/src/utils.ts b/src/utils.ts index 8f9a2e9..17fa348 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -51,8 +51,8 @@ export const CONSTANTS = { DEFAULT_PANEL_SIZE: 110, DEFAULT_MAX_COLUMN: 6, DEFAULT_MAX_ROW: 3, - DEFAULT_PADDING_W: 0, - DEFAULT_PADDING_H: 0, + DEFAULT_MARGIN_W: 0, + DEFAULT_MARGIN_H: 0, }; export enum RANK {