diff --git a/src/encoders/createImage.ts b/src/encoders/createImage.ts new file mode 100644 index 0000000..995b278 --- /dev/null +++ b/src/encoders/createImage.ts @@ -0,0 +1,20 @@ +import { BoardConfig } from "../types"; +import Board from "../board/Board"; +import Game from "../game/Game"; + +const createImage = async ( + fen: string, + boardConfig: BoardConfig, + size: number +) => { + const game = new Game().loadFEN(fen); + const board = new Board({ ...boardConfig, size }); + + const position = game.getPosition(0); + await board.frame(position, {}); + board.render(); + + return board.toImgUrl(); +}; + +export default createImage; diff --git a/src/main.tsx b/src/main.tsx index 5eeb381..34eb750 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -13,6 +13,8 @@ import { state, setState } from "./state"; import { render } from "solid-js/web"; import App from "./ui/App"; +import download from "./utils/download"; +import createImage from "./encoders/createImage"; const boardConfig: BoardConfig = { size: 1024, @@ -34,7 +36,8 @@ const gameConfig: GameConfig = { toPly: null, loop: true, format: "GIF", - size: "M", + picSize: "M", + animationSize: "M", }; const createDownloadLink = async (pgn: string, boardConfig: BoardConfig) => { @@ -114,7 +117,7 @@ const main = async () => { }, async loadPGN(pgn: string) { const game = new Game().loadPGN(pgn); - setState({ pgn, fen: null, moves: game.getMoves() }); + setState({ pgn, fen: "", moves: game.getMoves() }); await player.load(game); }, async loadFEN(fen: string) { @@ -122,6 +125,10 @@ const main = async () => { setState({ pgn: null, fen, moves: game.getMoves() }); await player.load(game); }, + async downloadImage() { + const data = await createImage(state.fen, state.board, 1024); + download(data, "fen", "png"); + }, }; // @ts-ignore diff --git a/src/state.ts b/src/state.ts index 2571020..4fa9db2 100644 --- a/src/state.ts +++ b/src/state.ts @@ -21,7 +21,8 @@ const gameConfig: GameConfig = { toPly: null, loop: true, format: "GIF", - size: "M", + picSize: "M", + animationSize: "M", }; export type State = { diff --git a/src/types.ts b/src/types.ts index 607e4ab..24dca1a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -137,7 +137,8 @@ export type GameConfig = { toPly: number | null; loop: boolean; format: "GIF" | "MP4" | "WebM"; - size: "XS" | "S" | "M" | "L" | "XL"; + picSize: "XS" | "S" | "M" | "L" | "XL"; + animationSize: "XS" | "S" | "M" | "L" | "XL"; }; export type MaterialCount = { @@ -203,4 +204,5 @@ export type Handlers = { changePiecesStyle: (style: PiecesStyle) => void; loadPGN: (pgn: string) => Promise; loadFEN: (fen: string) => Promise; + downloadImage: () => void; }; diff --git a/src/ui/App.css b/src/ui/App.css index a3c153c..24d11e4 100644 --- a/src/ui/App.css +++ b/src/ui/App.css @@ -62,6 +62,13 @@ h2 { margin: 25px 0 10px 0; } +h3 { + color: #aaa; + text-align: left; + font-size: 1.6rem; + margin: 15px 0 10px 0; +} + .dark { background-color: #191d24; /* background-image: url(src/ui/img/pattern.png); */ diff --git a/src/ui/components/Share.css b/src/ui/components/Share.css index b4430fb..08fa781 100644 --- a/src/ui/components/Share.css +++ b/src/ui/components/Share.css @@ -40,3 +40,8 @@ .share__btn { margin-bottom: 10px; } + +.header--first { + margin-top: 0; + margin-bottom: 25px; +} diff --git a/src/ui/components/Share.tsx b/src/ui/components/Share.tsx index bbf2833..4a06ce7 100644 --- a/src/ui/components/Share.tsx +++ b/src/ui/components/Share.tsx @@ -1,14 +1,21 @@ -import { Component } from "solid-js"; +import { Component, createSignal } from "solid-js"; import { Handlers } from "../../types"; import Scrollable from "./reusable/Scrollable"; import { state, setState } from "../../state"; import "./Share.css"; const Share: Component<{ handlers: Handlers }> = (props) => { + const [copyId, setCopyId] = createSignal(""); + + const blinkCopy = (id: string) => { + setCopyId(id); + setTimeout(() => setCopyId(""), 1000); + }; + return ( ); diff --git a/src/ui/components/sizeToPX.ts b/src/ui/components/sizeToPX.ts new file mode 100644 index 0000000..11b69ae --- /dev/null +++ b/src/ui/components/sizeToPX.ts @@ -0,0 +1,9 @@ +const sizeToPX = { + XS: "256", + S: "512", + M: "720", + L: "1024", + XL: "1440", +}; + +export default sizeToPX; diff --git a/src/utils/download.ts b/src/utils/download.ts new file mode 100644 index 0000000..0f524be --- /dev/null +++ b/src/utils/download.ts @@ -0,0 +1,10 @@ +const download = (data: string | Blob, name: string, ext: string) => { + const url = typeof data === "string" ? data : URL.createObjectURL(data); + + const link = document.createElement("a"); + link.href = url; + link.download = `${name}_${Date.now()}.${ext}`; + link.click(); +}; + +export default download;