CifVis Widget Demo

A comprehensive guide to utilizing the CifVis widget for crystal structure visualization

Introduction

CifVis is a JavaScript library that allows you to visualize crystallographic information files (CIF) directly in a web browser. The library provides a convenient web component (<cifview-widget>) that makes it easy to display interactive 3D models of crystal structures in your web pages.

Adding a crystal structure visualization to your webpage is as simple as including the CifVis library and adding the <cifview-widget> element:

<script type="module" src="https://niolon.github.io/cifvis/dist/cifvis.alldeps.js"></script>

<cifview-widget 
    src="path/to/structure.cif"
    caption="Crystal Structure">
</cifview-widget>

Although you might consider uploading the library to your own webserver if you are after something persistent.

This demo page showcases various features of the CifVis widget and provides code examples for implementation.

Basic Usage

The CifVis widget provides several interactive features:

Interactive Example (try selecting atoms and bonds):

Loading CIF Data

You can load CIF data in two ways:

From URL
From String

Load a CIF file from a URL:

<cifview-widget 
    src="https://example.com/path/to/structure.cif"
    caption="Structure from URL">
</cifview-widget>

Load CIF data directly from a string:

<cifview-widget 
    data="data_
_cell.length_a 10.5
_cell.length_b 12.3
_cell.length_c 15.2
_cell.angle_alpha 90
_cell.angle_beta 90
_cell.angle_gamma 90
# ... rest of CIF data ...
"
    caption="Structure from data string">
</cifview-widget>

Display Options

The CifVis widget provides several display options that can be controlled via attributes:

Hydrogen atoms hidden:
Hydrogen atoms displayed:
Anisotropic hydrogens:
Disorder - all groups shown:

Growing symmetry

You can grow the symmetry one additional sphere using bonds or hydrogen bonds contained in the cif tables:

Assymetric Unit:
Grow with both bond types:

Setting Display Options

Set initial display options using the following attributes, if a display mode is incompatible with the given structure (say there are no anisotropic hydrogens, even though hydrogen-mode="anisotropic" was selected as start mode), the widget will automatically fall back to a valid option:

<cifview-widget 
    src="structure.cif"
    caption="My Structure"
    hydrogen-mode="constant"
    disorder-mode="all"
    symmetry-mode="bonds-yes-hbonds-no">
</cifview-widget>
Attribute Values Description
hydrogen-mode none, constant, anisotropic Controls the display of hydrogen atoms
disorder-mode all, group1, group2 Controls which disorder groups are displayed.
group1 displays disorder group 1 and non-disorder atoms,
group2 displays all but disorder groups 1 atoms.
symmetry-mode bonds-yes-hbonds-yes, bonds-yes-hbonds-none, bonds-none-hbonds-no, etc. Controls symmetry-generated atoms and bonds,
yes/noswitches growing on of off,
none means there are no bonds/H-bonds to grow.
Only one additional sphere is grown.

Filtered Atoms

You can exclude specific atoms from the visualization using the filtered-atoms attribute. This is particularly useful for removing solvent molecules or counter-ions to focus on the main structure. Individual atoms are separated by a comma a range (inclusive) can be indicated by the > sign. The used order is determined by the atom_site table:

Complete structure with solvent:
Structure with solvent removed:
<cifview-widget 
    src="structure.cif"
    caption="Structure with solvent removed"
    filtered-atoms="C200,H200,C201>H218">
</cifview-widget>

Configuration Options

CifVis provides extensive configuration options through the options attribute. This attribute accepts a JSON string with various settings that control the appearance and behavior of the visualization.

These options control the camera behavior and settings for the 3D viewer.

Option Type Description
camera.minDistance Number Minimum camera distance when zooming. Default: 1
camera.maxDistance Number Maximum camera distance when zooming. Default: 100
camera.wheelZoomSpeed Number Speed of zooming when using the mouse wheel. Default: 0.0008
camera.pinchZoomSpeed Number Speed of zooming when using pinch gestures on touch devices. Default: 0.001
camera.initialPosition Array Initial camera position as [x, y, z] coordinates. Default: [0, 0, 10]
camera.fov Number Field of view in degrees. Default: 45
camera.near Number Near clipping plane distance. Default: 0.1
camera.far Number Far clipping plane distance. Default: 1000

These options control the behavior and appearance of atom and bond selections.

Option Type Description
selection.mode String Selection mode, either "single" or "multiple". Default: "multiple"
selection.markerMult Number Size multiplier for atom selection markers. Default: 1.3
selection.bondMarkerMult Number Size multiplier for bond selection markers. Default: 1.7
selection.highlightEmissive Number (hex) Emissive color value for highlighted selections. Default: 0xaaaaaa
selection.markerColors Array Array of colors (hex values) used for selection markers. Default: cycle of 10 different colors

