Image Viewer Interactive Modes

Summary

The ImageViewer class supports rich built-in and fully customizable and extensible user-interface interaction support for panning, zooming, magnify glass, rubber banding and many more. Support for both mouse and touch input.

To use an interactive mode, create an instance of one of types derived from ImageViewerInteractiveMode and add it to the ImageViewer.InteractiveModes collection. For example:

JavaScript Example
// Create an instance of Pan/Zoom interactive mode 
var mode = new lt.Controls.ImageViewerPanZoomInteractiveMode(); 
// Add it to the viewer 
imageViewer.interactiveModes.add(mode); 

With the viewer is set up like the example above, clicking and dragging with the mouse on the viewer surface will pan the image. CTRL-clicking and dragging will zoom the image in and out. Similarly on devices that support touch input, pressed down and dragging will pan the image while dragging with two fingers will zoom in and out.

ImageViewer supports the following built in and fully functional interactive modes:

Interactive Mode Description
ImageViewerPanZoomInteractiveMode

Provides interactive pan and zoom user interface functionality to an ImageViewer

ImageViewerZoomToInteractiveMode

Zooms to the image rectangle created by the user using mouse or touch

ImageViewerMagnifyGlassInteractiveMode

Magnifies portion of the image under the mouse or touch

ImageViewerSpyGlassInteractiveMode

Generic spy glass interactive mode

ImageViewerCenterAtInteractiveMode

Centers (and optionally zooms) the image around the user mouse click or touch tap

ImageViewerZoomAtInteractiveMode

Zooms the image around the user mouse click or touch tap

ImageViewerRubberBandInteractiveMode

Draws a temporary rectangle on top of the image using the mouse or touch. Can be used to perform any extra functionality such as drawing a region of interest for a user-defined operation

ImageViewerNoneInteractiveMode

Empty interactive mode

ImageViewerAutoPanInteractiveMode

Automatically pans the image to a direction when the user is dragging near the edge of the viewer

ImageViewerActiveItemInteractiveMode

Changes the active item upon user mouse click or touch tap

ImageViewerPagerInteractiveMode

Flips the pages of multi-page image when the user drags on the viewer surface

ImageViewerSelectItemsInteractiveMode

Provides single and multi-selection support using mouse or touch input

These interactive modes have extra functionality to further customize the behavior as well as being fully extensible for further detailed operations.

The ImageViewer.InteractiveModes property is a collection, hence, you can add as many modes to the viewer as desired. This allows you to:

  • Add all the interactive modes needed by the application once. Then enable only the mode for the current application state.

  • Some interactive modes can be combined together to add further functionality

The following example will be used by all the code snippets below to demonstrate how to use this collection. Assume imageViewer is the ID of a div in the HTML page:

JavaScript Example
// Get the div item for the viewer 
var div = document.getElementById("imageViewer"); 
var createOptions = new lt.Controls.ImageViewerCreateOptions(div); 
// Create a new image viewer instance using the default constructor 
var imageViewer = new lt.Controls.ImageViewer(createOptions); 
// Load an image into it 
imageViewer.imageUrl = "http://localhost/images/image1.png"; 

Which mode handles the input action depends on the following:

The order of the modes in the collection

The mode at the top of the collection (at index 0) will have first chance of handling the action. If this mode does not handle the action, then it will be passed to the next mode in the list (if any).

Add the following code to the example after setting the image:

JavaScript Example
// Add Pan/Zoom mode first 
var panZoomMode = new lt.Controls.ImageViewerPanZoomInteractiveMode(); 
imageViewer.interactiveModes.add(panZoomMode); 
// And Rubber Band mode second 
var rubberBandMode = new lt.Controls.ImageViewerRubberBandInteractiveMode(); 
imageViewer.interactiveModes.add(rubberBandMode); 

Run the example and notice that only pan/zoom is usable, rubber-band does not work. Now change code like this:

