{"spec_id":"heatmap-mandelbrot","library":"muix","language":"javascript","code":"// anyplot.ai\n// heatmap-mandelbrot: Mandelbrot Set Fractal Visualization\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 86/100 | Updated: 2026-10-24\n//# anyplot-orientation: square\nimport * as React from \"react\";\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { useDrawingArea } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst { width, height } = window.ANYPLOT_SIZE;\nconst TITLE = \"heatmap-mandelbrot · javascript · muix · anyplot.ai\";\nconst ESCAPE_RADIUS_SQ = 4;\nconst COLOR_GAMMA = 0.42;\nconst INTERIOR_HEX = \"#1A1A17\";\nconst LUT_SIZE = 512;\nconst GRADIENT_STOPS = [0, 0.25, 0.5, 0.75, 1];\n\nconst BASE_TOP = 96;\nconst BASE_BOTTOM = 164;\nconst BASE_LEFT = 80;\nconst BASE_RIGHT = 160;\nconst X_AXIS_H = 60;\nconst Y_AXIS_W = 80;\n\n// Data: replace this block with your own parameters\nconst X_MIN = -2.0; // role: x_min\nconst X_MAX = 0.5; // role: x_max\nconst Y_MIN = -1.25; // role: y_min\nconst Y_MAX = 1.25; // role: y_max\nconst MAX_ITERATIONS = 100; // role: max_iterations\n\n// Plot: reads only data and the constants above\nconst seqLowNum = parseInt(t.seq[0].slice(1), 16);\nconst seqHighNum = parseInt(t.seq[1].slice(1), 16);\nconst lowR = (seqLowNum >> 16) & 255;\nconst lowG = (seqLowNum >> 8) & 255;\nconst lowB = seqLowNum & 255;\nconst highR = (seqHighNum >> 16) & 255;\nconst highG = (seqHighNum >> 8) & 255;\nconst highB = seqHighNum & 255;\n\nconst interiorNum = parseInt(INTERIOR_HEX.slice(1), 16);\nconst interiorR = (interiorNum >> 16) & 255;\nconst interiorG = (interiorNum >> 8) & 255;\nconst interiorB = interiorNum & 255;\n\nconst COLOR_LUT: Array<[number, number, number]> = [];\nfor (let i = 0; i < LUT_SIZE; i++) {\n  const frac = i / (LUT_SIZE - 1);\n  const gFrac = Math.pow(frac, COLOR_GAMMA);\n  COLOR_LUT.push([\n    Math.round(lowR + (highR - lowR) * gFrac),\n    Math.round(lowG + (highG - lowG) * gFrac),\n    Math.round(lowB + (highB - lowB) * gFrac),\n  ]);\n}\n\nconst stopColors = GRADIENT_STOPS.map((stop) => {\n  const idx = Math.round(stop * (LUT_SIZE - 1));\n  const [r, g, b] = COLOR_LUT[idx];\n  return { offset: `${stop * 100}%`, color: `rgb(${r},${g},${b})` };\n});\n\nconst SUBTITLE = `z(n+1) = z(n)² + c · Re(c) ∈ [${X_MIN.toFixed(2)}, ${X_MAX.toFixed(2)}], Im(c) ∈ [${Y_MIN.toFixed(2)}, ${Y_MAX.toFixed(2)}] · max ${MAX_ITERATIONS} iterations`;\n\nconst maxAreaW = width - BASE_LEFT - BASE_RIGHT - Y_AXIS_W;\nconst maxAreaH = height - BASE_TOP - BASE_BOTTOM - X_AXIS_H;\nconst complexAspect = (X_MAX - X_MIN) / (Y_MAX - Y_MIN);\nconst targetAreaW = complexAspect >= 1 ? maxAreaW : Math.round(maxAreaH * complexAspect);\nconst targetAreaH = complexAspect >= 1 ? Math.round(maxAreaW / complexAspect) : maxAreaH;\nconst extraW = maxAreaW - targetAreaW;\nconst extraH = maxAreaH - targetAreaH;\nconst margin = {\n  top: BASE_TOP + Math.floor(extraH / 2),\n  bottom: BASE_BOTTOM + Math.ceil(extraH / 2),\n  left: BASE_LEFT + Math.floor(extraW / 2),\n  right: BASE_RIGHT + Math.ceil(extraW / 2),\n};\n\nfunction MandelbrotLayer() {\n  const { left, top, width: areaW, height: areaH } = useDrawingArea();\n  const canvasRef = React.useRef<HTMLCanvasElement | null>(null);\n\n  React.useLayoutEffect(() => {\n    const canvas = canvasRef.current;\n    if (!canvas) return;\n    const dpr = Math.min(window.devicePixelRatio || 1, 2);\n    const pxW = Math.round(areaW * dpr);\n    const pxH = Math.round(areaH * dpr);\n    canvas.width = pxW;\n    canvas.height = pxH;\n    const ctx = canvas.getContext(\"2d\")!;\n    const image = ctx.createImageData(pxW, pxH);\n    const pixels = image.data;\n\n    for (let py = 0; py < pxH; py++) {\n      const im0 = Y_MAX - (py / (pxH - 1)) * (Y_MAX - Y_MIN);\n      for (let px = 0; px < pxW; px++) {\n        const re0 = X_MIN + (px / (pxW - 1)) * (X_MAX - X_MIN);\n\n        let zr = 0;\n        let zi = 0;\n        let n = 0;\n        while (n < MAX_ITERATIONS && zr * zr + zi * zi <= ESCAPE_RADIUS_SQ) {\n          const zr2 = zr * zr - zi * zi + re0;\n          zi = 2 * zr * zi + im0;\n          zr = zr2;\n          n++;\n        }\n\n        const idx = (py * pxW + px) * 4;\n        if (n >= MAX_ITERATIONS) {\n          pixels[idx] = interiorR;\n          pixels[idx + 1] = interiorG;\n          pixels[idx + 2] = interiorB;\n        } else {\n          const magSq = zr * zr + zi * zi;\n          const smoothN =\n            n + 1 - Math.log(Math.log(Math.sqrt(magSq))) / Math.LN2;\n          const frac = Math.max(0, Math.min(1, smoothN / MAX_ITERATIONS));\n          const lutIdx = Math.round(frac * (LUT_SIZE - 1));\n          const [r, g, b] = COLOR_LUT[lutIdx];\n          pixels[idx] = r;\n          pixels[idx + 1] = g;\n          pixels[idx + 2] = b;\n        }\n        pixels[idx + 3] = 255;\n      }\n    }\n    ctx.putImageData(image, 0, 0);\n  }, [areaW, areaH]);\n\n  const drawingBottom = top + areaH;\n  const barX = left;\n  const barW = areaW * 0.62;\n  const barH = 22;\n  const titleY = drawingBottom + 86;\n  const barY = titleY + 12;\n  const endLabelY = barY + barH + 22;\n  const swatchSize = 22;\n  const swatchX = left + areaW * 0.72;\n  const swatchY = barY;\n\n  return (\n    <g>\n      <defs>\n        <linearGradient id=\"mandelbrotRamp\" x1=\"0\" y1=\"0\" x2=\"1\" y2=\"0\">\n          {stopColors.map((s) => (\n            <stop key={s.offset} offset={s.offset} stopColor={s.color} />\n          ))}\n        </linearGradient>\n      </defs>\n\n      <text\n        x={width / 2}\n        y={42}\n        textAnchor=\"middle\"\n        fontSize={22}\n        fontWeight={600}\n        fill={t.ink}\n      >\n        {TITLE}\n      </text>\n      <text\n        x={width / 2}\n        y={70}\n        textAnchor=\"middle\"\n        fontSize={15}\n        fill={t.inkSoft}\n      >\n        {SUBTITLE}\n      </text>\n\n      <foreignObject x={left} y={top} width={areaW} height={areaH}>\n        <canvas\n          ref={canvasRef}\n          style={{ width: \"100%\", height: \"100%\", display: \"block\" }}\n        />\n      </foreignObject>\n      <rect\n        x={left}\n        y={top}\n        width={areaW}\n        height={areaH}\n        fill=\"none\"\n        stroke={t.grid}\n        strokeWidth={1}\n      />\n\n      <text\n        x={barX + barW / 2}\n        y={titleY}\n        textAnchor=\"middle\"\n        fontSize={14}\n        fontWeight={600}\n        fill={t.ink}\n      >\n        Smooth escape-time scale (1–{MAX_ITERATIONS} iterations)\n      </text>\n      <rect\n        x={barX}\n        y={barY}\n        width={barW}\n        height={barH}\n        fill=\"url(#mandelbrotRamp)\"\n        stroke={t.grid}\n        strokeWidth={1}\n        rx={4}\n      />\n      <text\n        x={barX}\n        y={endLabelY}\n        textAnchor=\"start\"\n        fontSize={14}\n        fill={t.inkSoft}\n      >\n        Fast escape (1)\n      </text>\n      <text\n        x={barX + barW}\n        y={endLabelY}\n        textAnchor=\"end\"\n        fontSize={14}\n        fill={t.inkSoft}\n      >\n        Slow escape ({MAX_ITERATIONS})\n      </text>\n\n      <rect\n        x={swatchX}\n        y={swatchY}\n        width={swatchSize}\n        height={swatchSize}\n        fill={INTERIOR_HEX}\n        stroke={t.ink}\n        strokeWidth={1.5}\n        rx={4}\n      />\n      <text\n        x={swatchX + swatchSize + 10}\n        y={swatchY + swatchSize / 2 + 5}\n        textAnchor=\"start\"\n        fontSize={14}\n        fontWeight={600}\n        fill={t.ink}\n      >\n        Bounded interior\n      </text>\n      <text\n        x={swatchX + swatchSize + 10}\n        y={endLabelY}\n        textAnchor=\"start\"\n        fontSize={14}\n        fill={t.inkSoft}\n      >\n        Never escapes (|z| ≤ 2)\n      </text>\n    </g>\n  );\n}\n\nexport default function Chart() {\n  return (\n    <ChartsContainer\n      width={width}\n      height={height}\n      series={[]}\n      skipAnimation\n      margin={margin}\n      xAxis={[\n        {\n          id: \"real-axis\",\n          scaleType: \"linear\",\n          min: X_MIN,\n          max: X_MAX,\n          height: X_AXIS_H,\n          label: \"Real Part Re(c)\",\n          tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          labelStyle: { fontSize: 15, fill: t.ink },\n        },\n      ]}\n      yAxis={[\n        {\n          id: \"imag-axis\",\n          scaleType: \"linear\",\n          min: Y_MIN,\n          max: Y_MAX,\n          width: Y_AXIS_W,\n          label: \"Imaginary Part Im(c)\",\n          tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          labelStyle: { fontSize: 15, fill: t.ink },\n        },\n      ]}\n    >\n      <MandelbrotLayer />\n      <ChartsXAxis axisId=\"real-axis\" />\n      <ChartsYAxis axisId=\"imag-axis\" />\n    </ChartsContainer>\n  );\n}\n"}