addFromCanvas Method

Summary

Adds a new item from an HTML5 Canvas Element.

Syntax
TypeScript
JavaScript
ImageViewerItems.prototype.addFromCanvas = function( 
   canvas, 
   resolution 
) 
addFromCanvas( 
   canvas: HTMLCanvasElement, 
   resolution: LeadSizeD 
): ImageViewerItem; 

Parameters

canvas

Source HTML5 Canvas Element.

resolution

The resolution to use for the image in dots/inch (DPI). A value of 0,0 or Empty means to use the default image resolution of 96 pixels.

Return Value

The newly created item, if successful.

Remarks

This method allows you to quickly add an item from an HTML5 Canvas Element. It is the equivalent of the following code:

var item = new lt.Controls.ImageViewerItem(); 
item.canvas = canvas; 
item.resolution = resolution; // Optional 
imageViewer.items.add(item); 

AddFromElement can be used as a shortcut for AddFromCanvas or AddFromImage. Passing a valid HTML5 Canvas Element to AddFromElement will create an ImageViewerItem with the Canvas property set, just as it does with AddFromCanvas. Similarly, passing a valid HTML Image Element to AddFromElement will create an ImageViewerItem with the Image property set, just as it does with AddFromImage. The item can have a value in only one of Image, Element, or Canvas. Setting one value will automatically delete the values of the others.

To add the ImageViewerItem at a specific location, see InsertFromCanvas or InsertFromElement. To create an item without adding it to any ImageViewer, refer to CreateFromCanvas or CreateFromElement.

If canvas is null, then an empty item is added.

Example

This example creates an HTMLCanvasElement, draws in it, and adds it to the viewer.

AddFromCanvas.ts
ImageViewer.ts
AddFromCanvas.js
ImageViewer.js
AddFromCanvas.html
import { ImageViewer_Example } from "../ImageViewer"; 
 
export class ImageViewerItems_AddFromCanvasExample { 
   private viewerExample; 
   constructor() { 
      this.viewerExample = new ImageViewer_Example(this.run); 
   } 
 
   private run = (viewer: lt.Controls.ImageViewer) => { 
      // Set up margin and padding and layout 
      viewer.viewLayout = new lt.Controls.ImageViewerVerticalViewLayout(); 
      viewer.itemBorderThickness = 5; 
      viewer.itemPadding = lt.Controls.ControlPadding.createAll(20); 
      viewer.itemMargin = lt.Controls.ControlPadding.createAll(10); 
 
      const canvas = document.createElement("canvas") as HTMLCanvasElement; 
      canvas.width = 150; 
      canvas.height = 150; 
      const context: CanvasRenderingContext2D = canvas.getContext("2d"); 
      context.fillStyle = "rgba(0, 0, 255, .1)"; 
      context.fillRect(0, 0, 150, 150); 
      context.fillStyle = "rgba(0, 0, 255, .5)"; 
      context.fillRect(25, 25, 100, 100); 
 
      viewer.items.addFromCanvas(canvas, lt.LeadSizeD.create(96, 96)); 
   } 
} 
export class ImageViewer_Example { 
   // LEADTOOLS ImageViewer to be used with this example 
   protected imageViewer: lt.Controls.ImageViewer = null; 
   // Generic state value used by the examples 
   public timesClicked: number = 0; 
 
   constructor(callback?: (viewer: lt.Controls.ImageViewer) => void) { 
      // Set the LEADTOOLS license. Replace this with your actual license file 
      lt.RasterSupport.setLicenseUri("https://demo.leadtools.com/licenses/v200/LEADTOOLSEVAL.txt", "EVAL", null); 
 
      // Create an image viewer inside the imageViewerDiv element 
      const imageViewerDiv = document.getElementById("imageViewerDiv"); 
      const createOptions: lt.Controls.ImageViewerCreateOptions = new lt.Controls.ImageViewerCreateOptions(imageViewerDiv); 
      this.imageViewer = new lt.Controls.ImageViewer(createOptions); 
      this.imageViewer.viewVerticalAlignment = lt.Controls.ControlAlignment.center; 
      this.imageViewer.viewHorizontalAlignment = lt.Controls.ControlAlignment.center; 
      this.imageViewer.autoCreateCanvas = true; 
 
      // Add Pan/Zoom interactive mode 
      // Click and drag to pan, CTRL-Click and drag to zoom in and out 
      this.imageViewer.interactiveModes.add(new lt.Controls.ImageViewerPanZoomInteractiveMode()); 
 
      // Load an image 
      this.imageViewer.imageUrl = "https://demo.leadtools.com/images/jpeg/cannon.jpg"; 
 
      this.imageViewer.zoom(lt.Controls.ControlSizeMode.fit, .9, this.imageViewer.defaultZoomOrigin); 
 
      const exampleButton = document.getElementById("exampleButton"); 
      exampleButton.addEventListener("click", () => { 
         this.timesClicked++; 
         // Run the example 
         if (callback) 
            callback(this.imageViewer); 
      }); 
   } 
} 
import { ImageViewer_Example } from "../ImageViewer"; 
 