JavaScript Example
// And Rubber Band mode first 
var rubberBandMode = new lt.Controls.ImageViewerRubberBandInteractiveMode(); 
imageViewer.interactiveModes.add(rubberBandMode); 
// Add Pan/Zoom mode second 
var panZoomMode = new lt.Controls.ImageViewerPanZoomInteractiveMode(); 
imageViewer.interactiveModes.add(panZoomMode); 

Run the example and notice now that you can draw rubber bands but cannot pan/zoom anymore.

You can also change the order by using the collection to add/remove and insert items in different locations.

The value of ImageViewerInteractiveMode.IsEnabled

Modes that are disabled will never run and ignore all events. IsEnabled is true by default.

Change the code in the example as follows:

JavaScript Example
// And Rubber band first 
var rubberBandMode = new lt.Controls.ImageViewerRubberBandInteractiveMode(); 
// But disable it 
rubberBandMode.isEnabled = false; 
imageViewer.interactiveModes.add(rubberBandMode); 
// Add Pan/Zoom mode second 
var panZoomMode = new lt.Controls.ImageViewerPanZoomInteractiveMode(); 
imageViewer.interactiveModes.add(panZoomMode); 

Run the example and even though rubber band is the first mode; it is not usable, pan zoom is.

This is handled by each mode through the use of the InteractiveEventArgs.IsHandled property.

The value of ImageViewerInteractiveMode.MouseButtons

Used when the device supports mouse input. This value is MouseButtons.Left, but you can change it to attach an interactive mode to a different button.

Change the code in the example as follows:

JavaScript Example
// And Rubber band first 
var rubberBandMode = new lt.Controls.ImageViewerRubberBandInteractiveMode(); 
// Assign it to left mouse button (this is the default) 
rubberBandMode.mouseButtons = lt.Controls.MouseButtons.left; 
imageViewer.interactiveModes.add(rubberBandMode); 
// Add Pan/Zoom mode second 
var panZoomMode = new lt.Controls.ImageViewerPanZoomInteractiveMode(); 
// Assign to the right mouse button 
panZoomMode.mouseButtons = lt.Controls.MouseButtons.right; 
imageViewer.interactiveModes.add(panZoomMode); 

Run the example. Now using the left mouse button will draw a rubber band while using the right mouse button will pan and zoom.

The order of the modes and the state of IsEnabled still applies when multiple modes are attached to the same mouse button.

Note that in touch devices that do not support a mouse. The touch input is treated as a left mouse button. Attaching a mode to any other mouse button will have no effect. However, if the application is designed to support both mouse and touch events (for example, a web application) then you can assign modes to left and right mouse buttons for extra functionality when the application is running in a desktop browser that supports mouse or both mouse and touch.

You can also set an interactive mode MouseButtons property to a combination of values. For example MouseButtons.Left | MouseButtons.Right. This will instruct the mode to run when either mouse button is pressed.

Some interactive modes can run when the value of MouseButtons is MouseButtons.None. For example:

JavaScript Example
// Add auto-pan attached to left mouse button 
var autoPanMode = new lt.Controls.ImageViewerAutoPanInteractiveMode(); 
autoPanMode.mouseButtons = lt.Controls.MouseButtons.left; 
imageViewer.interactiveModes.add(autoPanMode); 

Run the example and click and move the mouse to the edge of the viewer and see how it pans the image to that direction. Now change as follows:

JavaScript Example
// Add auto-pan, not attached to any button 
var autoPanMode = new lt.Controls.ImageViewerAutoPanInteractiveMode(); 
autoPanMode.MouseButtons = lt.Controls.MouseButtons.none; 
imageViewer.interactiveModes.add(autoPanMode); 

Run the example and see how auto-pan works now when no mouse buttons is pressed.

The value of ImageViewerInteractiveMode.CanStartWork

This virtual method is called by the base class to check the standard conditions (such as the IsEnabled and MouseButtons property described above) as well as any further custom condition such as the value of WorkOnBounds, Item, ItemPart.

Change the code in the example to use multiple-items:

JavaScript Example
// Get the div item for the viewer 
var div = document.getElementById("imageViewer"); 
// Create a new image viewer instance with a vertical layout 
var createOptions = new lt.Controls.ImageViewerCreateOptions(div); 
createOptions.viewLayout = new lt.Controls.ImageViewerVerticalViewLayout(); 
var imageViewer = new lt.Controls.ImageViewer(createOptions); 
// Add a border (need some padding as well) 
imageViewer.imageBorderThickness = 1; 
// Add some padding 
imageViewer.itemPadding = lt.Controls.ControlPadding.createAll(80); 
// Use a border around the items so we can see them 
imageViewer.itemBackgroundColor = "yellow"; 
imageViewer.itemBorderThickness = 1; 
// Add a couple of items 
imageViewer.items.addFromUrl("http://localhost/images/image1.png", lt.LeadSizeD.empty); 
imageViewer.items.addFromUrl("http://localhost/images/image2.png", lt.LeadSizeD.empty); 

Now add the following interactive mode:

JavaScript Example
var mode = new lt.Controls.ImageViewerRubberBandInteractiveMode(); 
imageViewer.interactiveModes.add(mode); 

Notice how if you try to draw a rubber band outside the yellow item boxes, the mode does not run. This is because the default value of ItemPart is Content and the default value of WorkOnBounds is true. Meaning: Only work on the content (inside the item) part of an item. The mode will perform hit-testing on the item and if the condition is not met, it will not run.

Change the code as follows:

JavaScript Example
var mode = new lt.Controls.ImageViewerRubberBandInteractiveMode(); 
mode.workOnBounds = false; 
imageViewer.interactiveModes.add(mode); 

Now even though the part is still Content, we instructed the mode to ignore this value and not erform hit-testing. Run the code and notice how you can pan/zoom on any part inside the viewer control.

To work on a specific part of the item, we need to:

For example, this code will make sure rubber band will only work on the image part of an item.

JavaScript Example
var mode = new lt.Controls.ImageViewerRubberBandInteractiveMode(); 
mode.autoItemMode = lt.Controls.ImageViewerAutoItemMode.autoSet; 
mode.itemPart = lt.Controls.ImageViewerItemPart.image; 
mode.workOnBounds = true; 
imageViewer.interactiveModes.add(mode); 

Finally. You can restrict the mode to work on a certain item. The following will allow rubber band to work on the second item (index of 1) only. Note that we set AutoItemMode to None so the mode does not change the Item property.

JavaScript Example
var mode = new lt.Controls.ImageViewerRubberBandInteractiveMode(); 
mode.autoItemMode = lt.Controls.ImageViewerAutoItemMode.none; 
mode.item = imageViewer.items.item(1); 
mode.itemPart = lt.Controls.ImageViewerItemPart.image; 
mode.workOnBounds = true; 
imageViewer.interactiveModes.add(mode); 

Run this example and notice that rubber band only works on the second item.

Combing Interactive Modes

Some interactive modes can be combined with others to provide extra functionality. For example, ImageViewerAutoPanInteractiveMode can be combined with any other mode to allow auto-panning the viewer while the other mode is running.

Change the code as follows:

JavaScript Example
// Add auto-pan, also left button 
var autoPanMode = new lt.Controls.ImageViewerAutoPanInteractiveMode(); 
autoPanMode.mouseButtons = lt.Controls.MouseButtons.left; 
imageViewer.interactiveModes.add(autoPanMode); 
// Add rubber-band, left button 
var rubberBandMode = new lt.Controls.ImageViewerRubberBandInteractiveMode(); 
rubberBandMode.mouseButtons = lt.Controls.MouseButtons.left; 
imageViewer.interactiveModes.add(rubberBandMode); 

Run the example, and notice that when drawing a rubber band and moving towards the edge of the viewer, it will pan in that direction. This is the auto-pan performing extra functionality to rubber-band.

This is accomplished because auto-pan does not set the value of InteractiveEventArgs.IsHandled to true, hence the event will propagate to the next interactive mode in the list: the rubber-band.

Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS HTML5 JavaScript
Click or drag to resize