LEADTOOLS JavaScript (Leadtools.Controls)
LEAD Technologies, Inc

ImageViewer Object

Example 

Members 
Represents a scrollable control that displays an image with optional interactive UI operations.
Object Model
ImageViewer ObjectImageViewerInteractiveMode ObjectInteractiveService ObjectImageViewerInteractiveMode ObjectImageViewerInteractiveMode ObjectImageViewerInteractiveMode Object
Syntax
function Leadtools.Controls.ImageViewer() 
!MISSING Scrap '_RTJavaScript_Class_SYNTAX'!
Remarks

Usage

To use the ImageViewer class in your HTML 5 application:

  1. Add a DIV element with a unique ID to your HTML page. This element will act as the parent container for the viewer

  2. In your JavaScript code, create a new instance of ImageViewer, passing the DIV element ID into the ImageViewerCreateOptions.ParentDivElementId property.

  3. Start using the control. For example, set the URL to an image file into the ImageUrl property.

Features

ImageViewer supports the following functionality:

Components

ImageViewer requires that your browser supports the HTML 5 canvas element.

To understand the various elements of the ImageViewer, consider the example where you have the following HTML:

    
            <div id="myContainerDiv"></div>
            

  

In your JavaScript, use the following code to create the viewer:

    
            // Create the viewer
            var createOptions = new Leadtools.Controls.ImageViewerCreateOptions('myContainerDiv', 'myViewer');
            var viewer = new Leadtools.Controls.ImageViewer(createOptions);
            

  

This will result in the following HTML:

    
            <div id="myContainerDiv">
              <!-- This is DIV -->
              <div id="myViewer_div">
                 <!-- The viewer auto scroll DIV, this will not be created for mobile or tablet devices -->
                 <div id="myViewer_autoScrollDiv"></div>
              </div>
              <!-- The viewer canvas (foreground) -->
              <canvas id="myViewer_canvas"></canvas>
            </div>
            

  

From the above, you can deduce that creating an image viewer inside a DIV will add the following elements to your HTML page:

Element ID (as string) ImageViewer property ID Description

Main viewer DIV

ImageViewerCreateOptions.ControlId_div

DivId

The main viewer DIV. You can customize this DIV element by providing the name of the CSS class to use in the ImageViewerCreateOptions.DivClassName property.

Auto scroll DIV

ImageViewerCreateOptions.ControlId_autoScrollDiv

AutoScrollDivId

The auto scroll DIV is used in desktop browsers only to show and control the size of the scroll bars. Mobile and tablet browsers do not support scrollbars and this DIV will not be created. Panning is supported in either case. You can customize this DIV element by providing the name of the CSS class to use in the ImageViewerCreateOptions.AutoScrollDivClassName property.

Foreground Canvas

ImageViewerCreateOptions.ControlId_canvas

CanvasId

The foreground canvas. The current image will be drawn into the surface of this canvas. You can customize this Canvas element by providing the name of the CSS class to use in the ImageViewerCreateOptions.CanvasClassName property.

In addition to these elements, the image viewer will create the following elements that are not added to the HTML page but are required for the viewer operation. Since these elements are not added to the page, they do not use an ID. However, you can use these elements through the corresponding property directly:

Element ImageViewer Property Description

Background Canvas

BackCanvas

The background canvas. The current image will be drawn onto the surface of this canvas and then applied to the foreground canvas when needed. Owner draw can also be used to manually draw an image or any other shape on the back canvas.

Viewing an Image

Use one of the following methods to view an image with the ImageViewer:

Rendering

As described in the Components section, the image viewer contains two canvas elements, a foreground canvas (its ID is stored in the CanvasId) that is created when the viewer initializes and is added to the HTML page inside the main DIV element. This canvas will always be the same size as the current viewable area of the viewer (not the entire image size) to optimize rendering and allow the viewer to display images that are much greater in size than the maximum allowed by the browser. This canvas displays the current image view with transformation and effects.

The other element is the background canvas (BackCanvas). This canvas is created when the user sets an image in the viewer and is never added to the HTML page. The size of this canvas is the same size as the image. Since this canvas is never added to the page, it has no restriction on size.

Setting a new image, zooming in, and applying an effect are all actions that will cause the viewer to re-calculate the transformation matrix (Transform) and call the OnTransformChanged method. This method will call the Invalidate method and cause the viewer to be in an invalid state that requires rendering. TheInvalidate method can also be called manually to trigger a render.

