Feynman Diagram for Particle Interactions in D3.js (JavaScript)

The same plot in 14 other libraries — JavaScript: Chart.js, Apache ECharts, Highcharts, MUI X Charts; Python: Altair, Bokeh, lets-plot, Matplotlib, Plotly, plotnine, Pygal, Seaborn; R: ggplot2; Julia: Makie.jl. Compare all 15 side by side: Feynman Diagram for Particle Interactions in Python, R, Julia and JavaScript.

A Feynman diagram visualizes interactions between subatomic particles in quantum field theory. Different line styles represent different particle types: straight lines for fermions (electrons, quarks), wavy lines for photons, curly/looped lines for gluons, and dashed lines for scalar bosons (e.g., Higgs). Lines meet at vertices representing interaction points. Invented by Richard Feynman, these diagrams are both a computational tool and a cultural icon of modern physics.

Feynman Diagram for Particle Interactions rendered with D3.js

Renders

JavaScript source (D3.js)

// anyplot.ai
// feynman-basic: Feynman Diagram for Particle Interactions
// Library: d3 7.9.2 | JavaScript 22.23.5
// Quality: 85/100 | Updated: 2026-12-01

const t = window.ANYPLOT_TOKENS;
const { width, height } = window.ANYPLOT_SIZE;

// Imprint palette — data colors identical across themes
const COLORS = {
  fermion: t.palette[0], // #009E73 green    — top-quark loop
  photon: t.palette[1], //  #C475FD lavender — photons
  gluon: t.palette[2], //   #4467A3 blue     — gluons
  boson: t.palette[3], //   #BD8233 ochre    — Higgs
};

// Columns: names of the fields in the records below
const FROM = "from_vertex"; // role: source vertex id
const TO = "to_vertex"; // role: target vertex id
const KIND = "type"; // role: fermion | photon | gluon | boson
const LABEL = "label"; // role: particle symbol
const LABEL_DX = "label_dx"; // role: label offset (px) from the line midpoint
const LABEL_DY = "label_dy";

// Data: gg → H → γγ — schematic positions as fractions of the canvas
const vertices = [
  { id: "g1", x: 0.08, y: 0.17, dot: false },
  { id: "g2", x: 0.08, y: 0.83, dot: false },
  { id: "A", x: 0.3, y: 0.3, dot: true },
  { id: "B", x: 0.3, y: 0.7, dot: true },
  { id: "C", x: 0.42, y: 0.5, dot: true },
  { id: "D", x: 0.58, y: 0.5, dot: true },
  { id: "E", x: 0.7, y: 0.3, dot: true },
  { id: "F", x: 0.7, y: 0.7, dot: true },
  { id: "p1", x: 0.92, y: 0.17, dot: false },
  { id: "p2", x: 0.92, y: 0.83, dot: false },
];
const data = [
  { [FROM]: "g1", [TO]: "A", [KIND]: "gluon", [LABEL]: "g", [LABEL_DX]: 12, [LABEL_DY]: 50 },
  { [FROM]: "g2", [TO]: "B", [KIND]: "gluon", [LABEL]: "g", [LABEL_DX]: 10, [LABEL_DY]: 44 },
  { [FROM]: "A", [TO]: "C", [KIND]: "fermion", [LABEL]: "t", [LABEL_DX]: 6, [LABEL_DY]: -22 },
  { [FROM]: "C", [TO]: "B", [KIND]: "fermion", [LABEL]: "t", [LABEL_DX]: 6, [LABEL_DY]: 40 },
  { [FROM]: "B", [TO]: "A", [KIND]: "fermion", [LABEL]: "t", [LABEL_DX]: -26, [LABEL_DY]: 8 },
  { [FROM]: "C", [TO]: "D", [KIND]: "boson", [LABEL]: "H", [LABEL_DX]: 0, [LABEL_DY]: -26 },
  { [FROM]: "D", [TO]: "E", [KIND]: "fermion", [LABEL]: "t", [LABEL_DX]: -10, [LABEL_DY]: -22 },
  { [FROM]: "E", [TO]: "F", [KIND]: "fermion", [LABEL]: "t", [LABEL_DX]: 28, [LABEL_DY]: 8 },
  { [FROM]: "F", [TO]: "D", [KIND]: "fermion", [LABEL]: "t", [LABEL_DX]: -10, [LABEL_DY]: 40 },
  { [FROM]: "E", [TO]: "p1", [KIND]: "photon", [LABEL]: "γ", [LABEL_DX]: 8, [LABEL_DY]: 50 },
  { [FROM]: "F", [TO]: "p2", [KIND]: "photon", [LABEL]: "γ", [LABEL_DX]: -4, [LABEL_DY]: -30 },
];

