lib/visualizer.js~ CrystVis

An object providing a full interface to a renderer for crystallographic models

Constructor

new CrystVis(element, width, height, rendererOptions)

An object providing a full interface to a renderer for crystallographic models

Parameters:
NameTypeDefaultDescription
elementstring

CSS-style identifier for the HTML element to put the renderer in

widthint0

Window width

heightint0

Window height. If both this and width are set to 0, the window fits its context and automatically resizes with it

rendererOptionsObject

Options for the renderer

Members

displayed :ModelView

Displayed atoms

Type:
  • ModelView

highlightSelected :bool

Whether the selected atoms should be highlighted with auras

Type:
  • bool

(readonly) isDisposed :boolean

Whether this instance has been disposed. Once true, most methods will throw rather than silently fail.

Type:
  • boolean

(readonly) model :Model

Currently loaded model

Type:
  • Model

(readonly) modelList :Array

List of loaded models

Type:
  • Array

(readonly) modelName :String

Name of the currently loaded model

Type:
  • String

selected :ModelView

Selected atoms

Type:
  • ModelView

theme :object

Theme

Type:
  • object

Methods

addNotification()

Add a notification to the list of notifications to be displayed

addNotifications()

Adds all notifications to the drawing

addPrimitive(p)

Add a primitive shape to the drawing

Parameters:
NameTypeDescription
pTHREE.Object3D

Primitive to add

clearNotifications()

Removes notifications from the drawing

deleteModel(name)

Erase a model from the recorded ones

Parameters:
NameTypeDescription
nameString

Name of the model to delete

displayModel(name)

Render a model

Parameters:
NameTypeDefaultDescription
nameStringnull

Name of the model to display. If empty, just clear the renderer window.

dispose()

Release all resources held by this instance: cancels the animation loop, removes all canvas event listeners, disposes OrbitControls and the THREE.WebGLRenderer, and nulls internal references. After calling this method the instance must not be used again.

getCameraState() → {Object}

Return a plain serialisable snapshot of the current camera state.

Returns:
Type: 
Object

getModelMeta(name) → {Object|null}

Return metadata stored alongside the named model: { prefix, originalName }.

Parameters:
NameTypeDescription
nameString

Model name

Returns:
Type: 
Object | null

getModelParameters(name) → {Object|null}

Return the loading parameters that were used when the named model was last loaded / reloaded (a clone of the merged parameter object).

Parameters:
NameTypeDescription
nameString

Model name

Returns:
Type: 
Object | null

getModelSource(name) → {Object|null}

Return the raw file text and format extension originally passed to loadModels() for the named model.

Parameters:
NameTypeDescription
nameString

Model name

Returns:
Type: 
Object | null

getScreenshotData() → {String}

Recover a data URL of a PNG screenshot of the current scene

Returns:

A data URL of the PNG screenshot

Type: 
String

loadModels(contents, format, prefix, parameters) → {Object}

Load one or more atomic models from a file's contents

Parameters:
NameTypeDefaultDescription
contentsString

The contents of the structure file

formatStringcif

The file's format (cif, xyz, etc.). Default is cif.

prefixStringnull

Prefix to use when naming the models. Default is empty.

parametersObject

Loading parameters:

  • supercell: supercell size (only used if the structure is periodic)
  • molecularCrystal: if true, try to make the model load completing molecules across periodic boundaries
  • useNMRActiveIsotopes: if true, all isotopes are set by default to the most common one with non-zero spin
  • vdwScaling: scale van der Waals radii by a constant factor
  • vdwElementScaling: table of per-element factors to scale VdW radii by
Returns:

Status map of the models we tried to load. Keys are the model names (strings that can be passed directly to displayModel()). Values are 0 for a successful load, or an error message string if loading failed. Example: to display the first loaded model, use visualizer.displayModel(Object.keys(loaded)[0]) and check loaded[modelName] !== 0 to detect errors.

Type: 
Object

onAtomBox(callback)

Set a callback function for an event where a user drags a box around multiple atoms. The function should take as arguments a ModelView including the atoms in the box:

function callback(view) { ... }

Parameters:
NameTypeDefaultDescription
callbackfunctionnull

Callback function for the event. Passing "null" restores default behaviour

onAtomClick(callback, modifiers)

Set a callback function for an event where a user clicks on an atom. The function should take as arguments the atom image for the clicked atom and the event object:

function callback(atom, event) { ... }

Parameters:
NameTypeDefaultDescription
callbackfunctionnull

Callback function for the event. Passing "null" restores default behaviour

modifiersint

Click event. Use the following flags to define it:

  • CrystVis.LEFT_CLICK
  • CrystVis.RIGHT_CLICK
  • CrystVis.MIDDLE_CLICK
  • CrystVis.CTRL_BUTTON
  • CrystVis.ALT_BUTTON
  • CrystVis.SHIFT_BUTTON
  • CrystVis.CMD_BUTTON

For example, CrystVis.LEFT_CLICK + CrystVis.SHIFT_BUTTON defines the event for a click while the Shift key is pressed.

onCameraChange(callback) → {function}

Subscribe to camera-change events (rotate, pan, zoom). The callback receives a snapshot identical to CrystVis#getCameraState.

Parameters:
NameTypeDescription
callbackfunction

callback(cameraState)

Returns:

Unsubscribe function

Type: 
function

onDisplayChange(callback) → {function}

Subscribe to display-change events fired whenever displayModel() completes. The callback receives the name of the newly displayed model (or null when cleared).

Parameters:
NameTypeDescription
callbackfunction

callback(modelName: string|null)

Returns:

Unsubscribe function

Type: 
function

onModelListChange(callback) → {function}

Subscribe to model-list change events (model added or deleted). The callback receives the new list of model names.

Parameters:
NameTypeDescription
callbackfunction

callback(modelNames: string[])

Returns:

Unsubscribe function

Type: 
function

reloadModel(name, parameters)

Reload a model, possibly with new parameters

Parameters:
NameTypeDescription
nameString

Name of the model to reload.

parametersObject

Loading parameters as in .loadModels()

removePrimitive(p)

Remove a primitive shape from the drawing

Parameters:
NameTypeDescription
pTHREE.Object3D

Primitive to remove

setCameraState(state)

Restore a camera snapshot produced by CrystVis#getCameraState. Safe to call after displayModel().

Parameters:
NameTypeDescription
state

unloadAll()

Remove all loaded models and reset the view in a single atomic operation (only one render pass after everything is cleared, unlike calling deleteModel() in a loop).