{"spec_id":"chessboard-pieces","library":"muix","language":"javascript","code":"// anyplot.ai\n// chessboard-pieces: Chess Board with Pieces for Position Diagrams\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 89/100 | Updated: 2026-11-08\n//# anyplot-orientation: square\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { useDrawingArea, useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst FILES = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\"];\nconst RANKS = [\"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\"];\nconst SQUARE_COLOR = { light: t.amber, dark: t.palette[3] }; // light and dark wood, the same in both themes\n// Pieces keep fixed inks so white and black read the same on both square colors in both themes\nconst PIECE_LIGHT = \"#FAF8F1\";\nconst PIECE_DARK = \"#1A1A17\";\nconst MARK_COLOR = t.palette[4]; // semantic: matte red frames the squares the position marks\nconst PIECE_ORDER = \"KQRBNP\"; // white glyphs U+2654-2659 and black glyphs U+265A-265F follow this order\nconst TEXT_STYLE = \"︎\"; // asks for the text glyph, not an emoji\nconst GLYPH_SHARE = 0.8; // glyph size as a share of the square, leaves an inset to the edges\nconst HEADER_H = 96;\nconst AXIS_SIZE = 48;\nconst PAD = 20;\n\n// Data: replace this block with your own parameters\n// Position after Scholar's Mate (1.e4 e5 2.Qh5 Nc6 3.Bc4 Nf6?? 4.Qxf7#); uppercase is white, lowercase black\nconst POSITION = \"Scholar's Mate\"; // role: extra\nconst PIECES: Record<string, string> = {\n  e1: \"K\", f7: \"Q\", a1: \"R\", h1: \"R\", c1: \"B\", c4: \"B\", b1: \"N\", g1: \"N\",\n  a2: \"P\", b2: \"P\", c2: \"P\", d2: \"P\", e4: \"P\", f2: \"P\", g2: \"P\", h2: \"P\",\n  e8: \"k\", d8: \"q\", a8: \"r\", h8: \"r\", c8: \"b\", f8: \"b\", c6: \"n\", f6: \"n\",\n  a7: \"p\", b7: \"p\", c7: \"p\", d7: \"p\", e5: \"p\", g7: \"p\", h7: \"p\",\n}; // role: pieces\nconst MARKED = [\"f7\", \"e8\"]; // role: extra\nconst MARK_NOTE = \"Qxf7#: the white queen on f7 checkmates the black king on e8\"; // role: extra\n\n// Plot: reads only the parameters and the constants above\nconst ranksTopToBottom = [...RANKS].reverse();\nconst marked = new Set(MARKED);\n// a1 is dark, so a square is light when file index + rank number is even (h1 and a8 light)\nconst squares = ranksTopToBottom.flatMap((rank) =>\n  FILES.map((file, fIdx) => ({\n    id: `${file}${rank}`,\n    file,\n    rank,\n    fill: (fIdx + Number(rank)) % 2 === 0 ? SQUARE_COLOR.light : SQUARE_COLOR.dark,\n  })),\n);\nconst pieces = Object.entries(PIECES).map(([square, code]) => {\n  const index = PIECE_ORDER.indexOf(code.toUpperCase());\n  return {\n    square,\n    file: square[0],\n    rank: square.slice(1),\n    white: code === code.toUpperCase(),\n    outline: String.fromCodePoint(0x2654 + index) + TEXT_STYLE,\n    solid: String.fromCodePoint(0x265a + index) + TEXT_STYLE,\n  };\n});\nconst whiteCount = pieces.filter((p) => p.white).length;\n\nconst TITLE = `${POSITION} · chessboard-pieces · javascript · muix · anyplot.ai`;\nconst subtitle = `${whiteCount} white and ${pieces.length - whiteCount} black pieces · framed: ${MARK_NOTE}`;\n\nfunction BoardLayer() {\n  const xScale = useXScale(\"file\");\n  const yScale = useYScale(\"rank\");\n  const { left, top, width, height } = useDrawingArea();\n  const cell = xScale.bandwidth();\n  return (\n    <g>\n      {squares.map((sq) => (\n        <rect key={sq.id} x={xScale(sq.file)} y={yScale(sq.rank)} width={cell} height={cell} fill={sq.fill} />\n      ))}\n      {squares\n        .filter((sq) => marked.has(sq.id))\n        .map((sq) => (\n          <rect\n            key={`mark-${sq.id}`}\n            x={xScale(sq.file) + 3}\n            y={yScale(sq.rank) + 3}\n            width={cell - 6}\n            height={cell - 6}\n            fill=\"none\"\n            stroke={MARK_COLOR}\n            strokeWidth={5}\n          />\n        ))}\n      <rect x={left} y={top} width={width} height={height} fill=\"none\" stroke={t.ink} strokeWidth={3} />\n    </g>\n  );\n}\n\n// White pieces: the white glyph's outline over a light fill of the solid shape; black pieces: the solid glyph\nfunction PieceLayer() {\n  const xScale = useXScale(\"file\");\n  const yScale = useYScale(\"rank\");\n  const cell = xScale.bandwidth();\n  const glyph = {\n    textAnchor: \"middle\" as const,\n    dominantBaseline: \"central\" as const,\n    fontSize: cell * GLYPH_SHARE,\n    fontFamily: \"'DejaVu Sans', 'Noto Sans Symbols 2', 'Segoe UI Symbol', sans-serif\",\n  };\n  return (\n    <g pointerEvents=\"none\">\n      {pieces.map((p) => {\n        const x = xScale(p.file) + cell / 2;\n        const y = yScale(p.rank) + cell / 2;\n        return p.white ? (\n          <g key={p.square}>\n            <text x={x} y={y} fill={PIECE_LIGHT} stroke={PIECE_LIGHT} strokeWidth={cell * 0.06} strokeLinejoin=\"round\" {...glyph}>\n              {p.solid}\n            </text>\n            <text x={x} y={y} fill={PIECE_DARK} {...glyph}>\n              {p.outline}\n            </text>\n          </g>\n        ) : (\n          <text key={p.square} x={x} y={y} fill={PIECE_DARK} {...glyph}>\n            {p.solid}\n          </text>\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  // The rank labels take an axis band on the left only; a matching right margin keeps the board centered\n  const containerSide = Math.min(width - AXIS_SIZE, height - HEADER_H);\n\n  return (\n    <div style={{ width, height, background: t.pageBg, display: \"flex\", flexDirection: \"column\", alignItems: \"center\" }}>\n      <div style={{ height: HEADER_H, display: \"flex\", flexDirection: \"column\", alignItems: \"center\", justifyContent: \"center\", gap: 6, paddingTop: 6 }}>\n        <div style={{ fontSize: 22, fontWeight: 600, color: t.ink }}>{TITLE}</div>\n        <div style={{ fontSize: 15, color: t.inkSoft }}>{subtitle}</div>\n      </div>\n      <ChartsContainer\n        width={containerSide + AXIS_SIZE}\n        height={containerSide}\n        margin={{ top: PAD, right: PAD + AXIS_SIZE, bottom: PAD, left: PAD }}\n        skipAnimation\n        series={[]}\n        xAxis={[\n          {\n            id: \"file\",\n            scaleType: \"band\",\n            data: FILES,\n            categoryGapRatio: 0,\n            height: AXIS_SIZE,\n            disableLine: true,\n            disableTicks: true,\n            tickLabelStyle: { fontSize: 16, fontWeight: 600, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"rank\",\n            scaleType: \"band\",\n            data: ranksTopToBottom,\n            categoryGapRatio: 0,\n            width: AXIS_SIZE,\n            disableLine: true,\n            disableTicks: true,\n            tickLabelStyle: { fontSize: 16, fontWeight: 600, fill: t.inkSoft },\n          },\n        ]}\n      >\n        <BoardLayer />\n        <PieceLayer />\n        <ChartsXAxis axisId=\"file\" />\n        <ChartsYAxis axisId=\"rank\" />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}