When the viewer is invalidated, the following occur:

  1. First, the viewer will determine whether the BeginUpdate method has been called without a matching EndUpdate method.

  2. If so, the EraseBackground event is fired and the background of the fore canvas is cleared.

  3. Then the PreRenderImage event is fired.

  4. The content of the background canvas is drawn onto the foreground canvas using the current transformation matrix (Transform)

  5. Then the PostRenderImage event is fired.

If you set the value of OwnerDraw to true, then the viewer will not draw the background canvas onto the foreground canvas (step 5 above). This can be used to implement custom painting on the foreground canvas directly.

Use the PostRenderImage method to add extra drawing on top of the image, such as annotations or labels.

Notes

The ImageViewer will add HTML elements to the page and subscribe to multiple JavaScript events. If your application involves adding and removing ImageViewer objects from the page, then you must call the Dispose method to remove the object from the page and clean up any resources being used.

The viewer hooks up to the standard HTML Window "sizechanged" or "orientationchange" events, depending on which browser being used. This allows the viewer to recalculate the transformation matrix and cause rendering if the size of the control (container DIV) changes. If however, the size of the control is changed programmatically or due to CSS rules that do not fire a "sizechanged" or "orientationchange" event, then the control will not be updated. You must call the OnSizeChanged method to trigger the control to update its transformation.

Example
  • JavaScript
  •   
     
    <!DOCTYPE html>
    <html>
    <head>
       <title>Leadtools Examples</title>
       <meta http-equiv="X-UA-Compatible" content="IE=9" />
       <meta http-equiv="content-type" content="text/html; charset=utf-8" />
       <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" />
       <meta name="apple-mobile-web-app-capable" content="yes" />
       <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
       <style>
          #imageViewerDiv
          {
             border: 1px solid #000000;
             width: 400px;
             height: 400px;
             background-color: #7F7F7F;
          }
       </style>
       <script type="text/javascript" src="Scripts/Leadtools.js"></script>
       <script type="text/javascript" src="Scripts/Leadtools.Controls.js"></script>
       <script type="text/javascript">
          (function () {
             DefaultPage = function DefaultPage() {
             }
    
             DefaultPage.prototype = {
    
                example: function SiteLibrary_DefaultPage$example(viewer) {
                   // TODO: add example code here
                   alert("example code goes here");
                },
    
                run: function SiteLibrary_DefaultPage$run() {
                   // Create the viewer
                   var createOptions = new Leadtools.Controls.ImageViewerCreateOptions("imageViewerDiv", "myViewer");
                   var viewer = new Leadtools.Controls.ImageViewer(createOptions);
    
                   // Set the interactive mode to PanZoom
                   var interactiveMode = new Leadtools.Controls.ImageViewerPanZoomInteractiveMode();
                   viewer.set_defaultInteractiveMode(interactiveMode);
    
                   // Set the image URL
                   viewer.set_imageUrl("http://www.leadtools.com/images/boxshots/leadtools-200x250.jpg");
    
                   var exampleButton = document.getElementById("exampleButton");
                   var _this = this;
                   exampleButton.addEventListener("click", function(e) {
                      _this.example(viewer);
                   }, false);
                },
    
                dispose: function SiteLibrary_DefaultPage$dispose() {
                },
             }
    
             DefaultPage.registerClass("DefaultPage", null, ss.IDisposable);
             var page = null;
             var windowLoad = null;
             var windowUnload = null;
             windowLoad = function (e) {
                window.removeEventListener("load", windowLoad, false);
                page = new DefaultPage();
                page.run();
                window.addEventListener("unload", windowUnload, false);
             };
             windowUnload = function (e) {
                window.removeEventListener("unload", windowUnload, false);
                page.dispose();
             };
             window.addEventListener("load", windowLoad, false);
          })();
       </script>
    </head>
    <body>
       <p>Press and drag on the image to pan</p>
       <p>Hold down the control key and press and drag on the image or pinch with two fingers to zoom in and out</p>
       <div>
          <input type="button" id="exampleButton" value="Example" />
          <label id="exampleLabel" />
       </div>
       <div id="imageViewerDiv" />
    </body>
    </html>
See Also

Reference

ImageViewer Members
Leadtools.Controls Namespace

 

 


Products | Support | Contact Us | Copyright Notices

© 2006-2012 All Rights Reserved. LEAD Technologies, Inc.