Skip to content

Commit a041d6c

Browse files
committed
✨ demo background color
1 parent 9cb4161 commit a041d6c

1 file changed

Lines changed: 40 additions & 22 deletions

File tree

packages/demo/interactive/index.ts

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import {
44
getCanvasWorldSize,
55
} from "@snk/draw/drawWorld";
66
import type { Res } from "@snk/github-user-contribution";
7-
import { cellsToGrid } from "generate-snake-animation/cellsToGrid";
8-
import { parseEntry } from "generate-snake-animation/outputsOptions";
9-
import { basePalettes } from "generate-snake-animation/palettes";
107
import { step } from "@snk/solver/step";
118
import { createSvg } from "@snk/svg-creator";
129
import { Color, copyGrid, Grid } from "@snk/types/grid";
1310
import type { Point } from "@snk/types/point";
1411
import type { Snake } from "@snk/types/snake";
12+
import { cellsToGrid } from "generate-snake-animation/cellsToGrid";
13+
import { parseEntry } from "generate-snake-animation/outputsOptions";
14+
import { basePalettes } from "generate-snake-animation/palettes";
1515
import { isStableAndBound, stepSpring } from "./springUtils";
1616
import type { API as WorkerAPI } from "./worker";
1717
import { createRpcClient } from "./worker-utils";
@@ -169,11 +169,13 @@ const createViewer = ({
169169
const k = spring.x % 1;
170170

171171
ctx.clearRect(0, 0, 9999, 9999);
172-
drawLerpWorld(ctx, grid, cells, snake0, snake1, stack, k, drawOptions);
172+
drawLerpWorld(ctx, grid, cells, snake0, snake1, stack, k, {
173+
...drawOptions,
174+
colorBackground: undefined,
175+
});
173176

174177
if (!stable) animationFrame = requestAnimationFrame(loop);
175178
};
176-
loop();
177179

178180
//
179181
// controls
@@ -200,18 +202,36 @@ const createViewer = ({
200202
const applyDrawOptions = (options: Partial<DrawOptions>) => {
201203
Object.assign(drawOptions, options);
202204

203-
svgString = createSvg(grid0, cells, chain, drawOptions, {
205+
const svgContent = createSvg(grid0, cells, chain, drawOptions, {
204206
stepDurationMs: 100,
205207
});
206-
svgLink.href = `data:image/*;charset=utf-8;base64,${btoa(svgString)}`;
208+
svgLink.href = `data:image/*;charset=utf-8;base64,${btoa(svgContent)}`;
209+
svgLink.setAttribute("data-svg-content", svgContent);
210+
svgLink.setAttribute(
211+
"background-color",
212+
drawOptions.colorBackground ?? "auto",
213+
);
207214

208215
if (
209216
Object.entries(basePalettes)
210-
.find(([_, o]) => o.colorEmpty === drawOptions.colorEmpty)?.[0]
217+
.find(
218+
([_, o]) =>
219+
o.colorEmpty === drawOptions.colorEmpty &&
220+
o.colorBackground === drawOptions.colorBackground,
221+
)?.[0]
211222
.includes("-dark")
212-
)
223+
) {
213224
document.body.parentElement?.classList.add("dark-mode");
214-
else document.body.parentElement?.classList.remove("dark-mode");
225+
} else document.body.parentElement?.classList.remove("dark-mode");
226+
227+
if (drawOptions.colorBackground)
228+
document.body.parentElement?.style.setProperty(
229+
"--background-color",
230+
drawOptions.colorBackground,
231+
);
232+
else {
233+
document.body.parentElement?.style.removeProperty("--background-color");
234+
}
215235

216236
loop();
217237
};
@@ -259,31 +279,27 @@ const createViewer = ({
259279
// dark mode
260280
const style = document.createElement("style");
261281
style.innerText = `
262-
html { transition:background-color 180ms }
282+
html {
283+
transition:background-color 180ms;
284+
background-color:var(--background-color);
285+
}
263286
a { transition:color 180ms }
264-
html.dark-mode{ background-color:#0d1117 }
265-
html.dark-mode a{ color:rgb(201, 209, 217) }
287+
html.dark-mode a{ color:white }
288+
html.dark-mode { --background-color:#0d1117; }
266289
`;
267290
document.head.append(style);
268291

269292
//
270293
// svg
271294
const svgLink = document.createElement("a");
272-
let svgString = createSvg(grid0, cells, chain, drawOptions, {
273-
stepDurationMs: 100,
274-
});
275-
const svgImageUri = `data:image/*;charset=utf-8;base64,${btoa(svgString)}`;
276-
svgLink.href = svgImageUri;
277295
svgLink.innerText = "github-user-contribution.svg";
278296
svgLink.download = "github-user-contribution.svg";
279297
svgLink.addEventListener("click", (e) => {
280298
const w = window.open("")!;
281299
w.document.write(
282-
(document.body.parentElement?.classList.contains("dark-mode")
283-
? "<style>html{ background-color:#0d1117 }</style>"
284-
: "") +
300+
`<style>html{ background-color: ${svgLink.getAttribute("background-color") ?? "auto"} }</style>` +
285301
`<a href="${svgLink.href}" download="github-user-contribution.svg">` +
286-
svgString +
302+
svgLink.getAttribute("data-svg-content") +
287303
"<a/>",
288304
);
289305
e.preventDefault();
@@ -293,6 +309,8 @@ const createViewer = ({
293309
svgLink.style.alignSelf = "flex-start";
294310
document.body.append(svgLink);
295311

312+
applyDrawOptions(drawOptions);
313+
296314
//
297315
// dispose
298316
const dispose = () => {

0 commit comments

Comments
 (0)