HTML5 Canvas Slider Control Without Using Range Input

Introduction

 
In this article, I will describe the implementation and use of Slider Control without the use of HTML5 Range Input.
 

Slider Bar in HTML5

 
A slider is also known as a Track Bar. Slider Control is a useful control for user input having a fixed range. It allows us to render a slider whose position represents a value in a range you specify. Generally, a Slider Control is used for user input.
 
A slider consists of a knob and a bar for the knob to slide on. The leftmost side of the bar represents a minimum value, the rightmost represents the maximum value. The user can slide the bar and choose the value they like.
 
It is different from a Scrollbar. A Scrollbar is used to adjust a value without changing the format of the display or the other information on the screen.
 
With the range input element, we can create sliding controls but here in this article, we will create a sliding bar without range input.
What does a Slider Control look like
 
The figure given below represents a Slider Control
 
sliderimg.jpg
 
Browser Support
 
The Slider Control is supported only by Google Chrome and Safari. It is not supported by Internet Explorer and Mozilla Firefox.
Procedure for building the Slider Control
 
Step 1
 
We first define an HTML5 canvas element. The height and width attributes set the canvas size.
 
<canvas id="can" height="200" width="300"></canvas>
 
Step 2
 
In the following step, we add the styles. In that, we use a class selector to specify a style for a group of elements. Since the class selector is most often used on several elements, this allows you to set a particular style for many HTML elements with the same class.
 
In this example we have used a class selector (.slider,.bar,.knob) inside the style tag to style a set of elements to act as a slider.
  1. <style>  
  2.    canvas {  
  3.        position: absolute;  
  4.        top: 10px;  
  5.        left: 10px;  
  6.        background-color: green;  
  7.        border-radius: 25px;  
  8.        border: 3px solid black;  
  9.    }  
  10.    
  11.    .slider {  
  12.        position: absolute;  
  13.        top: 110px;  
  14.        left: 85px;  
  15.        width: 150px;  
  16.        height: 50px;  
  17.     }  
  18.    
  19.     .bar {  
  20.         position: relative;  
  21.         top: 30px;  
  22.         width: 150px;  
  23.         height: 2px;  
  24.         background-color: black;  
  25.      }  
  26.    
  27.      .knob {  
  28.          position: relative;  
  29.          left: 0;  
  30.          border: 1px solid black;  
  31.          background-color: blue;  
  32.          width: 50px;  
  33.          height: 50px;  
  34.          border-radius: 20px;  
  35.      }  
  36. </style> 
Step 3
 
In order to interact with this canvas through JavaScript, we will need to first get the element by Id and then create a context.
  1. <script type="text/javascript">  
  2.     var can = document.getElementById('can');  
  3.     var ctx = can.getContext("2d");  
  4. </script> 
Step 4
 
In the script tag, we declare some variables and methods.
 
