A viewer can be automatically instantiated by simply assigning the class viewer_3Dmoljs to a div.
The viewer will be styled according to the containing div, so be sure to set a height and width.
The code below is all that is needed to create the displayed viewer.
<script src="https://3Dmol.org/build/3Dmol-min.js"></script>
<script src="https://3Dmol.org/build/3Dmol.ui-min.js"></script>
<div style="height: 400px; width: 400px; position: relative;" class='viewer_3Dmoljs' data-pdb='2POR' data-backgroundcolor='0xffffff' data-style='stick' data-ui='true'></div> The core script creates viewers when the document finishes loading. Load the separate UI script after the core script when using the controls shown above.
Use lowercase HTML data- attributes to configure the viewer. Selection and style values accept JSON or the shorthand described in the URL tutorial. Supported attributes include:
data-pdbThe value describes a PDB ID to be loaded into the viewer.data-cidThe value describes a PubChem compound id to be loaded into the viewer.data-hrefThe value is a URL to a molecular data file (data-urlis an alias). The data server must permit cross-origin requests if it is on another origin.data-elementThe value is the id of an HTML element on the current page that has molecular data embedded in it.data-typeThe file format passed to GLViewer#addModel. Without it, PDB and PubChem sources use their known formats, URLs use their extensions, and embedded data uses content detection. Specify the type explicitly for ambiguous content or URLs without a usable extension. See FileFormats.data-backgroundcolorThe background color (default black).data-backgroundalphaThe background alpha (default opaque: 1.0).data-optionsParserOptionsSpec options passed to each loaded model.data-configViewerSpec options passed to the viewer. ExplicitbackgroundColorandbackgroundAlphavalues here take precedence over the separate background attributes.data-selectAn AtomSelectionSpec; omitted selects all atoms.data-styleAn AtomStyleSpec. An explicit style replaces the selected atoms' representations. If omitted, line style is added while retaining styles supplied by the molecular file.data-surfaceA SurfaceStyleSpec for a Van der Waals surface of the selected atoms.data-labelresA LabelSpec for one label per selected residue.data-spinIf set will spin the model using GLViewer#spin. Can specify axis and speed (e.g.data-spin='axis:z;speed:10')data-callbackA JavaScript function called after the models are loaded and initial styles and camera fitting are applied. Receives the GLViewer. Asynchronous surface computation may still be running.data-uiSet totrueto enable both the edit dialog (hamburger button) and trajectory controls. Requires the separate3Dmol.uibundle. Set tofalseto disable both by default.data-ui-editSet totrueorfalseto override whether the edit dialog is available.data-ui-trajectorySet totrueorfalseto override whether trajectory controls are available. The controls only appear when the viewer has multiple trajectory frames.data-zoomtoAn AtomSelectionSpec to fit in the view; omitted fits all atoms.
Trajectory controls can be enabled independently of the edit dialog. This example
loads the repository's aluminum trajectory,
which contains five frames of 256 atoms. Use the play button or frame slider to
explore the trajectory; only the trajectory controls are enabled.
<script src="https://3Dmol.org/build/3Dmol-min.js"></script>
<script src="https://3Dmol.org/build/3Dmol.ui-min.js"></script>
<div id="trajectory-example"
style="height: 400px; width: 400px; max-width: 100%; position: relative;"
class="viewer_3Dmoljs"
data-href="https://3dmol.org/tests/auto/data/Al.lammpstrj"
data-type="lammpstrj" data-options="multimodel:true;frames:true"
data-backgroundcolor="white" data-style="sphere:radius=0.05,color=royalblue"
data-ui-trajectory="true" data-ui-edit="false"
data-callback="function(viewer) { viewer.zoom(4); viewer.render(); }"></div>The callback zooms in on this file's fractional coordinates. The separate UI
bundle provides the playback controls; data-ui-edit="false" disables the edit dialog.
Alternatively, use data-ui="true" data-ui-edit="false". The trajectory bar has a
minimize button that collapses it to a playback icon in the upper left. When the
edit dialog is disabled, that icon uses the hamburger button's position. It stays
hidden when no trajectory is loaded. Minimizing does not stop playback.
The same options are available in JavaScript:
const controls = new $3Dmol.StateManager(viewer, { edit: false, trajectory: true });
controls.setControls({ edit: true, trajectory: false });
controls.ui.setTrajectoryMinimized(true);Omitting edit and trajectory when constructing a StateManager enables both.
setControls changes only the options provided.
Multiple selections, styles, residue labels, and surfaces can be provided by appending a matching suffix
to each attribute. data-select1 supplies the selection for data-style1,
data-surface1, and data-labelres1. Missing selections mean all atoms. Suffixed
styles are applied after the unsuffixed style, in lexicographic attribute order
(so style10 precedes style2); later styles replace earlier representations on
overlapping atoms.
For example:
<div style="height: 400px; width: 400px; position: relative;" class='viewer_3Dmoljs' data-pdb='1YCR'
data-select1='chain:A' data-style1='cartoon:color=spectrum' data-surface1='opacity:.7;color:white' data-select2='chain:B' data-style2='stick'></div> Once created, the viewer is available in viewers ($3Dmol.viewers) under the container's ID, or a numeric key if the container has no ID. The presence of a viewer in this registry does not mean a remote file has finished loading; use data-callback before working with its atoms.
To insert viewers after the initial document load, use createViewer and the JavaScript API. This gives your application ownership of the instance and its loading and cleanup lifecycle.