// Plot
const pos = new Map(vertices.map((v) => [v.id, [v.x * width, v.y * height]]));
const SW = 3.5;
const LEGEND = [
  { type: "fermion", text: "Fermion (t)" },
  { type: "photon", text: "Photon (γ)" },
  { type: "gluon", text: "Gluon (g)" },
  { type: "boson", text: "Scalar boson (H)" },
];

// Wavy line for photons — sine wave perpendicular to the path
function wavyPath(x1, y1, x2, y2, waves, amp) {
  const dx = x2 - x1, dy = y2 - y1;
  const len = Math.hypot(dx, dy);
  const cx = dx / len, cy = dy / len;
  const n = waves * 24;
  return d3.line()(Array.from({ length: n + 1 }, (_, i) => {
    const u = i / n;
    const p = amp * Math.sin(u * waves * 2 * Math.PI);
    return [x1 + u * len * cx - p * cy, y1 + u * len * cy + p * cx];
  }));
}

// Looped curly line for gluons — cycloidal arches perpendicular to the path
function gluonPath(x1, y1, x2, y2, nLoops, radius) {
  const dx = x2 - x1, dy = y2 - y1;
  const len = Math.hypot(dx, dy);
  const cx = dx / len, cy = dy / len;
  const n = nLoops * 72;
  return d3.line()(Array.from({ length: n + 1 }, (_, i) => {
    const theta = (i / n) * nLoops * 2 * Math.PI;
    const along = (i / n) * len + radius * Math.sin(theta);
    const perp = radius * (1 - Math.cos(theta));
    return [x1 + along * cx + perp * cy, y1 + along * cy - perp * cx];
  }));
}

const svg = d3.select("#container").append("svg")
  .attr("width", width).attr("height", height);
const defs = svg.append("defs");

// Arrow markers: fermion direction and time axis
defs.append("marker").attr("id", "arrow-f")
  .attr("markerWidth", 16).attr("markerHeight", 12)
  .attr("refX", 8).attr("refY", 6)
  .attr("orient", "auto").attr("markerUnits", "userSpaceOnUse")
  .append("polygon").attr("points", "0 0, 16 6, 0 12").attr("fill", COLORS.fermion);
defs.append("marker").attr("id", "arrow-t")
  .attr("markerWidth", 10).attr("markerHeight", 7)
  .attr("refX", 10).attr("refY", 3.5)
  .attr("orient", "auto").attr("markerUnits", "userSpaceOnUse")
  .append("polygon").attr("points", "0 0, 10 3.5, 0 7").attr("fill", t.inkSoft);

// Draws one line in the style of its particle type; the fermion arrow sits at the midpoint
function drawLine(sel, type, [x1, y1], [x2, y2], sw) {
  const mx = (x1 + x2) / 2, my = (y1 + y2) / 2;
  if (type === "fermion") {
    sel.append("line").attr("x1", x1).attr("y1", y1).attr("x2", mx).attr("y2", my)
      .attr("stroke", COLORS.fermion).attr("stroke-width", sw).attr("marker-end", "url(#arrow-f)");
    sel.append("line").attr("x1", mx).attr("y1", my).attr("x2", x2).attr("y2", y2)
      .attr("stroke", COLORS.fermion).attr("stroke-width", sw);
  } else if (type === "photon") {
    sel.append("path").attr("d", wavyPath(x1, y1, x2, y2, sw === SW ? 7 : 3, sw === SW ? 13 : 7))
      .attr("stroke", COLORS.photon).attr("stroke-width", sw).attr("fill", "none");
  } else if (type === "gluon") {
    sel.append("path").attr("d", gluonPath(x1, y1, x2, y2, sw === SW ? 6 : 3, sw === SW ? 15 : 9))
      .attr("stroke", COLORS.gluon).attr("stroke-width", sw).attr("fill", "none");
  } else {
    sel.append("line").attr("x1", x1).attr("y1", y1).attr("x2", x2).attr("y2", y2)
      .attr("stroke", COLORS.boson).attr("stroke-width", sw).attr("stroke-dasharray", "16,9");
  }
}

