StealThis .dev

Science — Molecule / Structure Viewer Card

An interactive ball-and-stick molecule viewer card built with inline SVG and vanilla JavaScript. Drag to rotate a pseudo-3D structure, scroll or click to zoom, and toggle auto-spin while depth is faked through radius scaling and opacity. A structure selector switches between water, methane, benzene, and caffeine, each with a CPK element legend, formula, molar mass, atom and bond counts, and a fictional Helix Structural Database citation.

Open in Lab
html css vanilla-js
Targets: JS HTML

Code

Molecule / Structure Viewer Card

A self-contained molecular structure viewer rendered entirely with inline SVG and a few hundred lines of vanilla JavaScript — no WebGL, no 3D libraries, and no math heavier than a pair of rotation matrices. Atoms are colored circles with a specular highlight, bonds are single or double lines, and a pseudo-3D feel is produced by projecting each atom’s (x, y, z) coordinate and cueing depth with radius scaling and opacity so closer atoms read larger and more opaque.

The viewport is fully interactive: drag (mouse or touch) to rotate the model, scroll or use the zoom buttons to scale, and the arrow keys rotate when the stage is focused for keyboard users. An auto-spin toggle drives a requestAnimationFrame loop, and a reset button restores the default camera. Painter’s-algorithm sorting redraws atoms and bonds back-to-front on every frame so overlaps stay believable as the molecule turns.

A segmented selector switches between water, methane, benzene, and caffeine. Each structure repopulates the info panel — IUPAC name, formula, molar mass, atom and bond counts — and rebuilds the CPK element legend with per-element counts. Figure captions, a fictional Helix Structural Database entry ID, and a mock journal citation round out the academic framing, and a small toast helper confirms actions like loading a structure or toggling spin.

Illustrative UI only — fictional authors, data, and figures; not real scientific results.