Docs are under construction. Content may be incomplete or change.

Viewer Widget Reference

Page type: Reference
On this page

The viewer widget puts a live, interactive Tako structure view on any web page. It mounts the same renderer the Tako app uses, so an embedded structure shows the same atom impostors, colour schemes, lighting, and trajectory playback as the app itself.

For a task-oriented walkthrough see Embed a Structure Viewer. This page is the complete contract.

Loading the script

<script src="https://tako.atom.li/embed/tako-viewer.js"></script>

The bundle is one self-contained file. It defines window.Tako and makes no further network request except for the structure files you point it at.

Declarative embedding

Any element with a data-tako-viewer attribute is mounted automatically once the document is ready. The attribute value is the structure URL.

<div data-tako-viewer="/structures/quartz.cif" data-height="480"></div>

Every option below is available as data-<option>, in the same spelling as the JavaScript API. HTML attribute names are case-insensitive, so data-colorscheme and data-color-scheme both reach the colorScheme option.

Programmatic embedding

const viewer = Tako.viewer('#host', { src: '/structures/quartz.cif' });
await viewer.ready;

Tako.viewer(target, options)

Creates and mounts a viewer. target is a CSS selector or an element. Returns a TakoViewer instance immediately; loading continues in the background.

Tako.viewAll(root?)

Mounts every not-yet-mounted [data-tako-viewer] element under root (the document by default). Already-mounted elements are skipped, so it is safe to call after a single-page app renders new content. Returns the instances it created.

Tako.get(element)

Returns the instance mounted into element, or undefined.

Tako.version

The widget contract version, currently "1". It changes only on a breaking change to the contract documented here.

Source options

Give exactly one source. When more than one is present, snapshot wins over data, which wins over src.

OptionTypeMeaning
srcstringURL of a structure file, or of a .json file holding a captured Tako view. Format is detected from the extension.
datastringStructure file contents supplied inline. Requires format.
formatstringcif, xyz, extxyz, poscar, or cube. Required with data; with src it overrides extension detection, which is what a download URL carrying no filename needs.
snapshotobjectA tako-viewer-snapshot object captured by Tako, mounted verbatim.

A .json URL is read as a captured snapshot. Anything else is parsed as a structure file.

Layout options

OptionTypeDefaultMeaning
heightnumber or CSS length420pxA bare number is read as pixels.
widthnumber or CSS lengthfills the host
titlestringthe structure’s own titleCaption shown by the viewer.

Look options

Each of these sets how the scene is drawn. Every one is also a valid entry in controls.

OptionTypeValues
stylestringflat, skeletal, vdw, bTube, bubble
colorSchemestringvesta-soft, jmol, kessoku
materialstringmodern-matte, classic-matte, glossy, metallic, 2-5d, 2d
backgroundstringlight, white, black
atomScalenumberAtom radius, in percent. 100 is the default size.
bondScalenumberBond radius, in percent.
bondColorModestringbicolor splits a bond at its midpoint, unicolor paints it one colour.
edgesbooleanOutline atoms and bonds.
bondsbooleanDraw bonds.
cellbooleanDraw the unit cell box. Periodic structures only.
axesbooleanDraw the orientation axes.
labelsbooleanDraw per-atom element labels.
polyhedrabooleanDraw coordination polyhedra.

An unrecognised value is ignored with a console warning rather than failing the embed, so one bad attribute cannot blank out a working page.

Behaviour options

OptionTypeDefaultMeaning
autoRotatebooleanfalseSpin the scene continuously.
camerastringfitfit frames the structure on load. snapshot keeps the camera stored in a captured snapshot, and has no effect on a structure file, which carries no camera.
controlsboolean, string, or arraynoneWhich controls the reader may adjust. See below.

controls

Controls appear behind a settings button in the corner of the viewer. Accepted forms:

ValueResult
true or "all"Every control.
false, "none", or []No controls. This is the default for a structure file.
"style,background" or ["style", "background"]Just those controls.
"snapshot"Keep whatever panel a captured snapshot was captured with. This is the default when the source is a snapshot.

Valid entries are the look option names: style, colorScheme, material, background, atomScale, bondScale, bondColorMode, edges, bonds, labels, axes, cell, polyhedra.

Instance API

MemberReturnsMeaning
elementelementThe element the widget was mounted into.
readyPromiseResolves once the structure is on screen. Rejects if it cannot load.
load(source?)PromiseReplaces the structure. Takes src, data, format, snapshot, or title. Look options persist across the swap.
set(options)instanceApplies look options to the mounted scene in place, without a reload. Also accepts autoRotate, height, and width.
autoRotate(on?)instanceStarts or stops the spin.
frameCount()numberTrajectory frames. 1 for a static structure.
currentFrame()numberIndex of the frame on screen.
setFrame(index)instanceScrubs to a frame, clamped, pausing playback.
play() / pause()instanceTrajectory playback. No-op for a static structure.
setFps(fps)instancePlayback rate.
camera()object or nullCurrent camera state. null before the viewer mounts.
resetCamera()instanceRe-frames the structure, undoing the reader’s orbit and zoom.
destroy()voidTears the viewer down and releases its WebGL context.

Every method that returns instance can be chained.

Events

viewer.on('select', (atom) => console.log(atom));
EventPayload
readyThe instance.
errorAn Error. Also shown in place inside the widget.
select{ id, element, position } for the clicked atom, or null when the selection is cleared.
cameraThe camera state, on every camera change.
frame{ index, count }, on every trajectory frame change.

off(event, listener) removes a listener.

Behaviour notes

  • WebGL is required. Where it is unavailable the widget shows the failure reason in place and emits error.
  • Cross-origin structure files must be served with permissive CORS headers, since the widget fetches them from the page’s origin.
  • A failed load never throws into your page. ready rejects, error fires, and the reason is shown inside the widget. Attach an error listener or catch ready if you want to react.
  • Concurrent loads settle in order. If load() is called again before a previous load finishes, only the newest one is applied.