// Propagators, then particle labels
data.forEach((d) => drawLine(svg, d[KIND], pos.get(d[FROM]), pos.get(d[TO]), SW));
data.forEach((d) => {
  const [x1, y1] = pos.get(d[FROM]);
  const [x2, y2] = pos.get(d[TO]);
  svg.append("text")
    .attr("x", (x1 + x2) / 2 + d[LABEL_DX]).attr("y", (y1 + y2) / 2 + d[LABEL_DY])
    .attr("text-anchor", "middle").attr("fill", COLORS[d[KIND]])
    .style("font-size", "30px").style("font-weight", "700").style("font-style", "italic")
    .text(d[LABEL]);
});

// Interaction vertices
svg.selectAll("circle").data(vertices.filter((v) => v.dot)).join("circle")
  .attr("cx", (v) => pos.get(v.id)[0]).attr("cy", (v) => pos.get(v.id)[1])
  .attr("r", 8).attr("fill", t.ink);

// Time axis, left to right
const tY = height * 0.9;
svg.append("line")
  .attr("x1", width * 0.08).attr("y1", tY).attr("x2", width * 0.3).attr("y2", tY)
  .attr("stroke", t.inkSoft).attr("stroke-width", 2).attr("marker-end", "url(#arrow-t)");
svg.append("text").attr("x", width * 0.3 + 14).attr("y", tY + 6)
  .attr("fill", t.inkSoft).style("font-size", "22px").text("time");

// Legend — one sample per line type
const lY = height * 0.9;
const lx0 = width * 0.42;
LEGEND.forEach(({ type, text }, i) => {
  const x = lx0 + i * width * 0.13;
  drawLine(svg, type, [x, lY], [x + 56, lY], 3);
  svg.append("text").attr("x", x + 56 + 10).attr("y", lY + 6)
    .attr("fill", t.inkSoft).style("font-size", "20px").text(text);
});

// Title
svg.append("text").attr("x", width / 2).attr("y", 48)
  .attr("text-anchor", "middle").attr("fill", t.ink)
  .style("font-size", "26px").style("font-weight", "600")
  .text("feynman-basic · javascript · d3 · anyplot.ai");

Retrieve this implementation

Runnable source as JSON, for any HTTP client: https://api.anyplot.ai/specs/feynman-basic/d3/code. Any spec id and library id listed in llms-full.txt fit the same URL shape; every URL below is complete and callable.

{
  "spec_id": "feynman-basic",
  "language": "javascript",
  "library": "d3",
  "page": "https://anyplot.ai/feynman-basic/javascript/d3",
  "hub": "https://anyplot.ai/feynman-basic",
  "code_json": "https://api.anyplot.ai/specs/feynman-basic/d3/code",
  "spec_json": "https://api.anyplot.ai/specs/feynman-basic",
  "render_light_png": "https://storage.googleapis.com/anyplot-images/plots/feynman-basic/javascript/d3/plot-light.png",
  "render_dark_png": "https://storage.googleapis.com/anyplot-images/plots/feynman-basic/javascript/d3/plot-dark.png",
  "interactive_light_html": "https://storage.googleapis.com/anyplot-images/plots/feynman-basic/javascript/d3/plot-light.html",
  "interactive_dark_html": "https://storage.googleapis.com/anyplot-images/plots/feynman-basic/javascript/d3/plot-dark.html",
  "quality_score": 85.0,
  "license": "MIT",
  "guide": "https://anyplot.ai/llms.txt"
}

Part of Feynman Diagram for Particle Interactions on anyplot.ai.

Other implementations