Skip to content

Commit

Permalink
feat: optimize rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
rei1024 committed Nov 2, 2024
1 parent 7a50adf commit b6de63b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
50 changes: 26 additions & 24 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,30 @@ const frequencyList = [
300, 400, 500,
];

export function dataToColor(
export function makeColorMap(
list: number[],
data: number,
style: "hue" | "heat"
) {
const index = list.findIndex((t) => t === data) ?? 0;
// 80% 0.1
// return `oklch(92% 0.35 ${value * 360})`;
if (style === "hue") {
const value = index / list.length;
return `lch(70% 70 ${value * 360})`;
} else if (style === "heat") {
// make red
const value = (index + 1) / list.length;
// Heat map
const h = (1 - value) * 240;
return "hsl(" + h + " 100% 70%)";
} else {
throw new Error("unknown style");
}
): Map<number, string> {
const len = list.length;
return new Map(
list.map((x, index) => {
let color: string;
if (style === "hue") {
const value = index / len;
color = `lch(70% 70 ${value * 360})`;
} else if (style === "heat") {
// make red
const value = (index + 1) / len;
// Heat map
const h = (1 - value) * 240;
color = "hsl(" + h + " 100% 70%)";
} else {
throw new Error("unknown style");
}

// return `hsl(${value * 360} 100% 70%)`;
return [x, color];
})
);
}

export class App {
Expand Down Expand Up @@ -111,15 +113,15 @@ export class App {

const mapData = this.getMapData();
const list = mapData.list;
const colorMap = makeColorMap(
list,
this.mapType === "heat" ? "heat" : "hue"
);
for (const [y, row] of mapData.data.entries()) {
for (const [x, p] of row.entries()) {
if (p >= (this.mapType === "heat" ? 0 : 1)) {
ctx.beginPath();
ctx.fillStyle = dataToColor(
list,
p,
this.mapType === "heat" ? "heat" : "hue"
);
ctx.fillStyle = colorMap.get(p) ?? "";
ctx.rect(
(x - dx + safeArea) * cellSize,
(y - dy + safeArea) * cellSize,
Expand Down
5 changes: 3 additions & 2 deletions src/ui/colorTable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dataToColor } from "../app";
import { makeColorMap } from "../app";
import { $colorTable } from "../bind";
import { displayMapTypeTitle, type MapType } from "./core";

Expand Down Expand Up @@ -29,9 +29,10 @@ export function setColorTable(
$colorTable.append(trHead);
}

const colorMap = makeColorMap(list, mapType === "heat" ? "heat" : "hue");
for (const item of list) {
const row = document.createElement("tr");
const color = dataToColor(list, item, mapType === "heat" ? "heat" : "hue");
const color = colorMap.get(item) ?? "";
const $color = document.createElement("td");
$color.style.backgroundColor = color;
$color.style.width = "40px";
Expand Down

0 comments on commit b6de63b

Please sign in to comment.