{"spec_id":"feynman-basic","library":"d3","language":"javascript","code":"// anyplot.ai\n// feynman-basic: Feynman Diagram for Particle Interactions\n// Library: d3 7.9.2 | JavaScript 22.23.5\n// Quality: 85/100 | Updated: 2026-12-01\n\nconst t = window.ANYPLOT_TOKENS;\nconst { width, height } = window.ANYPLOT_SIZE;\n\n// Imprint palette — data colors identical across themes\nconst COLORS = {\n  fermion: t.palette[0], // #009E73 green    — top-quark loop\n  photon: t.palette[1], //  #C475FD lavender — photons\n  gluon: t.palette[2], //   #4467A3 blue     — gluons\n  boson: t.palette[3], //   #BD8233 ochre    — Higgs\n};\n\n// Columns: names of the fields in the records below\nconst FROM = \"from_vertex\"; // role: source vertex id\nconst TO = \"to_vertex\"; // role: target vertex id\nconst KIND = \"type\"; // role: fermion | photon | gluon | boson\nconst LABEL = \"label\"; // role: particle symbol\nconst LABEL_DX = \"label_dx\"; // role: label offset (px) from the line midpoint\nconst LABEL_DY = \"label_dy\";\n\n// Data: gg → H → γγ — schematic positions as fractions of the canvas\nconst vertices = [\n  { id: \"g1\", x: 0.08, y: 0.17, dot: false },\n  { id: \"g2\", x: 0.08, y: 0.83, dot: false },\n  { id: \"A\", x: 0.3, y: 0.3, dot: true },\n  { id: \"B\", x: 0.3, y: 0.7, dot: true },\n  { id: \"C\", x: 0.42, y: 0.5, dot: true },\n  { id: \"D\", x: 0.58, y: 0.5, dot: true },\n  { id: \"E\", x: 0.7, y: 0.3, dot: true },\n  { id: \"F\", x: 0.7, y: 0.7, dot: true },\n  { id: \"p1\", x: 0.92, y: 0.17, dot: false },\n  { id: \"p2\", x: 0.92, y: 0.83, dot: false },\n];\nconst data = [\n  { [FROM]: \"g1\", [TO]: \"A\", [KIND]: \"gluon\", [LABEL]: \"g\", [LABEL_DX]: 12, [LABEL_DY]: 50 },\n  { [FROM]: \"g2\", [TO]: \"B\", [KIND]: \"gluon\", [LABEL]: \"g\", [LABEL_DX]: 10, [LABEL_DY]: 44 },\n  { [FROM]: \"A\", [TO]: \"C\", [KIND]: \"fermion\", [LABEL]: \"t\", [LABEL_DX]: 6, [LABEL_DY]: -22 },\n  { [FROM]: \"C\", [TO]: \"B\", [KIND]: \"fermion\", [LABEL]: \"t\", [LABEL_DX]: 6, [LABEL_DY]: 40 },\n  { [FROM]: \"B\", [TO]: \"A\", [KIND]: \"fermion\", [LABEL]: \"t\", [LABEL_DX]: -26, [LABEL_DY]: 8 },\n  { [FROM]: \"C\", [TO]: \"D\", [KIND]: \"boson\", [LABEL]: \"H\", [LABEL_DX]: 0, [LABEL_DY]: -26 },\n  { [FROM]: \"D\", [TO]: \"E\", [KIND]: \"fermion\", [LABEL]: \"t\", [LABEL_DX]: -10, [LABEL_DY]: -22 },\n  { [FROM]: \"E\", [TO]: \"F\", [KIND]: \"fermion\", [LABEL]: \"t\", [LABEL_DX]: 28, [LABEL_DY]: 8 },\n  { [FROM]: \"F\", [TO]: \"D\", [KIND]: \"fermion\", [LABEL]: \"t\", [LABEL_DX]: -10, [LABEL_DY]: 40 },\n  { [FROM]: \"E\", [TO]: \"p1\", [KIND]: \"photon\", [LABEL]: \"γ\", [LABEL_DX]: 8, [LABEL_DY]: 50 },\n  { [FROM]: \"F\", [TO]: \"p2\", [KIND]: \"photon\", [LABEL]: \"γ\", [LABEL_DX]: -4, [LABEL_DY]: -30 },\n];\n\n// Plot\nconst pos = new Map(vertices.map((v) => [v.id, [v.x * width, v.y * height]]));\nconst SW = 3.5;\nconst LEGEND = [\n  { type: \"fermion\", text: \"Fermion (t)\" },\n  { type: \"photon\", text: \"Photon (γ)\" },\n  { type: \"gluon\", text: \"Gluon (g)\" },\n  { type: \"boson\", text: \"Scalar boson (H)\" },\n];\n\n// Wavy line for photons — sine wave perpendicular to the path\nfunction wavyPath(x1, y1, x2, y2, waves, amp) {\n  const dx = x2 - x1, dy = y2 - y1;\n  const len = Math.hypot(dx, dy);\n  const cx = dx / len, cy = dy / len;\n  const n = waves * 24;\n  return d3.line()(Array.from({ length: n + 1 }, (_, i) => {\n    const u = i / n;\n    const p = amp * Math.sin(u * waves * 2 * Math.PI);\n    return [x1 + u * len * cx - p * cy, y1 + u * len * cy + p * cx];\n  }));\n}\n\n// Looped curly line for gluons — cycloidal arches perpendicular to the path\nfunction gluonPath(x1, y1, x2, y2, nLoops, radius) {\n  const dx = x2 - x1, dy = y2 - y1;\n  const len = Math.hypot(dx, dy);\n  const cx = dx / len, cy = dy / len;\n  const n = nLoops * 72;\n  return d3.line()(Array.from({ length: n + 1 }, (_, i) => {\n    const theta = (i / n) * nLoops * 2 * Math.PI;\n    const along = (i / n) * len + radius * Math.sin(theta);\n    const perp = radius * (1 - Math.cos(theta));\n    return [x1 + along * cx + perp * cy, y1 + along * cy - perp * cx];\n  }));\n}\n\nconst svg = d3.select(\"#container\").append(\"svg\")\n  .attr(\"width\", width).attr(\"height\", height);\nconst defs = svg.append(\"defs\");\n\n// Arrow markers: fermion direction and time axis\ndefs.append(\"marker\").attr(\"id\", \"arrow-f\")\n  .attr(\"markerWidth\", 16).attr(\"markerHeight\", 12)\n  .attr(\"refX\", 8).attr(\"refY\", 6)\n  .attr(\"orient\", \"auto\").attr(\"markerUnits\", \"userSpaceOnUse\")\n  .append(\"polygon\").attr(\"points\", \"0 0, 16 6, 0 12\").attr(\"fill\", COLORS.fermion);\ndefs.append(\"marker\").attr(\"id\", \"arrow-t\")\n  .attr(\"markerWidth\", 10).attr(\"markerHeight\", 7)\n  .attr(\"refX\", 10).attr(\"refY\", 3.5)\n  .attr(\"orient\", \"auto\").attr(\"markerUnits\", \"userSpaceOnUse\")\n  .append(\"polygon\").attr(\"points\", \"0 0, 10 3.5, 0 7\").attr(\"fill\", t.inkSoft);\n\n// Draws one line in the style of its particle type; the fermion arrow sits at the midpoint\nfunction drawLine(sel, type, [x1, y1], [x2, y2], sw) {\n  const mx = (x1 + x2) / 2, my = (y1 + y2) / 2;\n  if (type === \"fermion\") {\n    sel.append(\"line\").attr(\"x1\", x1).attr(\"y1\", y1).attr(\"x2\", mx).attr(\"y2\", my)\n      .attr(\"stroke\", COLORS.fermion).attr(\"stroke-width\", sw).attr(\"marker-end\", \"url(#arrow-f)\");\n    sel.append(\"line\").attr(\"x1\", mx).attr(\"y1\", my).attr(\"x2\", x2).attr(\"y2\", y2)\n      .attr(\"stroke\", COLORS.fermion).attr(\"stroke-width\", sw);\n  } else if (type === \"photon\") {\n    sel.append(\"path\").attr(\"d\", wavyPath(x1, y1, x2, y2, sw === SW ? 7 : 3, sw === SW ? 13 : 7))\n      .attr(\"stroke\", COLORS.photon).attr(\"stroke-width\", sw).attr(\"fill\", \"none\");\n  } else if (type === \"gluon\") {\n    sel.append(\"path\").attr(\"d\", gluonPath(x1, y1, x2, y2, sw === SW ? 6 : 3, sw === SW ? 15 : 9))\n      .attr(\"stroke\", COLORS.gluon).attr(\"stroke-width\", sw).attr(\"fill\", \"none\");\n  } else {\n    sel.append(\"line\").attr(\"x1\", x1).attr(\"y1\", y1).attr(\"x2\", x2).attr(\"y2\", y2)\n      .attr(\"stroke\", COLORS.boson).attr(\"stroke-width\", sw).attr(\"stroke-dasharray\", \"16,9\");\n  }\n}\n\n// Propagators, then particle labels\ndata.forEach((d) => drawLine(svg, d[KIND], pos.get(d[FROM]), pos.get(d[TO]), SW));\ndata.forEach((d) => {\n  const [x1, y1] = pos.get(d[FROM]);\n  const [x2, y2] = pos.get(d[TO]);\n  svg.append(\"text\")\n    .attr(\"x\", (x1 + x2) / 2 + d[LABEL_DX]).attr(\"y\", (y1 + y2) / 2 + d[LABEL_DY])\n    .attr(\"text-anchor\", \"middle\").attr(\"fill\", COLORS[d[KIND]])\n    .style(\"font-size\", \"30px\").style(\"font-weight\", \"700\").style(\"font-style\", \"italic\")\n    .text(d[LABEL]);\n});\n\n// Interaction vertices\nsvg.selectAll(\"circle\").data(vertices.filter((v) => v.dot)).join(\"circle\")\n  .attr(\"cx\", (v) => pos.get(v.id)[0]).attr(\"cy\", (v) => pos.get(v.id)[1])\n  .attr(\"r\", 8).attr(\"fill\", t.ink);\n\n// Time axis, left to right\nconst tY = height * 0.9;\nsvg.append(\"line\")\n  .attr(\"x1\", width * 0.08).attr(\"y1\", tY).attr(\"x2\", width * 0.3).attr(\"y2\", tY)\n  .attr(\"stroke\", t.inkSoft).attr(\"stroke-width\", 2).attr(\"marker-end\", \"url(#arrow-t)\");\nsvg.append(\"text\").attr(\"x\", width * 0.3 + 14).attr(\"y\", tY + 6)\n  .attr(\"fill\", t.inkSoft).style(\"font-size\", \"22px\").text(\"time\");\n\n// Legend — one sample per line type\nconst lY = height * 0.9;\nconst lx0 = width * 0.42;\nLEGEND.forEach(({ type, text }, i) => {\n  const x = lx0 + i * width * 0.13;\n  drawLine(svg, type, [x, lY], [x + 56, lY], 3);\n  svg.append(\"text\").attr(\"x\", x + 56 + 10).attr(\"y\", lY + 6)\n    .attr(\"fill\", t.inkSoft).style(\"font-size\", \"20px\").text(text);\n});\n\n// Title\nsvg.append(\"text\").attr(\"x\", width / 2).attr(\"y\", 48)\n  .attr(\"text-anchor\", \"middle\").attr(\"fill\", t.ink)\n  .style(\"font-size\", \"26px\").style(\"font-weight\", \"600\")\n  .text(\"feynman-basic · javascript · d3 · anyplot.ai\");\n"}