We first define the textInit() method for describing the text properties (such as text alignment, font, textbaseline,etc.); see:
  1. function textInit()  
  2. {  
  3.    ctx.fillStyle = "blue";  
  4.    ctx.font = "20pt Ariel";  
  5.    ctx.textAlign = "center";  
  6.    ctx.textBaseline = "bottom";  
  7.    
Step 5
 
In the following step, we will create a "showVal()" function. In this function, the value on the slider goes from 0 to the slider width minus the knob width
  1. function showVal()  
  2. {  
  3.    var sliderVal = knob.offsetLeft;  
  4.    ctx.save();  
  5.    ctx.clearRect(0, 0, can.width, can.height);  
  6.    var scale = .25 + sliderVal / 200;  
  7.    ctx.scale(scale, scale);  
  8.    ctx.drawImage(image, 0, 0);  
  9.    ctx.restore();  
  10.    ctx.fillText(sliderVal, can.width / 2, can.height - 5);  
  11.    setTimeout(showVal, 25);  
Step 6
 
In the "init()" method we first get the element by Id for the slider, knob and canvas separately and then create a context. At last in this method, we will call both the textInit() method and showVal() method. We assign a zero value to the mouseIsDown variable.
  1. function init()  
  2. {  
  3.     slider = document.getElementById("slider");  
  4.     knob = document.getElementById("knob");  
  5.     image = document.getElementById("image");  
  6.     can = document.getElementById("can");  
  7.     ctx = can.getContext("2d");  
  8.     mouseIsDown = 0;  
  9.     knobMid = knob.offsetWidth / 2;  
  10.     margin = can.offsetLeft - 1;  
  11.     textInit();  
  12.     showVal();  
Step 7
 
In the following step, we create the "mouseXY(ev)" method. In this method, we check whether the "if(mouseIsDown)" condition is true or not.
  1. function mouseXY(ev)  
  2. {  
  3.   if (mouseIsDown)  
  4.    {  
  5.      if (!ev)  
  6.          var ev = event;  
  7.      var mouseX = ev.pageX - slider.offsetLeft;  
  8.          if (mouseX >= 0 && mouseX <= slider.offsetWidth)  
  9.           {  
  10.              setKnob(mouseX);  
  11.           }  
  12.      }  
Step 8
 
In the following step, we create the "touchXY(ev)" method. In this method, we begin touch event handlers with the "preventDefault()" method to allow the finger to drag the slider instead of scrolling the page.
  1. function touchXY(ev)  
  2. {  
  3.    if (!ev)  
  4.       var ev = event;  
  5.    // slide, don't scroll  
  6.    ev.preventDefault();  
  7.    var touchX = ev.touches[0].pageX - slider.offsetLeft;  
  8.    if (touchX >= 0 && touchX <= slider.offsetWidth)  
  9.     {  
  10.       setKnob(touchX);  
  11.     }  
Step 9
 
In the following step, we will create a "setKnob()" method. The knob value of the mouse or touch event's pageX property, minus the slider's offsetLeft property. The knob should be positioned half its width to the left of the knob value, so the mouse or finger drags the center of the knob. The knob value should be clamped at 0 and the bar width minus the knob width.
  1. function setKnob(x)  
  2. {  
  3.    var knobX = x - knobMid;  
  4.    knobX = Math.max(knobX, 0);  
  5.    knobX = Math.min(knobX, slider.offsetWidth - knob.offsetWidth);  
  6.    knob.style.left = knobX;  
  7. }  
Step 10
 
In the following step, we create the "mouseup()" method on the body element and track the mouse button state.
  1. function mouseUp()  
  2. {  
  3.    mouseIsDown = 0;  
  4.    
Step 11
 
In the following step we create "mouseDown()" method. In this method we assign 1 to the mouseIsDown varibale and then call the mouseXY() method.
  1. function mouseDown()  
  2. {  
  3.    mouseIsDown = 1;  
  4.    mouseXY();  
Step 12 
 
In the following step, the body tag uses three nested div elements. The outermost div element is the slider and is positioned absolutely. The two inner div elements are the bar and the knob and are positioned relatively within the slider. The bar div is styled into a horizontal line and the knob div is styled into a circle using CSS.
  1. <body onload="init()" onmouseup="mouseUp()">  
  2.     <canvas id="can" height="200" width="300"></canvas>  
  3.     <div class="slider" id="slider"  
  4.         onmousedown="mouseDown()" onmousemove="mouseXY()"  
  5.         ontouchstart="touchXY()" ontouchmove="touchXY()">  
  6.         <div class="bar"></div>  
  7.         <div id="knob" class="knob"></div>  
  8.         <img id="image" style="display: none" />  
  9.     </div>  
  10. </body> 
Example
  1. <!DOCTYPE html>  
  2.    
  3. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <title>Slider in HTML5</title>  
  7.     <meta name="view" content="width=400" />  
  8.     <style>  
  9.         canvas {  
  10.             position: absolute;  
  11.             top: 10px;  
  12.             left: 10px;  
  13.             background-color: green;  
  14.             border-radius: 25px;  
  15.             border: 3px solid black;  
  16.         }  
  17.    
  18.         .slider {  
  19.             position: absolute;  
  20.             top: 110px;  
  21.             left: 85px;  
  22.             width: 150px;  
  23.             height: 50px;  
  24.         }  
  25.    
  26.         .bar {  
  27.             position: relative;  
  28.             top: 30px;  
  29.             width: 150px;  
  30.             height: 2px;  
  31.             background-color: black;  
  32.         }  
  33.    
  34.         .knob {  
  35.             position: relative;  
  36.             left: 0;  
  37.             border: 1px solid black;  
  38.             background-color: blue;  
  39.             width: 50px;  
  40.             height: 50px;  
  41.             border-radius: 20px;  
  42.         }  
  43.     </style>  
  44.     <script type="text/javascript">  
  45.         var can, ctx, image, slider,  
  46.             knob, mouseIsDown, knobMid;  
  47.    
  48.         function init() {  
  49.             slider = document.getElementById("slider");  
  50.             knob = document.getElementById("knob");  
  51.             can = document.getElementById("can");  
  52.             ctx = can.getContext("2d");  
  53.             mouseIsDown = 0;  
  54.             knobMid = knob.offsetWidth / 2;  
  55.             margin = can.offsetLeft - 1;  
  56.             textInit();  
  57.             showVal();  
  58.         }  
  59.    
  60.         function textInit() {  
  61.             ctx.fillStyle = "blue";  
  62.             ctx.font = "20pt Ariel";  
  63.             ctx.textAlign = "center";  
  64.             ctx.textBaseline = "bottom";  
  65.         }  
  66.    
  67.         function showVal() {  
  68.             var sliderVal = knob.offsetLeft;  
  69.             ctx.save();  
  70.             ctx.clearRect(0, 0, can.width, can.height);  
  71.             var scale = .25 + sliderVal / 200;  
  72.             ctx.scale(scale, scale);  
  73.             ctx.drawImage(image, 0, 0);  
  74.             ctx.restore();  
  75.             ctx.fillText(sliderVal, can.width / 2, can.height - 5);  
  76.             setTimeout(showVal, 25);  
  77.         }  
  78.    
  79.         function mouseDown() {  
  80.             mouseIsDown = 1;  
  81.             mouseXY();  
  82.         }  
  83.   
  84.         function mouseUp() {  
  85.             mouseIsDown = 0;  
  86.         }  
  87.    
  88.         function mouseXY(ev) {  
  89.             if (mouseIsDown) {  
  90.                 if (!ev)  
  91.                     var ev = event;  
  92.                 var mouseX = ev.pageX - slider.offsetLeft;  
  93.                 if (mouseX >= 0 && mouseX <= slider.offsetWidth) {  
  94.                     setKnob(mouseX);  
  95.                 }  
  96.             }  
  97.         }  
  98.    
  99.         function touchXY(ev) {  
  100.             if (!ev)  
  101.                 var ev = event;  
  102.             // slide, don't scroll  
  103.             ev.preventDefault();  
  104.             var touchX = ev.touches[0].pageX - slider.offsetLeft;  
  105.             if (touchX >= 0 && touchX <= slider.offsetWidth) {  
  106.                 setKnob(touchX);  
  107.             }  
  108.         }  
  109.    
  110.         function setKnob(x) {  
  111.             var knobX = x - knobMid;  
  112.             knobX = Math.max(knobX, 0);  
  113.             knobX = Math.min(knobX, slider.offsetWidth - knob.offsetWidth);  
  114.             knob.style.left = knobX;  
  115.         }  
  116.     </script>  
  117. </head>  
  118. <body onload="init()" onmouseup="mouseUp()">  
  119.     <canvas id="can" height="200" width="300"></canvas>  
  120.     <div class="slider" id="slider"  
  121.         onmousedown="mouseDown()" onmousemove="mouseXY()"  
  122.         ontouchstart="touchXY()" ontouchmove="touchXY()">  
  123.         <div class="bar"></div>  
  124.         <div id="knob" class="knob"></div>  
  125.         <img id="image" style="display: none" />  
  126.     </div>  
  127. </body>  
  128. </html> 
Output
 
Figure 1
 
When the Knob is at beginning(zero)
 
slider1.jpg
 
Figure 2 When we move the knob with the mouse
 
slider2.jpg
 
Figure 3 When the knob is positioned at the end
 
slider3.jpg


Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles