Not Able to drag and drop the text in canvas image
<p>Actually i have tried to drag and drop the canvas text which was created in javascript . But I am not able to drag it properly because selecting the text is not working properly. I kept my code in below snippet.</p>

<p>please help me on this. Thanks in advance.</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

// variables used to get mouse position on the canvas
var $canvas = $("#canvas");
var canvasOffset = $canvas.offset();
var offsetX = canvasOffset.left;
var offsetY = canvasOffset.top;
var scrollX = $canvas.scrollLeft();
var scrollY = $canvas.scrollTop();

// variables to save last mouse position
// used to see how far the user dragged the mouse
// and then move the text by that distance
var startX;
var startY;

// an array to hold text objects
var texts = [];

// this var will hold the index of the hit-selected text
var selectedText = -1;

// clear the canvas &amp; redraw all texts
function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.drawImage(window.imageSrc, 0, 0, canvas.width, canvas.height);
    for (var i = 0; i &lt; texts.length; i++) {
        var text = texts[i];
  ctx.strokeText(text.text, text.x, text.y);
        ctx.fillText(text.text, text.x, text.y);
    }
}

// test if x,y is inside the bounding box of texts[textIndex]
function textHittest(x, y, textIndex) {
    var text = texts[textIndex];
    return (x &gt;= text.x &amp;&amp; x &lt;= text.x + text.width &amp;&amp; y &gt;= text.y - text.height &amp;&amp; y &lt;= text.y);
}

// handle mousedown events
// iterate through texts[] and see if the user
// mousedown'ed on one of them
// If yes, set the selectedText to the index of that text
function handleMouseDown(e) {
    e.preventDefault();
    startX = parseInt(e.clientX - offsetX);
    startY = parseInt(e.clientY - offsetY);
    // Put your mousedown stuff here
    //alert(texts.length);
 
 
    for (var i = 0; i &lt; texts.length; i++) {
        if (textHittest(startX, startY, i)) {
            selectedText = i;
        }
    }
}

// done dragging
function handleMouseUp(e) {
    e.preventDefault();
    selectedText = -1;
}

// also done dragging
function handleMouseOut(e) {
    e.preventDefault();
    selectedText = -1;
}

// handle mousemove events
// calc how far the mouse has been dragged since
// the last mousemove event and move the selected text
// by that distance
function handleMouseMove(e) {
    if (selectedText &lt; 0) {
        return;
    }
    e.preventDefault();
    mouseX = parseInt(e.clientX - offsetX);
    mouseY = parseInt(e.clientY - offsetY);

    // Put your mousemove stuff here
    var dx = mouseX - startX;
    var dy = mouseY - startY;
    startX = mouseX;
    startY = mouseY;

    var text = texts[selectedText];
    text.x += dx;
    text.y += dy;
    draw();
}

// listen for mouse events
$("#canvas").mousedown(function (e) {
    handleMouseDown(e);
});
$("#canvas").mousemove(function (e) {
    handleMouseMove(e);
});
$("#canvas").mouseup(function (e) {
    handleMouseUp(e);
});
$("#canvas").mouseout(function (e) {
    handleMouseOut(e);
});

$("[id^='memeText']").change(function () {
    // calc the y coordinate for this text on the canvas
    var y = texts.length * 20 + 100;
  var position = $(this).attr('name');
    
    position = position - 1;
    // get the text from the input element
    var text = {
        text: $(this).val(),
        x: 250,
        y: y
    };

    // calc the size of this text for hit-testing purposes
    ctx.font = "32px verdana";
    ctx.textAlign = 'center';
 ctx.lineWidth = 3;
 ctx.fillStyle = "#fff";
 ctx.strokeStyle = "#000";
    text.width = ctx.measureText(text.text).width;
    text.height = 16;
  if(texts[position]==""||texts[position]==null||texts[position]==undefined){
     texts.push(text);
    } else {
     console.log(texts[position].text);
     texts[position].text = $(this).val();
    }
    // put this new text in the texts array
    

    // redraw everything
    draw();

});

function handleFileSelect(evt) {
      //var canvasWidth = 500;
     // var canvasHeight = 500;
      var file = evt.target.files[0];
      var reader = new FileReader();
      reader.onload = function(fileObject) {
       var data = fileObject.target.result;
       var image = new Image();
       image.onload = function() {
        window.imageSrc = this;
        ctx.drawImage(window.imageSrc, 0, 0, canvas.width, canvas.height);

       };
       document.getElementById('box2').style.display = "block";
       image.src = data;
       // console.log(fileObject.target.result);
      };
      reader.readAsDataURL(file);
     }
          
          document.getElementById('file').addEventListener('change',
       handleFileSelect, false);</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
&lt;div id="box1" style=""&gt;
       
        &lt;input id="file" type="file" autocomplete="off"&gt;
        &lt;br&gt;&lt;br&gt;
    &lt;/div&gt;
    
    &lt;div id="box2" style="display: none;"&gt;
        
        &lt;input id="memeText" name="1" autocomplete="off"&gt;
        &lt;br&gt;&lt;br&gt;
        &lt;input id="memeText" name="2" autocomplete="off"&gt;
        &lt;br&gt;&lt;br&gt;
        &lt;canvas id="canvas" width="500" height="500"&gt;&lt;/canvas&gt;
        &lt;br&gt;&lt;br&gt;
        &lt;a class="buttonLink" id="downloadLink"&gt;Download Meme!&lt;/a&gt;
        &lt;br&gt;&lt;br&gt;
        &lt;a href=""&gt;Make Another Meme!&lt;/a&gt;
    &lt;/div&gt;</code></pre>
</div>
</div>
</p>