{"spec_id":"feynman-basic","library":"echarts","language":"javascript","code":"// anyplot.ai\n// feynman-basic: Feynman Diagram for Particle Interactions\n// Library: echarts 6.1.0 | JavaScript 22.23.5\n// Quality: 85/100 | Updated: 2026-12-01\n\n//# anyplot-orientation: landscape\nconst t = window.ANYPLOT_TOKENS;\n\n// Imprint palette: one colour per particle type\nconst C_FERMION = t.palette[0]; // #009E73 brand green\nconst C_GLUON = t.palette[1]; // #C475FD lavender\nconst C_PHOTON = t.palette[2]; // #4467A3 blue\nconst C_BOSON = t.palette[3]; // #BD8233 ochre\n\nconst LW = 5;\nconst W = 1600, H = 900; // CSS mount size; the graph series uses these as its coordinate range\n\n// Columns\nconst FROM = \"from\"; // role: start point of a propagator\nconst TO = \"to\"; // role: end point of a propagator\nconst KIND = \"type\"; // role: particle type (fermion | photon | gluon | boson)\nconst LABEL = \"label\"; // role: particle symbol\nconst LABEL_OFFSET = \"labelOffset\"; // role: label position relative to the line midpoint\n\n// Data: gg -> H -> gamma gamma, two top-quark triangles joined by a Higgs line.\n// Positions are schematic; only which lines meet at which vertex carries meaning.\nconst points = {\n  g1: [140, 200], g2: [140, 740], // incoming gluons\n  A1: [430, 330], A2: [430, 610], A3: [650, 470], // first top-quark triangle\n  B1: [950, 470], B2: [1170, 330], B3: [1170, 610], // second top-quark triangle\n  p1: [1460, 200], p2: [1460, 740], // outgoing photons\n};\nconst vertices = [\"A1\", \"A2\", \"A3\", \"B1\", \"B2\", \"B3\"];\nconst data = [\n  { [FROM]: \"g1\", [TO]: \"A1\", [KIND]: \"gluon\", [LABEL]: \"g\", [LABEL_OFFSET]: [14, -44] },\n  { [FROM]: \"g2\", [TO]: \"A2\", [KIND]: \"gluon\", [LABEL]: \"g\", [LABEL_OFFSET]: [-20, -40] },\n  { [FROM]: \"A1\", [TO]: \"A3\", [KIND]: \"fermion\", [LABEL]: \"t\", [LABEL_OFFSET]: [22, -30] },\n  { [FROM]: \"A3\", [TO]: \"A2\", [KIND]: \"fermion\", [LABEL]: \"t\", [LABEL_OFFSET]: [22, 30] },\n  { [FROM]: \"A2\", [TO]: \"A1\", [KIND]: \"fermion\", [LABEL]: \"t\", [LABEL_OFFSET]: [-34, 0] },\n  { [FROM]: \"A3\", [TO]: \"B1\", [KIND]: \"boson\", [LABEL]: \"H\", [LABEL_OFFSET]: [0, -34] },\n  { [FROM]: \"B1\", [TO]: \"B2\", [KIND]: \"fermion\", [LABEL]: \"t\", [LABEL_OFFSET]: [-22, -30] },\n  { [FROM]: \"B2\", [TO]: \"B3\", [KIND]: \"fermion\", [LABEL]: \"t\", [LABEL_OFFSET]: [34, 0] },\n  { [FROM]: \"B3\", [TO]: \"B1\", [KIND]: \"fermion\", [LABEL]: \"t\", [LABEL_OFFSET]: [-22, 30] },\n  { [FROM]: \"B2\", [TO]: \"p1\", [KIND]: \"photon\", [LABEL]: \"γ\", [LABEL_OFFSET]: [14, -44] },\n  { [FROM]: \"B3\", [TO]: \"p2\", [KIND]: \"photon\", [LABEL]: \"γ\", [LABEL_OFFSET]: [14, 44] },\n];\n\n// ── line-style generators (wavy and curly lines have no series type) ─────────\nfunction wavyBeziers(x1, y1, x2, y2, nWaves, amp, color, lw) {\n  const dx = x2 - x1, dy = y2 - y1, len = Math.hypot(dx, dy);\n  const nx = -dy / len, ny = dx / len, n = nWaves * 2, out = [];\n  for (let i = 0; i < n; i++) {\n    const t0 = i / n, t1 = (i + 1) / n, tm = (t0 + t1) / 2, sign = i % 2 === 0 ? 1 : -1;\n    out.push({ type: \"bezierCurve\", shape: {\n      x1: x1 + dx * t0, y1: y1 + dy * t0,\n      cpx1: x1 + dx * tm + nx * amp * sign, cpy1: y1 + dy * tm + ny * amp * sign,\n      x2: x1 + dx * t1, y2: y1 + dy * t1,\n    }, style: { stroke: color, lineWidth: lw, fill: \"rgba(0,0,0,0)\" } });\n  }\n  return out;\n}\n\n// Curly gluon: a trochoid, so each turn is a closed coil that loops back over itself\nfunction curlyLine(x1, y1, x2, y2, nLoops, amp, color, lw) {\n  const dx = x2 - x1, dy = y2 - y1, len = Math.hypot(dx, dy);\n  const ux = dx / len, uy = dy / len, nx = -uy, ny = ux;\n  const pitch = len / (2 * Math.PI * nLoops), steps = nLoops * 48, pts = [];\n  for (let i = 0; i <= steps; i++) {\n    const phi = (2 * Math.PI * nLoops * i) / steps;\n    const along = pitch * phi - amp * Math.sin(phi), across = amp * (1 - Math.cos(phi));\n    pts.push([x1 + ux * along + nx * across, y1 + uy * along + ny * across]);\n  }\n  return { type: \"polyline\", shape: { points: pts },\n    style: { stroke: color, lineWidth: lw, fill: \"rgba(0,0,0,0)\", lineJoin: \"round\" } };\n}\n\nfunction arrow(x1, y1, x2, y2, sz) {\n  const dx = x2 - x1, dy = y2 - y1, len = Math.hypot(dx, dy);\n  const ux = dx / len, uy = dy / len, px = -uy, py = ux;\n  const bx = x2 - ux * sz, by = y2 - uy * sz, w = sz * 0.42;\n  return [[x2, y2], [bx + px * w, by + py * w], [bx - px * w, by - py * w]];\n}\n\nfunction solidLine(x1, y1, x2, y2, color, lw, dash) {\n  return { type: \"polyline\", shape: { points: [[x1, y1], [x2, y2]] },\n    style: { stroke: color, lineWidth: lw, fill: \"rgba(0,0,0,0)\", lineDash: dash } };\n}\n\n// ── Plot ─────────────────────────────────────────────────────────────────────\nconst elems = []; // graphic layer: wavy/curly lines, fermion arrows, labels, key\nconst links = []; // graph series: straight fermion lines and the dashed Higgs line\n\nfor (const d of data) {\n  const [x1, y1] = points[d[FROM]], [x2, y2] = points[d[TO]];\n  if (d[KIND] === \"gluon\") elems.push(curlyLine(x1, y1, x2, y2, 5, 18, C_GLUON, LW));\n  else if (d[KIND] === \"photon\") elems.push(...wavyBeziers(x1, y1, x2, y2, 6, 22, C_PHOTON, LW));\n  else if (d[KIND] === \"boson\") {\n    links.push({ source: d[FROM], target: d[TO], lineStyle: { color: C_BOSON, width: LW, opacity: 1, type: [16, 10] } });\n  } else {\n    // fermion: series line, arrow at the midpoint along the direction of flow\n    links.push({ source: d[FROM], target: d[TO], lineStyle: { color: C_FERMION, width: LW, opacity: 1 } });\n    const mx = (x1 + x2) / 2, my = (y1 + y2) / 2, ux = (x2 - x1) * 0.03, uy = (y2 - y1) * 0.03;\n    elems.push({ type: \"polygon\", shape: { points: arrow(mx - ux, my - uy, mx + ux, my + uy, 26) },\n      style: { fill: C_FERMION, stroke: \"none\" } });\n  }\n  const color = { gluon: C_GLUON, photon: C_PHOTON, boson: C_BOSON, fermion: C_FERMION }[d[KIND]];\n  elems.push({ type: \"text\", style: {\n    text: d[LABEL], x: (x1 + x2) / 2 + d[LABEL_OFFSET][0], y: (y1 + y2) / 2 + d[LABEL_OFFSET][1],\n    textAlign: \"center\", textVerticalAlign: \"middle\", font: \"italic bold 30px serif\", fill: color } });\n}\n\n// Nodes: interaction vertices are dots with a faint halo, external legs end invisibly\nconst nodes = Object.entries(points).map(([name, [x, y]]) => vertices.includes(name)\n  ? { name, value: [x, y], symbol: \"circle\", symbolSize: 22,\n      itemStyle: { color: t.ink, borderColor: \"rgba(0,158,115,0.15)\", borderWidth: 9 } }\n  : { name, value: [x, y], symbol: \"none\" });\n\n// Legend: one sample per line style\nconst LY = 850;\nelems.push(solidLine(300, LY, 370, LY, C_FERMION, LW));\nelems.push({ type: \"polygon\", shape: { points: arrow(327, LY, 343, LY, 20) }, style: { fill: C_FERMION, stroke: \"none\" } });\nelems.push({ type: \"text\", style: { text: \"fermion\", x: 382, y: LY, textVerticalAlign: \"middle\", font: \"22px sans-serif\", fill: t.inkSoft } });\nelems.push(...wavyBeziers(560, LY, 630, LY, 3, 14, C_PHOTON, LW));\nelems.push({ type: \"text\", style: { text: \"photon\", x: 642, y: LY, textVerticalAlign: \"middle\", font: \"22px sans-serif\", fill: t.inkSoft } });\nelems.push(curlyLine(780, LY - 9, 880, LY - 9, 4, 11, C_GLUON, LW));\nelems.push({ type: \"text\", style: { text: \"gluon\", x: 900, y: LY, textVerticalAlign: \"middle\", font: \"22px sans-serif\", fill: t.inkSoft } });\nelems.push(solidLine(1030, LY, 1090, LY, C_BOSON, LW, [16, 10]));\nelems.push({ type: \"text\", style: { text: \"scalar boson\", x: 1102, y: LY, textVerticalAlign: \"middle\", font: \"22px sans-serif\", fill: t.inkSoft } });\n\n// Time axis\nelems.push(solidLine(1340, LY, 1480, LY, t.inkSoft, 2));\nelems.push({ type: \"polygon\", shape: { points: arrow(1458, LY, 1480, LY, 14) }, style: { fill: t.inkSoft, stroke: \"none\" } });\nelems.push({ type: \"text\", style: { text: \"time\", x: 1326, y: LY, textAlign: \"right\", textVerticalAlign: \"middle\", font: \"22px sans-serif\", fill: t.inkSoft } });\n\nconst chart = echarts.init(document.getElementById(\"container\"));\nchart.setOption({\n  animation: false,\n  backgroundColor: t.pageBg,\n  title: {\n    text: \"feynman-basic · javascript · echarts · anyplot.ai\",\n    left: \"center\", top: 22,\n    textStyle: { color: t.ink, fontSize: 28, fontWeight: \"normal\" },\n  },\n  // Hidden axes span the mount in CSS pixels, so graph coordinates equal graphic coordinates\n  grid: { left: 0, top: 0, right: 0, bottom: 0 },\n  xAxis: { type: \"value\", min: 0, max: W, show: false },\n  yAxis: { type: \"value\", min: 0, max: H, inverse: true, show: false },\n  series: [{\n    type: \"graph\", coordinateSystem: \"cartesian2d\", layout: \"none\", z: 6,\n    silent: true, data: nodes, links,\n  }],\n  graphic: elems.map((e) => ({ ...e, z: 5 })),\n});\n"}