These options control how users interact with the 3D structure.

Option Type Description
interaction.rotationSpeed Number Speed of rotation when dragging. Default: 5
interaction.clickThreshold Number Maximum time in milliseconds for an interaction to be considered a click. Default: 200
interaction.mouseRaycast.lineThreshold Number Threshold for line selection with mouse. Default: 0.5
interaction.mouseRaycast.pointsThreshold Number Threshold for point selection with mouse. Default: 0.5
interaction.mouseRaycast.meshThreshold Number Threshold for mesh selection with mouse. Default: 0.1
interaction.touchRaycast.lineThreshold Number Threshold for line selection with touch. Default: 2.0
interaction.touchRaycast.pointsThreshold Number Threshold for point selection with touch. Default: 2.0
interaction.touchRaycast.meshThreshold Number Threshold for mesh selection with touch. Default: 0.2

These options control how the 3D structure is rendered.

Option Type Description
renderMode String Rendering mode, either "constant" (render continuously) or "onDemand" (render only when needed). Default: "onDemand"
fixCifErrors Boolean Whether to attempt to fix common errors in CIF files. Default: false

These options control the appearance of atoms in the 3D structure.

Option Type Description
atomDetail Number Level of detail for atom geometry (1-5). Default: 3
atomColorRoughness Number Roughness of atom material (0-1). Default: 0.3
atomColorMetalness Number Metalness of atom material (0-1). Default: 0.5
atomADPRingWidthFactor Number Width factor for ADP (Anisotropic Displacement Parameter) rings. Default: 1.0
atomADPRingHeight Number Height of ADP rings. Default: 0.06
atomADPRingSections Number Number of sections in ADP rings. Default: 18
atomADPInnerSections Number Number of inner sections in ADP rings. Default: 7
atomConstantRadiusMultiplier Number Multiplier for atom radius when using constant mode. Default: 0.25

These options control the appearance of bonds in the 3D structure.

Option Type Description
bondRadius Number Radius of bond cylinders. Default: 0.05
bondSections Number Number of sections in bond cylinders. Default: 15
bondColor String Default color for bonds. Default: "#666666"
bondColorRoughness Number Roughness of bond material (0-1). Default: 0.3
bondColorMetalness Number Metalness of bond material (0-1). Default: 0.1
bondGrowToleranceFactor Number Tolerance factor for bond growing/generation. Default: 1.2

These options control the appearance of hydrogen bonds in the 3D structure.

Option Type Description
hbondRadius Number Radius of hydrogen bond cylinders. Default: 0.04
hbondColor String Default color for hydrogen bonds. Default: "#AAAAAA"
hbondColorRoughness Number Roughness of hydrogen bond material (0-1). Default: 0.3
hbondColorMetalness Number Metalness of hydrogen bond material (0-1). Default: 0.1
hbondDashSegmentLength Number Target length for each dash+gap segment. Default: 0.3
hbondDashFraction Number Fraction of segment that is solid (vs gap). Default: 0.6

The elementProperties option allows you to customize the appearance of specific elements. It contains a mapping of element symbols to their properties.

Element Property Type Description
elementProperties.X.radius Number Radius of the element X in Å. Used for bond distance calculation and visualization.
elementProperties.X.atomColor String Color of the element X. Can be any valid CSS color.
elementProperties.X.ringColor String Color of anisotropic displacement parameter rings for element X.

Example of customizing element properties:

{
  "elementProperties": {
    "Fe": {
      "radius": 1.4,
      "atomColor": "#FF5733",
      "ringColor": "#ffffff"
    },
    "O": {
      "atomColor": "#0088ff",
      "radius": 0.7
    }
  }
}

Complete Configuration Example

Here's a comprehensive example showing how to use the options attribute to customize various aspects of the CifVis widget:

<cifview-widget 
    src="structure.cif"
    caption="Customized Crystal Structure"
    options='{
        "camera": {
            "fov": 50,
            "initialPosition": [0, 0, 15],
            "wheelZoomSpeed": 0.001
        },
        "selection": {
            "mode": "multiple",
            "markerMult": 1.5,
            "bondMarkerMult": 2.0
        },
        "atomDetail": 4,
        "atomColorRoughness": 0.5,
        "atomColorMetalness": 0.3,
        "bondRadius": 0.08,
        "bondColor": "#555555",
        "elementProperties": {
            "Fe": {
                "atomColor": "#FF5733",
                "radius": 1.4,
                "ringColor": "#ffffff"
            },
            "O": {
                "atomColor": "#0088ff",
                "radius": 0.7
            }
        }
    }'>
</cifview-widget>