BUILD AN INTERFACE LIKE CHEMDRAW FOR MOBILE
- User
BUILD AN INTERFACE LIKE CHEMDRAW FOR MOBILE
Assistant
I'll build a ChemDraw-like molecular structure editor for mobile. Let me start by exploring the project structure.
Let me check the existing files to understand the setup.
Now I'll build a complete ChemDraw-like molecular editor for mobile. Let me plan the architecture:
- Canvas: SVG-based drawing area with atoms and bonds
- Tools: bond drawing, element selection, ring templates, erase, move
- Touch-optimized: large tap targets, bottom toolbar
- Features: single/double/triple bonds, element palette, ring templates, molecular formula calculation, undo/redo, clear
Let me start with the core types and geometry utilities.
Now the geometry/molecule helper functions:
Now let me create the SVG rendering component for the molecule:
Now the toolbar and element picker components:
Let me remove the unused
linedeclaration:Now the element picker component:
Now the main canvas component with all the touch interaction logic. This is the core of the app:
Now the main App component that ties it all together with undo/redo history, info panel, and header:
Now let me create the main App. I need to handle the interaction between continuous drags (which shouldn't spam history) and discrete commits. Let me use a simpler approach where Canvas commits go through history but drag-moves use a batching mechanism:
Now let me add the
no-scrollbarutility to the CSS and check the index.css:Now update the title in index.html and add mobile viewport settings:
Now let me build the project to check for errors:
The build succeeded. Now let me review the ring-fusing logic — there's a subtle bug. When attaching a ring to an existing atom,
buildRingplaces the ring's center relative to the start point, but the direction handling could put the ring on top of existing bonds. Let me also verify thesuggestAnglefor ring attachment works well and improve the ring attachment to properly fuse when tapping a bond.Let me improve one thing: when a ring tool taps an existing bond, it should fuse the ring to that bond edge (common in ChemDraw). Let me enhance the ring placement:
Let me add the
buildRingOnEdgefunction to geometry.ts:Now let me use
buildRingOnEdgein theplaceRingfunction for bond fusion:Now let me build again to confirm everything compiles:
The build succeeded. Let me add a "chain" tool implementation and a charge-editing feature, plus a small help/gesture improvement. First let me check the chain tool — currently it's treated as a bond tool but should draw a zig-zag chain. Let me verify the current chain handling and add a dedicated chain draw that creates multiple bonds when dragging:
The chain tool currently works as a single bond. Let me enhance it so dragging creates a multi-atom zig-zag chain based on drag distance:
I need to add the chainPreview state and handle committing the chain. Let me add the state and update handleUp and rendering:
Now update handleUp to commit the chain, and render the chain preview:
Now render the chain preview in the SVG:
The build succeeds. Let me add one more useful feature set: charge editing (tap a selected atom in Move mode to add +/− charges) via a small floating action, and a quick-templates system. Actually, let me add charge controls that appear when an atom is selected in Move mode, which is a common ChemDraw operation.
Let me add charge +/− buttons that appear when an atom is selected. First, I'll expose the selected atom from Canvas via a callback:
Now let me replace the internal
selectedstate with the props version:Now update App.tsx to manage selected state and add charge controls:
Now add props to Canvas and add the selected-atom action popover:
Let me add the PopBtn component:
The build succeeds. Let me clear the selection when switching away from select tool to avoid a stuck popover. Let me update the setTool call in the toolbar to clear selection:
Let me do a final build to confirm everything is working:
The build succeeds. I've built MolSketch, a complete ChemDraw-style molecular structure editor optimized for mobile touch devices.
What it does
Drawing tools (bottom scrollable toolbar):
- Single / Double / Triple bonds — tap an atom to sprout a new bond at the optimal chemistry angle (120°), or drag to draw in any direction with 30° angle snapping
- Wedge / Hash — stereochemistry bonds (solid wedge & dashed for 3D depth)
- Chain — drag to lay down a zig-zag carbon chain of any length
- Benzene & ring templates (3/4/5/6-membered) — tap empty space to drop a ring, tap an atom to attach it, or tap an existing bond to fuse a ring onto that edge (like real ChemDraw)
- Move — drag atoms around; tap an atom to get a floating popover to add +/− charges or delete it
- Erase — remove atoms (and their bonds) or individual bonds
Element palette (top scrollable strip): C, H, N, O, S, P, F, Cl, Br, I — color-coded CPK style. Selecting an element sets it for new atoms and lets you retype existing atoms.
Smart chemistry rendering:
- Skeletal (line-angle) drawing — carbons are implicit, heteroatoms get labels
- Bond lines auto-shorten around atom labels
- Live molecular formula (Hill system with subscripts) and molecular weight computed with implicit hydrogens
- Atom & bond counts
Mobile UX:
- Full-height layout with safe-area insets, no pinch-zoom jank, touch-action handling
- Pinch-free zoom controls (+/−/fit-to-screen), pan by dragging empty canvas in Move mode
- Full undo/redo history (drags batched into single entries) and clear-all
- Dotted grid background, hover/selection highlights, and drag previews
Everything is built with React, Vite, and Tailwind, organized into a clean
chem/domain layer (types, elements, geometry) andcomponents/(Canvas, MoleculeView, Toolbar, ElementPicker) with a custom history hook.Files built:
src/chem/types.ts,src/chem/elements.ts,src/chem/geometry.ts,src/components/MoleculeView.tsx,src/components/Toolbar.tsx,src/components/ElementPicker.tsx,src/components/Canvas.tsx,src/hooks/useHistory.ts,src/App.tsx,src/index.css,index.html- User
I NEED TO ADD A FILE OPTION AND CAN BE SAVE THE IN DIFFERENT FORMATS AND NEED AN INTER FACE TO PRODUCE NAME TO STRUCTURE AND STRUCTURE TO NAME , FINALLY GIVE IT IN APK FORMAT SO I CAN USE IT IN ANDROID PHONE
Assistant
I'll add these features. Let me break this down:
- File menu — save/load in different formats (MOL, SMILES, JSON, SVG, PNG)
- Name→Structure and Structure→Name interface
- APK guidance — I'll set up the project so it can be packaged as an Android APK (via Capacitor), and provide clear instructions.
Let me start by exploring the current structure and then build the file/format utilities.
Now let me build the file format serializers. First, the MOL/SMILES/JSON export & import module:
The SMILES ring-closure logic I wrote got convoluted. Let me rewrite the SMILES generator cleanly and fix the
toSVGunused param:Now let me create the Name↔Structure module. This needs a library of named compounds and a SMILES-like parser for name→structure, plus a naming heuristic for structure→name. Let me build a compound database and matching logic:
Now let me create the file/export utilities for downloading files and rendering PNG:
Now let me create the FileMenu component (save/open in different formats):
Now the Name↔Structure converter panel:
Now let me integrate these into App.tsx. Let me view the header section to add menu buttons:
Now let me add the imports, state, and modal buttons. First the imports and state:
Now add the buttons to the header and render the modals. Let me add File & Name buttons to the header:
Now render the modals. Let me find the closing of the main div:
Now let me check the nomenclature.ts unused import and build:
The build succeeds with all features. Now let me set up the Android APK packaging with Capacitor. Let me install Capacitor:
Now let me create the Capacitor config file and a build script helper. Capacitor needs
capacitor.config.ts:Now let me generate an app icon and create comprehensive build instructions. First the icon:
Now let me create a detailed README with APK build instructions:
Now let me add an in-app "About / Install on Android" info button so the user can see the APK instructions from within the app. Let me add a small help modal. First, let me add a footer note or an info action. I'll add an "Install" info modal accessible from the header:
Now let me wire the AndroidGuide into App.tsx with an install button in the header:
Now add the Android button to the header and render the modal:
Everything builds successfully. Let me verify the nomenclature file doesn't have the unused
buildRing/import issue by checking it compiled — it did (no lint errors reported). Let me do a final review of the header layout since I added several buttons which may crowd on mobile. Let me check the current header:The header now has: Files, Name, divider, Undo, Redo, Clear, APK button. That's a lot for a narrow phone. Let me make the header buttons slightly more compact to prevent overflow:
Now let me make the IconBtn slightly smaller to fit:
Everything builds successfully. Here's a summary of everything I added.
✅ 1. File menu with multiple save formats
A new Files button (folder icon) in the header opens a bottom-sheet with two tabs:
Save / Export — name your file and export to:
- MolSketch Project (.molsketch.json) — full-fidelity editable native format
- MDL Molfile (.mol) — the industry-standard chemistry format (V2000, with coordinates, bond orders, wedge/hash stereo, and charge blocks)
- SMILES (.smi) — line notation (with a proper spanning-tree + ring-closure generator)
- SVG — scalable vector image
- PNG — 2× raster image (rendered via canvas)
- Copy SMILES — straight to clipboard
Open — load
.molsketch.jsonor.molfiles back into the editor (auto-detects format).✅ 2. Name ⇄ Structure interface
A new Name button opens a converter with two directions:
- Name → Structure: type a common name (benzene, ethanol, acetic acid, acetone, aniline, water, CO₂, ammonia, etc.), get live suggestion chips, a rendered preview, and a "Load into editor" button. Includes a curated library of ~40 compounds plus alkane/alkene/alkyne/alcohol/acid builders.
- Structure → Name: names whatever you've drawn — exact matches from the library, heuristic IUPAC-style names for simple hydrocarbons (methane…decane, -ene/-yne, cyclo-, benzene), plus the molecular formula and generated SMILES, with a confidence badge (exact / guess / unknown).
✅ 3. Android APK packaging
- Installed Capacitor (
@capacitor/core,cli,android) and addedcapacitor.config.ts(app name MolSketch, IDcom.molsketch.app,distweb dir). - Generated an app icon at
public/icon.png. - Wrote
BUILD_ANDROID_APK.md— complete copy-paste instructions for producing a debug and signed release.apk. - Added an in-app APK button (green Android robot) that opens a step-by-step install guide right inside the app.
Why the APK isn't attached directly: Android APKs must be compiled with the Android SDK/Gradle, which can't run in this browser environment. The project is fully pre-configured, so on any computer with Node.js + JDK 17 + Android Studio you just run:
npm run build && npx cap add android && npx cap sync android cd android && ./gradlew assembleDebug…and your installable
app-debug.apkappears inandroid/app/build/outputs/apk/debug/.Files built:
src/chem/formats.ts,src/chem/nomenclature.ts,src/chem/exporter.ts,src/components/FileMenu.tsx,src/components/NameConverter.tsx,src/App.tsx,capacitor.config.ts,BUILD_ANDROID_APK.md,src/components/AndroidGuide.tsx
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.