Creating Image Magnifier Using HTML5

Introduction

 
In this article, we use a canvas of HTML5 to create an Image Magnifier. The mouse is used to select an area to magnify. First, we define the body by using a canvas element of HTML5 then add any image which we want to magnify then create JavaScript to add magnifying functionality then apply CSS to add style.
 
Step 1  First we create a canvas element in the body and define its id, width and height and two div maindiv and controls. In the controls div we add button to Reset Picture. We also define the name of our JavaScript file in the body, like this:
  1. <body>  
  2. <div id='maindiv'></div>  
  3. <canvas id='canvas' width='1500' height='820'>  
  4.         Sorry, Canvas is not supported in this Browser..  
  5. </canvas>  
  6. <div id='controls'>  
  7. <input type='button' id='Clear' value='Reset Picture'/>  
  8. </div>  
  9. <script src='ScriptFile.js'></script>  
  10. </body> 
Step 2 Now we define JavaScript for our HTML page. We use a getElementById method to get the id's of the canvas, maindiv and Clear. The Canvas.getContext('2d') method is used to return an object that provides methods and properties for drawing and image manipulations on the canvas element.
 
In the maindivstart function we use a and b to define the coordinates of the mouse cursor and set dragging to true and use the mouse position to create an area to magnify, like this:
  1. var canvas = document.getElementById('canvas'),  
  2.     context = canvas.getContext('2d'),  
  3.     maindiv = document.getElementById('maindiv'),  
  4.     Clear = document.getElementById('Clear'),  
  5.     image = new Image(),  
  6.     cursor = {},  
  7.     divr = {},  
  8.     dragging = false;  
  9. function maindivstart(a, b) {  
  10.     cursor.a = a;  
  11.     cursor.b = b;  
  12.     divr.left = cursor.a;  
  13.     divr.top = cursor.b;  
  14.     maindivposition();  
  15.     maindivvisible();  
  16.     dragging = true;  
  17. }  
  18. function maindivsize(a, b) {  
  19.     divr.left = a < cursor.a ? a : cursor.a;  
  20.     divr.top = b < cursor.b ? b : cursor.b;  
  21.     divr.width = Math.abs(a - cursor.a),  
  22.    divr.height = Math.abs(b - cursor.b);  
  23.     maindivposition();  
  24.     maindivresize();  
  25. }  
  26. function maindivend() {  
  27.     var bbox = canvas.getBoundingClientRect();  
  28.     try {  
  29.         context.drawImage(canvas,divr.left - bbox.left,divr.top - bbox.top,divr.width,divr.height,00, canvas.width, canvas.height);  
  30.     }  
  31.     catch (e) {  
  32.     }  
  33.     cleardivr();  
  34.     maindiv.style.width = 0;  
  35.     maindiv.style.height = 0;  
  36.     maindivinvisible();  
  37.     dragging = false;  
  38. }  
  39. function maindivposition() {  
  40.     maindiv.style.top = divr.top + 'px';  
  41.     maindiv.style.left = divr.left + 'px';  
  42. }  
  43. function maindivresize() {  
  44.     maindiv.style.width = divr.width + 'px';  
  45.     maindiv.style.height = divr.height + 'px';  
  46. }  
  47. function maindivvisible() {  
  48.     maindiv.style.display = 'inline';  
  49. }  
  50. function maindivinvisible() {  
  51.     maindiv.style.display = 'none';  
  52. }  
  53. function cleardivr() {  
  54.     divr = { top: 0, left: 0width0height0 };  
Step 3 Now we define event handlers in our JavaScript by using mouse positions and define a reset image on button click then at the end we initialize the picture, like this:
  1. canvas.onmousedown = function (e) {  
  2.     var a = e.clientX,  
  3.        b = e.clientY;  
  4.     e.preventDefault();  
  5.     maindivstart(a, b);  
  6. };  
  7. window.onmousemove = function (e) {  
  8.     var a = e.clientX,  
  9.        b = e.clientY;  
  10.     e.preventDefault();  
  11.     if (dragging) {  
  12.         maindivsize(a, b);  
  13.     }  
  14. };  
  15. window.onmouseup = function (e) {  
  16.     e.preventDefault();  
  17.     maindivend();  
  18. };  
  19. image.onload = function () {  
  20.     context.drawImage(image, 0, 0, canvas.width, canvas.height);  
  21. };  
  22. Clear.onclick = function (e) {  
  23.     context.clearRect(0, 0, context.canvas.width,context.canvas.height);  
  24.     context.drawImage(image, 0, 0, canvas.width, canvas.height);  
  25. };  
  26. image.src = 'forest.jpg'
Step 4: We use CSS in the Head section for describing the look and formatting to our HTML page, like this:
  1. </head>  
  2. <style>  
  3. body  
  4. {  
  5.      background: Green;  
  6. }  
  7. #maindiv  
  8. {  
  9.     position: absolute;  
  10.     border: 3px solid Black;  
  11.     cursor: crosshair;  
  12.     display: none;  
  13. }  
  14. #controls  
  15. {  
  16.     margin: 20px 0px 20px 20px;  
  17. }  
  18. #canvas  
  19. {  
  20.       margin-left: 20px;  
  21.       margin-right: 0;  
  22.       margin-bottom: 20px;  
  23.       border: thin solid #aaaaaa;  
  24.       cursor: crosshair;  
  25.       padding: 0;  
  26. }  
  27. </style>  
  28. </head> 
Output  
 
Now we Select the area to magnify by mouse.
 
img_magnifier2.jpg
 
The selected area is magnified.
 
img_magnifier3.jpg