allows to select palette in demo

This commit is contained in:
platane
2023-01-06 09:11:59 +01:00
committed by Platane
parent 38e2ed4f23
commit e2eb91cf8f
2 changed files with 60 additions and 21 deletions

View File

@@ -1,6 +1,6 @@
import { DrawOptions as DrawOptions } from "@snk/svg-creator"; import { DrawOptions as DrawOptions } from "@snk/svg-creator";
export const palettes: Record< export const basePalettes: Record<
string, string,
Pick< Pick<
DrawOptions, DrawOptions,
@@ -22,6 +22,7 @@ export const palettes: Record<
}; };
// aliases // aliases
export const palettes = { ...basePalettes };
palettes["github"] = { palettes["github"] = {
...palettes["github-light"], ...palettes["github-light"],
dark: { ...palettes["github-dark"] }, dark: { ...palettes["github-dark"] },

View File

@@ -14,6 +14,7 @@ import { createSvg } from "@snk/svg-creator";
import { createRpcClient } from "./worker-utils"; import { createRpcClient } from "./worker-utils";
import type { API as WorkerAPI } from "./demo.interactive.worker"; import type { API as WorkerAPI } from "./demo.interactive.worker";
import { AnimationOptions } from "@snk/gif-creator"; import { AnimationOptions } from "@snk/gif-creator";
import { basePalettes } from "@snk/action/palettes";
const createForm = ({ const createForm = ({
onSubmit, onSubmit,
@@ -119,13 +120,18 @@ const createViewer = ({
grid0, grid0,
chain, chain,
cells, cells,
drawOptions,
}: { }: {
grid0: Grid; grid0: Grid;
chain: Snake[]; chain: Snake[];
cells: Point[]; cells: Point[];
drawOptions: DrawOptions;
}) => { }) => {
const drawOptions: DrawOptions = {
sizeDotBorderRadius: 2,
sizeCell: 16,
sizeDot: 12,
...basePalettes["github-light"],
};
// //
// canvas // canvas
const canvas = document.createElement("canvas"); const canvas = document.createElement("canvas");
@@ -171,12 +177,12 @@ const createViewer = ({
// //
// controls // controls
const input = document.createElement("input") as any; const input = document.createElement("input");
input.type = "range"; input.type = "range";
input.value = 0; input.value = "0";
input.step = 1; input.step = "1";
input.min = 0; input.min = "0";
input.max = chain.length; input.max = "" + chain.length;
input.style.width = "calc( 100% - 20px )"; input.style.width = "calc( 100% - 20px )";
input.addEventListener("input", () => { input.addEventListener("input", () => {
spring.target = +input.value; spring.target = +input.value;
@@ -190,10 +196,49 @@ const createViewer = ({
window.addEventListener("click", onClickBackground); window.addEventListener("click", onClickBackground);
document.body.append(input); document.body.append(input);
//
const schemaSelect = document.createElement("select");
schemaSelect.style.margin = "10px";
schemaSelect.style.alignSelf = "flex-start";
schemaSelect.value = "github-light";
schemaSelect.addEventListener("change", () => {
Object.assign(drawOptions, basePalettes[schemaSelect.value]);
svgString = createSvg(grid0, cells, chain, drawOptions, {
frameDuration: 100,
} as AnimationOptions);
const svgImageUri = `data:image/*;charset=utf-8;base64,${btoa(svgString)}`;
svgLink.href = svgImageUri;
if (schemaSelect.value.includes("dark"))
document.body.parentElement?.classList.add("dark-mode");
else document.body.parentElement?.classList.remove("dark-mode");
loop();
});
for (const name of Object.keys(basePalettes)) {
const option = document.createElement("option");
option.value = name;
option.innerText = name;
schemaSelect.appendChild(option);
}
document.body.append(schemaSelect);
//
// dark mode
const style = document.createElement("style");
style.innerText = `
html { transition:background-color 180ms }
a { transition:color 180ms }
html.dark-mode{ background-color:#0d1117 }
html.dark-mode a{ color:rgb(201, 209, 217) }
`;
document.head.append(style);
// //
// svg // svg
const svgLink = document.createElement("a"); const svgLink = document.createElement("a");
const svgString = createSvg(grid0, cells, chain, drawOptions, { let svgString = createSvg(grid0, cells, chain, drawOptions, {
frameDuration: 100, frameDuration: 100,
} as AnimationOptions); } as AnimationOptions);
const svgImageUri = `data:image/*;charset=utf-8;base64,${btoa(svgString)}`; const svgImageUri = `data:image/*;charset=utf-8;base64,${btoa(svgString)}`;
@@ -203,7 +248,10 @@ const createViewer = ({
svgLink.addEventListener("click", (e) => { svgLink.addEventListener("click", (e) => {
const w = window.open("")!; const w = window.open("")!;
w.document.write( w.document.write(
`<a href="${svgImageUri}" download="github-user-contribution.svg">` + (document.body.parentElement?.classList.contains("dark-mode")
? "<style>html{ background-color:#0d1117 }</style>"
: "") +
`<a href="${svgLink.href}" download="github-user-contribution.svg">` +
svgString + svgString +
"<a/>" "<a/>"
); );
@@ -233,23 +281,13 @@ const onSubmit = async (userName: string) => {
); );
const cells = (await res.json()) as Res; const cells = (await res.json()) as Res;
const drawOptions: DrawOptions = {
sizeDotBorderRadius: 2,
sizeCell: 16,
sizeDot: 12,
colorDotBorder: "#1b1f230a",
colorDots: ["#ebedf0", "#9be9a8", "#40c463", "#30a14e", "#216e39"],
colorEmpty: "#ebedf0",
colorSnake: "purple",
};
const grid = userContributionToGrid(cells); const grid = userContributionToGrid(cells);
const chain = await getChain(grid); const chain = await getChain(grid);
dispose(); dispose();
createViewer({ grid0: grid, chain, cells, drawOptions }); createViewer({ grid0: grid, chain, cells });
}; };
const worker = new Worker( const worker = new Worker(