Displaying Interactive 3D Model Views in Web Browsers
Introduction
Debug text
Loading...
Molecular visualization has been a main focus of the RBVI since its inception. UCSF Chimera is our application for visualizing and manipulating many types of three-dimensional data. However, there are times when users wish to illustrate one particular view of models via the web without requiring readers to download and install applications or plugins. Typically, this is done by creating a movie, either an animation (where the reader only gets to look at a prepared series or images) or as an interactive view (where the reader can control the direction of view). The latter is typically done using either Apple Quicktime VR or Adobe Flash, both of which require readers to have plugins or applications installed on their computer. In addition, instructions for constructing Quicktime VR or Flash movies for 3D viewing are either sparse or require commercial software. In this tech note, we describe a method for creating an interactive 3D model views in web browsers using only Chimera, HTML/CSS and Javascript. An example is in the image on the right.Procedure
The implementation of an interactive web image is actually quite simple: load images of the same scene viewed from a variety of directions, then display the appropriate one in response to user input.
To get a reasonably smooth motion, we generate images at ten-degree intervals both longitudinally and latitudinally.
To select the appropriate view, we keep track of the azimuth (longitude) and inclination (latitude) angles. Mouse motion in the X direction proportionally changes the azimuth angle, while motion in Y changes the inclination angle. These two angles are mapped to the nearest longitude and latitude where we generated an image, and that image is displayed.
To help the user orient himself, we add a globe in the upper-left corner. The globe displays the current view orientation, and shows the user that horizontal mouse motion translates to rotation around the pole while vertical motion translates to tilting the pole.
Generating Images
To generate scene images from different view directions, we use UCSF Chimera. Once the view has been constructed (window size set to match image dimensions; model representations and colors selected; initial view chosen; etc.), we open the genview.py script which generates a series of image files named
img-III-AAA.png
, whereIII
andAAA
are three-digit numbers representing the inclination and azimuth angle values. Since inclination ranges from [0..180] and azimuth from [0..360), and we generate an image every 10 degrees, there are 684 files.This script was used to generate the globe images at the upper-left corner. The globe BILD object was generated using genglobe.py, displayed in Chimera with transparent background enabled. The globe image size was selected to balance visibility (so that the globe orientation is easily seen) and size (so that it does not take up too much screen area over the displayed view).
Creating Web Content
The web content for displaying an interactive 3D model view consists of:
- a Javascript file (globeview.js),
- two folders of images (globe and, for example, 1gc1),
- and the main HTML file (for example, show.html).
The Javascript file contains all the code for preloading images (so that rotations are smooth) and for tracking mouse motion to update the displayed image.
The two image folders are for the globe images and the view images. The contents of both folders should be the 684 image files generated using genview.py. In particular, the file names must be of form img-III-AAA.png, because globeview.js uses the same convention. The globe folder contains images of the upper-left corner globe; the 1gc1 example folder contains the view images.
The main HTML file must have three major elements:
- a
script
tag referencing globeview.js,- a
div
containingimage
tags with identifiersmain
andglobe
,- and a call to the
init
function defined in globeview.js.The reference to globeview.js, typically in the
head
section within ascript
tag, is required to include the necessary Javascript code.The view and globe image elements should be nested within a
div
element to control relative placement. Below is the HTML used in show.html:The outer<div id="globeview" style="position:relative;"> <img id="main" style="position:relative; left:0px; top:0px;"/> <img id="globe" style="visibility:hidden; position:absolute; left:0px; top:0px;"/> <p id="debug" style="visibility:hidden;">Debug</p> <p id="text" style="visibility:hidden;">Loading...</p> </div>div
uses CSSrelative
positioning so that the image elements can be aligned at its upper left corner. Themain
element displays the view image, while theglobe
element displays the globe image. Their CSS placement arerelative
andabsolute
respectively to force alignment. Thedebug
andtext
elements are only used for informational purposes and are optional. It is important that no other elements usemain
,globe
,debug
ortext
as their id names because the Javascript code expects these elements to be unique and of the correct type.A call to the
init
function is needed in order to start preloading images and monitoring mouse motion. In our example, show.html, the call is made as part of theonload
callback of thebody
tag. Other possibilities include makinginit
the callback function for an HTML button (see button.html), so that images are only downloaded in response to user action rather than spontaneously.Limitations
The code and examples shown here are only for reference purposes. Clearly there are some limitations that will need to be addressed when used in more complex scenarios.
Currently, the Javascript code will only handle a single view because it uses fixed "id" names to find the image element; to handle multiple images on the same page, the code will need to be updated to use Javascript classes where each instance can be initialized with its own image id.
The image positioning CSS code shown here is only one of many possible variations. The code above known to work with Firefox 3.5 and Internet Explorer 8.
Discussion
As the example above shows, our method works reasonably well, but with some issues. Some shortcomings are:
- The images from different view directions are stored as separate files. This means that we are compressing each image individually and not making use of spatial proximity across images that can potentially (greatly) reduce the amount of data that need to be sent to the web browser. It may be possible to use a single multi-image file, eg MNG or animated GIF, but we have not found the Javascript API for manipulating such files.
- Javascript must be enabled in the user's web browser. Some users disable Javascript for security reasons. Because the mouse-monitoring code is in Javascript, the interactive aspect of our method will not be in effect in such cases. As a silver lining, if Javascript is disabled, the browser will not waste network bandwidth downloading images.
The advantages of our method are:
- Readers do not need to download and install any applications or web browser plugins.
- The user interface is simple and easy to understand, especially with the globe displayed in the upper-left corner.
Laboratory Overview | Research | Outreach & Training | Available Resources | Visitors Center | Search