[]
Document Solutions Image Viewer (DsImageViewer) is a fast JavaScript based client-side Image Viewer that runs in all major browsers.
var viewer = new DsImageViewer("#root");
new DsImageViewer(element, options?): DsImageViewer;
DsImageViewer constructor.
Required. HTML element or CSS selector.
string
| HTMLElement
Partial
<ViewerOptions
>
Optional. Viewer options.
DsImageViewer
get actualSize(): object;
Gets the actual display size of the active image, including the active zoom value.
object
height: number;
width: number;
get adaptiveNaturalSize(): object;
Gets the active image DPI adaptive natural size. This is the image size that will be used to display the image at 100%. The adaptiveNaturalSize property is used for the actual size calculations.
object
height: number;
width: number;
GcImageViewer
.adaptiveNaturalSize
get eventBus(): IEventBus;
Image viewer event bus.
viewer.eventBus.on("after-open", function(args) {
console.log("Image opened.", args);
});
viewer.open('Test.png');
get frameIndex(): number;
Gets or sets the active frame index. This is applicable for multi-frame images such as TIFF and ICO.
When setting this value, it will also be used as the initial frame index when opening a new image.
var viewer = new DsImageViewer('#root');
viewer.frameIndex = 9;
viewer.open('Test.ico');
number
get framesCount(): number;
Gets total frames count for the active image. Applicable for TIFF, ICO images.
var viewer = new DsImageViewer('#root');
viewer.onAfterOpen.register(function() {
alert("The image opened. Total number of frames: " + viewer.framesCount);
});
viewer.open('Test.png');
number
get hasImage(): boolean;
Indicates whether the viewer has opened the image.
var hasImageFlag = viewer.hasImage;
boolean
get hasRedo(): boolean;
Gets a value indicating whether the image viewer can redo changes.
if(viewer.hasRedo) {
viewer.redo();
}
boolean
get hasUndo(): boolean;
Gets a value indicating whether the image viewer can undo changes.
if(viewer.hasUndo) {
viewer.undo();
}
boolean
get imageFormat(): ImageFormatCode;
Gets a format type of the opened image.
get isAnimationStarted(): boolean;
Gets a value indicating whether the image animation has started.
// Toggle image animation:
const viewer = DsImageViewer.findControl("#root");
if(viewer.isAnimationStarted) {
viewer.stopAnimation();
} else {
viewer.startAnimation();
}
boolean
GcImageViewer
.isAnimationStarted
get language(): string;
language - A property that retrieves the standardized language key based on the provided language option.
The language key is determined by the options.language
setting.
string
Standardized language key (e.g., 'en', 'ja').
get layers(): IImageLayer[];
Image layers. Used for painting.
get naturalSize(): object;
Gets the active image natural size. The natural size is the image's width/height if drawn with nothing constraining its width/height, this is the number of CSS pixels wide the image will be.
object
height: number;
width: number;
get onAfterOpen(): EventFan<EventArgs>;
The event raised when the user changes the viewer theme.
var viewer = new DsImageViewer('#root');
viewer.onAfterOpen.register(function() {
alert("The image opened.");
});
viewer.open('Test.png');
EventFan
<EventArgs
>
get onBeforeOpen(): EventFan<BeforeOpenEventArgs>;
Occurs immediately before the image opens.
var viewer = new DsImageViewer('#root');
viewer.onBeforeOpen.register(function(args) {
alert("A new image will be opened,\n payload type(binary or URL): " + args.type +",\n payload(bytes or string): " + args.payload);
});
viewer.open('Test.png');
EventFan
<BeforeOpenEventArgs
>
get onError(): EventFan<ErrorEventArgs>;
The event indicating error.
function handleError(args) {
console.error(args);
}
var viewer = new DsImageViewer('#root');
viewer.onError.register(handleError);
viewer.open('Test.png');
EventFan
<ErrorEventArgs
>
get onImagePaint(): EventFan<ImagePaintEventArgs>;
The event raised when appearance image element changed.
EventFan
<ImagePaintEventArgs
>
get openParameters(): OpenParameters;
The Open parameters that were used to open an image.
get options(): Partial<ViewerOptions>;
Viewer options
Partial
<ViewerOptions
>
get toolbarLayout(): ImageToolbarLayout;
Defines the layout of the toolbar.
The full list of the viewer specific toolbar items:
'open', '$navigation', 'navigation-auto', '$split', 'zoom', '$fullscreen', 'save', 'about'
// Customize the toolbar layout:
viewer.toolbarLayout.viewer.default = ["open", "$zoom", "$fullscreen", "save", "print", "about"];
viewer.applyToolbarLayout();
set toolbarLayout(toolbarLayout): void;
Defines the layout of the toolbar.
The full list of the viewer specific toolbar items:
'open', '$navigation', '$split', 'zoom', '$fullscreen', 'save', 'about'
// Customize the toolbar layout:
viewer.toolbarLayout.viewer.default = ["open", "$zoom", "$fullscreen", "save", "print", "about"];
viewer.applyToolbarLayout();
void
Defines the layout of the toolbar.
The full list of the viewer specific toolbar items:
'open', '$navigation', '$split', 'zoom', '$fullscreen', 'save', 'about'
// Customize the toolbar layout:
viewer.toolbarLayout.viewer.default = ["open", "$zoom", "$fullscreen", "save", "print", "about"];
viewer.applyToolbarLayout();
get undoCount(): number;
Gets total undo levels count.
alert("Undo levels count is " + viewer.undoCount);
number
get undoIndex(): number;
Gets current undo level index.
alert("The current Undo level index is " + viewer.undoIndex);
number
get undoStorage(): UndoStorage;
Command based undo state storage.
const isUndoInProgress = viewer.undoStorage.undoInProgress;
get version(): string;
Returns the current version of the DS Image viewer.
alert("The DsImageViewer version is " + viewer.version);
string
static findControl(selector): GcImageViewer;
Gets the viewer instance using the host element or host element selector
string
| HTMLElement
var viewer = DsImageViewer.findControl("#root");
addKeyboardListener(uniqueKey, handler): void;
Add window keyboard listener.
string
void
GcImageViewer
.addKeyboardListener
addPlugin(plugin): boolean;
Add plug-in instance which is used to add new functionality to the DsImageViewer.
boolean
var viewer = new DsImageViewer("#root");
var rotationPlugin = new RotationPlugin();
viewer.addPlugin(rotationPlugin);
applyOptions(): void;
Call this method in order to apply changed options.
void
viewer.applyOptions();
applyToolbarLayout(): void;
Call this method in order to apply changes in @see:toolbarLayout.
void
viewer.toolbarLayout.viewer.default = ["open", "save"];
viewer.applyToolbarLayout();
var viewer = new DsImageViewer(document.querySelector("#viewer"));
var toolbar = viewer.toolbar;
var toolbarLayout = viewer.toolbarLayout;
toolbar.addItem({
key: 'custom-action',
icon: { type: "svg", content: '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="24" height="24" viewBox="0 0 24 24"><path style="fill: #205F78;" d="M20.25 12l-2.25 2.25 2.25 2.25-3.75 3.75-2.25-2.25-2.25 2.25-2.25-2.25-2.25 2.25-3.75-3.75 2.25-2.25-2.25-2.25 2.25-2.25-2.25-2.25 3.75-3.75 2.25 2.25 2.25-2.25 2.25 2.25 2.25-2.25 3.75 3.75-2.25 2.25 2.25 2.25z"></path></svg>' },
title: 'Custom action',
checked: false, enabled: false,
action: function () {
alert("Implement your action here.");
},
onUpdate: function (args) {
return {
enabled: true,
checked: false,
title: 'Custom action title'
}
}
});
toolbarLayout.viewer.default.splice(0, 0, "custom-action");
viewer.applyToolbarLayout();
GcImageViewer
.applyToolbarLayout
clearUndo(): void;
Clear undo storage.
void
configurePluginMainToolbar(pos, buttonsToInsert): void;
Used preliminarily by the plugin developers to configure the main toolbar layout.
The position where the buttons should be inserted. Use false
or -1
to skip insertion. Undefined means the position will be determined automatically.
number
| boolean
string
[]
An array of button keys to be inserted.
void
void
// Apply a custom layout to insert "zoomIn" and "zoomOut" buttons at position 2 for the "PaintTools" plugin.
viewer.configurePluginMainToolbar(2, ["zoomIn", "zoomOut"]);
GcImageViewer
.configurePluginMainToolbar
confirm(
confirmationText?,
level?,
title?,
buttons?): Promise<boolean | ConfirmButton>;
Display confirmation dialog.
string
| Element
"error"
| "info"
| "warning"
string
Promise
<boolean
| ConfirmButton
>
const confirmResult = await viewer.confirm("Apply changes?", "info", "Confirm action", ["Yes", "No", "Cancel"]);
if (confirmResult === "Yes") {
// put your code here
} else if (confirmResult === "No") {
// put your code here
} else {
// put your code here
}
dataUrlToImageData(dataUrl, destinationSize?): Promise<ImageData>;
Load image data using given data url.
string
number
number
Promise
<ImageData
>
GcImageViewer
.dataUrlToImageData
dispose(): void;
Use this method to close and release resources occupied by the DsImageViewer.
void
download(fileName?, original?): void;
Downloads the Image document loaded in the Viewer to the local disk.
string
the destination file name.
boolean
Flag indicating whether to use the original image for save. Defaults to false
.
void
Deprecated since v1.2.0-a6 in favor of the save
method.
ensurePaintLayer(): IImageLayer;
Create at least one image layer which will be used for painting.
GcImageViewer
.ensurePaintLayer
executeCommand(command): Promise<void>;
Execute a new command.
Instance of a CommandBase.
Promise
<void
>
findPlugin(pluginId): IViewerPlugin;
Finds a viewer plugin by its id.
string
// find imageFilters plugin:
var imageFilters = viewer.findPlugin("imageFilters");
// find pageTools plugin:
var pageTools = viewer.findPlugin("pageTools");
getEvent(eventName): EventFan<any>;
Get event object.
EventFan
<any
>
viewer.getEvent("CustomEvent").register(function(args) {
console.log(args);
});
getEventCanvasPoint(event, includeDpi?): PointLocation;
Retrieves the point location from the pointer event provided by the 'event' parameter. The returned point is relative to the active canvas element
DOM Pointer or Mouse event object.
PointerEvent
| MouseEvent
boolean
GcImageViewer
.getEventCanvasPoint
getImageDataUrl(): string;
Get current image data url.
string
getOriginalImageDataUrl(): string;
Get unmodified current image data url.
string
GcImageViewer
.getOriginalImageDataUrl
hideActivitySpinner(): void;
Hide activity spinner.
void
viewer.hideActivitySpinner();
GcImageViewer
.hideActivitySpinner
hideSecondToolbar(toolbarKey?): Promise<void>;
Hides the second toolbar. This method deactivates any active editor mode associated with the second toolbar and then hides the toolbar itself.
Optional. The key identifying the specific second toolbar to hide. If provided, only hides the specified toolbar if it exists.
Promise
<void
>
A Promise that resolves once the second toolbar is successfully hidden.
// Hide the second toolbar
viewer.hideSecondToolbar();
// Hide a specific second toolbar by passing its key
viewer.hideSecondToolbar("page-tools");
GcImageViewer
.hideSecondToolbar
newImage(options?): Promise<any>;
Create new empty image.
object
& OpenParameters
Promise
<any
>
await viewer.newImage({ width: 300, height: 300, fileName: "myImage.png" });
open(file?, openParameters?): Promise<any>;
Open Image document.
any
URL to Image document(string) or binary data(Typed array - Uint8Array).
Loading parameters object.
ImageFormatCode
| OpenParameters
| ImageFormatName
Promise
<any
>
viewer.open("Images/HelloWorld.png");
openLocalFile(): any;
Show the file open dialog where the user can select the Image file.
any
viewer.openLocalFile();
redo(): Promise<void>;
Redo changes.
Promise
<void
>
if(viewer.hasRedo) {
viewer.redo();
}
removeKeyboardListener(uniqueKey): void;
Remove window keyboard listener.
string
void
GcImageViewer
.removeKeyboardListener
removeLayer(layerOrIndex): void;
Remove and dispose image layer given by argument layerOrIndex.
Image layer or image layer index or image layer name.
string
| number
| IImageLayer
void
removeLayers(): void;
Remove and dispose all image layers.
void
removePlugin(pluginId): void;
Remove plug-in instance from the DsImageViewer.
Plugin id or plugin instance.
string
| IViewerPlugin
void
var viewer = new DsImageViewer("#root");
var rotationPlugin = new RotationPlugin();
viewer.addPlugin(rotationPlugin);
viewer.removePlugin(rotationPlugin.id);
removePlugins(): void;
Remove all plug-ins.
void
viewer.removePlugins();
resetCursor(): IImageViewer;
Resets the cursor to default style
// Reset cursor when operation completes
viewer.resetCursor();
// Reset cursor on mouse leave
viewerElement.addEventListener('mouseleave', () => {
viewer.resetCursor();
});
save(options?, original?): void;
Saves the Image document loaded in the Viewer to the local disk.
The save options, including the destination file name and other settings.
string
| SaveOptions
boolean
Flag indicating whether to use the initial version of the image for save. Defaults to false
.
void
// Example: Save the modified image without using specific options.
const viewer = DsImageViewer.findControl("#root");
viewer.save();
// Example: Save the modified image as "image.png".
const viewer = DsImageViewer.findControl("#root");
viewer.save({ fileName: "image.png" });
// Example: Download the original version of the image as "original_image.jpg".
const viewer = DsImageViewer.findControl("#root");
viewer.save({ fileName: "original_image.jpg", original: true });
// Example: Save the modified image in PNG format.
const viewer = DsImageViewer.findControl("#root");
viewer.save({ convertToFormat: "image/png" });
// Example: Save the modified image with a custom file name and in JPEG format.
const viewer = DsImageViewer.findControl("#root");
viewer.save({ fileName: "custom_name", convertToFormat: "image/jpeg" });
setCursor(cursorType): IImageViewer;
Sets the cursor style for the image viewer
The cursor style to apply
// Set rotate cursor during image rotation
viewer.setCursor('rotate');
// Set resize cursor when hovering over edges
viewer.setCursor('nwse-resize');
GlobalCursorType for all available cursor options
setImageDataUrl(dataUrl): Promise<void>;
Modify current image data url.
any
Promise
<void
>
showAbout(): void;
Show about dialog.
void
showActivitySpinner(container?): void;
Show activity spinner.
HTMLElement
void
viewer.showActivitySpinner();
GcImageViewer
.showActivitySpinner
showMessage(
message,
details?,
severity?): void;
Shows the message for the user.
string
string
"error"
| "warn"
| "info"
| "debug"
void
showSecondToolbar(toolbarKey): Promise<void>;
Displays a second toolbar specified by the toolbarKey argument.
The key identifying the specific second toolbar to show.
Promise
<void
>
// Show the page tools toolbar
viewer.showSecondToolbar("page-tools");
GcImageViewer
.showSecondToolbar
startAnimation(): void;
Start GIF animation.
void
DsImageViewer.findControl("#root").startAnimation();
stopAnimation(): void;
Stop GIF animation. *
void
toggleAnimation(): void;
Toggle GIF animation.
void
DsImageViewer.findControl("#root").toggleAnimation();
toggleCursor(cursorType): IImageViewer;
Toggles between specified cursor and default style
Cursor style to apply, or false to reset
false
| GlobalCursorType
// Toggle grab cursor during drag operations
viewer.toggleCursor(isDragging ? 'grab' : false);
// Toggle zoom cursor based on modifier key
document.addEventListener('keydown', (e) => {
if (e.ctrlKey) {
viewer.toggleCursor('zoom-in');
}
});
triggerEvent(eventName, args?): void;
Trigger event.
any
void
// Listen CustomEvent:
viewer.getEvent("CustomEvent").register(function(args) {
console.log(args);
});
// Trigger CustomEvent:
viewer.triggerEvent("CustomEvent", { arg1: 1, arg2: 2});
undo(): Promise<void>;
Undo changes.
Promise
<void
>
if(viewer.hasUndo) {
viewer.undo();
}