Image Scaling by Slider Using HTML5

Introduction

In this article we learn how to create Image Scaling using a slider in HTML5. We use the drawImage method of HTML5 and a Slider to provide scaling functionality to an image.

drawImage Method

The drawImage method is for using an image on a canvas and can also draw parts of an image, reduce or increase its size. It takes up to nine parameters depending on what you want to do with the image. The image object can be an image, a video or another canvas element.

drawImage(image, dx, dy)
drawImage(image, dx, dy, dw, dh)
drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh)

Step 1 - First we define the body of the HTML using a canvas element and declare the name of JavaScript, like this:

  1. <body>  
  2. <div><h1>Scaling Using HTML5</h1></div>  
  3. <div id='controls'>  
  4. <output id='scaleOutput'>1.0 </output>  
  5.  <input id='slider_value' type='range'  
  6. min='1' max='5.0' step='0.01' value='1.0'/>  <!-- value is default slider value. Min and Max are slider range-->  
  7. </div>  
  8. <canvas id='mycanvas' width='1500' height='750'>  
  9. Sorry,Canvas is not supported in this Browser..  
  10. </canvas>  
  11. <script src='ImageScaling_script.js'></script>  
Step 2 - Then we define the functions in JavaScript and use the parseFloat() function to parse a string and return the floating point number and toFixed() method to convert a number into a string, keeping a specified number of decimals. MIN_SCALE and Max_Scale are use to set the slider min and max values.

 

  1. var canvas = document.getElementById('mycanvas'),  
  2. context = canvas.getContext('2d'),  
  3. image = new Image(),  
  4. slider_value = document.getElementById('slider_value'),  
  5. defaultval = 1.0,  
  6. MIN_SCALE = 1.0,  
  7. MAX_SCALE = 5.0;  
  8. function drawScaleText(value) {  
  9.     var text = parseFloat(value).toFixed(2);              
  10.     var percent = parseFloat(value - MIN_SCALE) /parseFloat(MAX_SCALE - MIN_SCALE);  
  11.     scaleOutput.innerText = text;  
  12.     percent = percent < 0.35 ? 0.35 : percent;  
  13.     scaleOutput.style.fontSize = percent * MAX_SCALE / 1.5 + 'em';  
  14. }  
  15. function CreateImage() {  
  16.     var w = canvas.width,  
  17.     h = canvas.height,  
  18.     sw = w * defaultval,  
  19.     sh = h * defaultval;  
  20.     context.clearRect(0, 0, canvas.width, canvas.height);  
  21.     context.drawImage(image, -sw / 2 + w / 2, -sh / 2 + h / 2, sw, sh);  
  22. }  
Step 3 - Now we define the Event Handler and Initialization in JavaScript, like this:
  1. // Event handlers   
  2. slider_value.onchange = function(e) {  
  3. defaultval = e.target.value;  
  4. if (defaultval < MIN_SCALE) scale = MIN_SCALE;  
  5. else if (defaultval > MAX_SCALE) scale = MAX_SCALE;  
  6. drawScaleText(defaultval);  
  7. CreateImage();  
  8. };  
  9.   
  10. // Initialization  
  11. context.fillStyle = 'cornflowerblue';  
  12. context.strokeStyle = 'Black';  
  13. context.shadowColor = 'Yellow';  
  14. context.shadowOffsetX = 5;  
  15. context.shadowOffsetY = 5;  
  16. context.shadowBlur = 20;  
  17. image.src = 'country.jpg';  
  18. image.onload = function(e) {  
  19. CreateImage();  
  20. drawScaleText(slider_value.value);  
  21. };  
Step 4 - We use CSS in the Head section for describing the the look and formatting to our HTML page, like this:
  1. <head>  
  2.     <title>Image Scaling using slider in HTML5</title>  
  3.     <style>  
  4. body {  
  5. background: rgba(2003055000.3);  
  6. }  
  7. #controls {  
  8. margin-left15px;  
  9. padding0;  
  10. }  
  11. #slider_value {  
  12. vertical-align10px;  
  13. width200px;  
  14. margin-left90px;  
  15. }  
  16. #scaleOutput {  
  17. positionabsolute;  
  18. width60px;  
  19. height30px;  
  20. margin-left10px;  
  21. vertical-aligncenter;  
  22. text-aligncenter;  
  23. color: Black;  
  24. font20px Verdana;  
  25. }  
  26. #mycanvas {  
  27. margin10px 20px 0px 20px;  
  28. borderthin solid Black;  
  29. cursorcrosshair;  
  30. }  
  31. </style>  
  32. </head>
Output

ImageScaling1.jpg

Image after changing the value of the slider:

ImageScaling2.jpg

 

Next Recommended Readings