export class ImageViewerItems_AddFromCanvasExample { 
   viewerExample; 
   constructor() { 
      this.viewerExample = new ImageViewer_Example(this.run); 
   } 
 
   run = (viewer) => { 
      // Set up margin and padding and layout 
      viewer.viewLayout = new lt.Controls.ImageViewerVerticalViewLayout(); 
      viewer.itemBorderThickness = 5; 
      viewer.itemPadding = lt.Controls.ControlPadding.createAll(20); 
      viewer.itemMargin = lt.Controls.ControlPadding.createAll(10); 
 
      const canvas = document.createElement("canvas"); 
      canvas.width = 150; 
      canvas.height = 150; 
      const context = canvas.getContext("2d"); 
      context.fillStyle = "rgba(0, 0, 255, .1)"; 
      context.fillRect(0, 0, 150, 150); 
      context.fillStyle = "rgba(0, 0, 255, .5)"; 
      context.fillRect(25, 25, 100, 100); 
 
      viewer.items.addFromCanvas(canvas, lt.LeadSizeD.create(96, 96)); 
   } 
} 
export class ImageViewer_Example { 
   // LEADTOOLS ImageViewer to be used with this example 
   imageViewer = null; 
   // Generic state value used by the examples 
   timesClicked = 0; 
 
   constructor(callback) { 
      // Set the LEADTOOLS license. Replace this with your actual license file 
      lt.RasterSupport.setLicenseUri("https://demo.leadtools.com/licenses/v200/LEADTOOLSEVAL.txt", "EVAL", null); 
 
      // Create an image viewer inside the imageViewerDiv element 
      const imageViewerDiv = document.getElementById("imageViewerDiv"); 
      const createOptions = new lt.Controls.ImageViewerCreateOptions(imageViewerDiv); 
      this.imageViewer = new lt.Controls.ImageViewer(createOptions); 
      this.imageViewer.viewVerticalAlignment = lt.Controls.ControlAlignment.center; 
      this.imageViewer.viewHorizontalAlignment = lt.Controls.ControlAlignment.center; 
      this.imageViewer.autoCreateCanvas = true; 
 
      // Add Pan/Zoom interactive mode 
      // Click and drag to pan, CTRL-Click and drag to zoom in and out 
      this.imageViewer.interactiveModes.add(new lt.Controls.ImageViewerPanZoomInteractiveMode()); 
 
      // Load an image 
      this.imageViewer.imageUrl = "https://demo.leadtools.com/images/jpeg/cannon.jpg"; 
 
      this.imageViewer.zoom(lt.Controls.ControlSizeMode.fit, .9, this.imageViewer.defaultZoomOrigin); 
 
      const exampleButton = document.getElementById("exampleButton"); 
      exampleButton.addEventListener("click", () => { 
         this.timesClicked++; 
         // Run the example 
         if (callback) 
            callback(this.imageViewer); 
      }); 
   } 
} 
<!doctype html> 
<html lang="en"> 
<title>Controls Example | AddFromCanvas</title> 
 
<head> 
   <script src="https://code.jquery.com/jquery-2.2.4.min.js" 
      integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> 
 
   <script src="../../LT/Leadtools.js"></script> 
   <script src="../../LT/Leadtools.Controls.js"></script> 
   <script src="../../LT/Leadtools.Annotations.Engine.js"></script> 
   <script src="../../LT/Leadtools.Annotations.Designers.js"></script> 
   <script src="../../LT/Leadtools.Annotations.Rendering.Javascript.js"></script> 
   <script src="../../LT/Leadtools.Annotations.Automation.js"></script> 
   <script src="../../LT/Leadtools.ImageProcessing.Main.js"></script> 
   <script src="../../LT/Leadtools.ImageProcessing.Color.js"></script> 
   <script src="../../LT/Leadtools.ImageProcessing.Core.js"></script> 
   <script src="../../LT/Leadtools.ImageProcessing.Effects.js"></script> 
   <script src="../../LT/Leadtools.Document.js"></script> 
   <script src="../../LT/Leadtools.Document.Viewer.js"></script> 
   <style> 
      body { 
         font-family: 'Segoe UI', sans-serif; 
      } 
 
      #imageViewerDiv { 
         border: 1px solid #888; 
         width: 500px; 
         height: 500px; 
         background-color: #eee; 
      } 
   </style> 
 
   <!-- All demo files are bundled and appended to the window --> 
   <script src="../../bundle.js" type="text/javascript"></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> 
      <button type="button" id="exampleButton">Run Example</button> 
   </div> 
   <div id="imageViewerDiv"></div> 
   <div id="output"></div> 
</body> 
 
<script> 
   window.onload = () => { 
      const example = new window.examples.ImageViewerItems.AddFromCanvas(); 
 
   }; 
</script> 
</html> 

Requirements

Target Platforms

Help Version 20.0.2020.4.3
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Controls Assembly