🐇 Burrow 13 · Nov 13 · 2 min read

Teaching a Website Chemistry: How the Molecule Builder Knows What You Made

Climb out

Little Alchemy, but real

I wanted something that feels like Little Alchemy or Infinite Craft but with actual chemistry, like you add an H and an O and it tells you what that is, then add another H and it updates to water right away.

The formula and weight update live as you build, and the names come from a small list of common ones with a PubChem button for everything else.

The annoying part

You can draw ethanol starting from either end and put the atoms anywhere and connect them in any order, so for it to know what you made every drawing has to end up with the same name. It turns your drawing into canonical SMILES, which is a line like CCO where the atoms always come out in the same order.

assets/chem.jsRanking atoms until they stop changing
const refine = () => { for (let it = 0; it < n + 2; it++) {
  const keys = r.map((x, k) => String(x).padStart(5, '0') + '|' +
    nb[k].map(([j, o]) => String(r[j]).padStart(5, '0') + ':' + o).sort().join(';'));
  const r2 = rankOf(keys);
  if (new Set(r2).size === new Set(r).size) { r = r2; return; }
  r = r2;
} };

Every atom starts with a rank, then gets a new one from its own rank plus the ranks of whatever it's bonded to, over and over till the ranks stop splitting apart. After that two atoms with the same rank really are the same spot in the molecule, so the order comes out the same no matter how you drew it.

Isomers, and left and right handed molecules

It does isomers and stereoisomers too. It works out R/S on carbon centers and E/Z on double bonds from your wedges and dashes using the same priority rules chemists use, and compare mode tells you if two molecules are the same, mirror images, or just have the same formula.

The old version

There was an older molecule tool on here that I wasn't a fan of, clicking just kept spawning carbons and atoms acted connected when they weren't. So I redid it as drag and drop with a clear board button, and the old element mixer went too cause it was kinda useless.

All 118 elements

There's a mini periodic table with all 118 now. In Progression mode you start with H, C and O and unlock the rest by making certain molecules, you just get a hint for what to make. I made sure none of them need the element you're unlocking cause that'd be impossible lol.

The Molecule Builder with the element bar, the drawing board and the discoveries listThe board

Elements up top, drag them onto the board, and your discoveries show up on the right.

The burrow ends hereClimb back up to the surface