Div scroll on mouse drag event javascript











up vote
-2
down vote

favorite













hi im trying to scroll the div with mouse drag event with canvas
selection area but unable to do it properly.That div contains an image
and im drawing a canvas selection with mouseup/move/down events on
image. wanted to select the area with drag with autoscroll of div.
Somehow ive managed to scroll the div with drag but on next selection
the div is getting scroll with wrong position.




    <!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
<div id="outerContainer"
style="width: 600px; height: 400px; overflow: scroll; border: thick; border-style: solid;">
<canvas id="myCanvas" width="1700" height="2200"></canvas>
</div>
<script>
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
var imageObj = new Image();

imageObj.onload = function() {
ctx.drawImage(imageObj, 0, 0);
};
imageObj.src = 'image4.jpg';
rect = {}, drag = false;
var xScroll;
var yScroll;
function init() {
canvas.addEventListener('mousedown', mouseDown, false);
canvas.addEventListener('mouseup', mouseUp, false);
canvas.addEventListener('mousemove', mouseMove, false);
}
function clearDraw() {
const ctx = canvas.getContext('2d');
ctx.save();
ctx.globalCompositeOperation = 'copy';
ctx.strokeStyle = 'transparent';
ctx.beginPath();
ctx.lineTo(0, 0);
ctx.stroke();
ctx.restore();
}
function clearDrawUpdated() {
const ctx = canvas.getContext('2d');
ctx.save();
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(rect.startX, rect.startY, rect.w, rect.h);
}
function mouseDown(e) {
rect = {};
yScroll = document.getElementById("outerContainer").scrollTop;
xScroll = document.getElementById("outerContainer").scrollLeft;
rect.startX = e.pageX - this.offsetLeft + xScroll;
rect.startY = e.pageY - this.offsetTop + yScroll;
console.log("down:: => e.pageX: " + e.pageX + ": this.offsetLeft: "
+ this.offsetLeft);
drag = true;
}
function mouseUp() {
document.getElementById("x1").value = rect.startX;
document.getElementById("y1").value = rect.startY;
document.getElementById("x2").value = rect.startX + rect.w;
document.getElementById("y2").value = rect.startY + rect.h;
document.getElementById("height1").value = rect.w;
document.getElementById("width1").value = rect.h;
drag = false;

if (rect.w == null) {
console.log('no area selected');
}
console.log("up: => " + rect.startX + ":" + rect.startY + ":"
+ rect.w + ":" + rect.h);

}
function mouseMove(e) {
if (drag) {
rect.w = (e.pageX - this.offsetLeft + xScroll) - rect.startX;
rect.h = (e.pageY - this.offsetTop + yScroll) - rect.startY;
ctx.drawImage(imageObj, 0, 0);
draw();
}

}
function draw() {
ctx.setLineDash([ 6 ])
ctx.strokeRect(rect.startX, rect.startY, rect.w, rect.h)
}
init();
</script>
<div style="margin-left: 780px; margin-top: -286px;">
<input name="x1" id="x1">X1</input></br> <input name="y1" id="y1">Y1</input></br>
<input name="x2" id="x2">X2</input></br> <input name="y2" id="y2">Y2</input></br>
<input name="height1" id="height1">H</input></br> <input name="width1"
id="width1">W</input>
</div>

</body>
</html>









share|improve this question




























    up vote
    -2
    down vote

    favorite













    hi im trying to scroll the div with mouse drag event with canvas
    selection area but unable to do it properly.That div contains an image
    and im drawing a canvas selection with mouseup/move/down events on
    image. wanted to select the area with drag with autoscroll of div.
    Somehow ive managed to scroll the div with drag but on next selection
    the div is getting scroll with wrong position.




        <!DOCTYPE HTML>
    <html>
    <head>
    <style>
    body {
    margin: 0px;
    padding: 0px;
    }
    </style>
    </head>
    <body>
    <div id="outerContainer"
    style="width: 600px; height: 400px; overflow: scroll; border: thick; border-style: solid;">
    <canvas id="myCanvas" width="1700" height="2200"></canvas>
    </div>
    <script>
    var canvas = document.getElementById('myCanvas');
    var ctx = canvas.getContext('2d');
    var imageObj = new Image();

    imageObj.onload = function() {
    ctx.drawImage(imageObj, 0, 0);
    };
    imageObj.src = 'image4.jpg';
    rect = {}, drag = false;
    var xScroll;
    var yScroll;
    function init() {
    canvas.addEventListener('mousedown', mouseDown, false);
    canvas.addEventListener('mouseup', mouseUp, false);
    canvas.addEventListener('mousemove', mouseMove, false);
    }
    function clearDraw() {
    const ctx = canvas.getContext('2d');
    ctx.save();
    ctx.globalCompositeOperation = 'copy';
    ctx.strokeStyle = 'transparent';
    ctx.beginPath();
    ctx.lineTo(0, 0);
    ctx.stroke();
    ctx.restore();
    }
    function clearDrawUpdated() {
    const ctx = canvas.getContext('2d');
    ctx.save();
    ctx.setTransform(1, 0, 0, 1, 0, 0);
    ctx.clearRect(rect.startX, rect.startY, rect.w, rect.h);
    }
    function mouseDown(e) {
    rect = {};
    yScroll = document.getElementById("outerContainer").scrollTop;
    xScroll = document.getElementById("outerContainer").scrollLeft;
    rect.startX = e.pageX - this.offsetLeft + xScroll;
    rect.startY = e.pageY - this.offsetTop + yScroll;
    console.log("down:: => e.pageX: " + e.pageX + ": this.offsetLeft: "
    + this.offsetLeft);
    drag = true;
    }
    function mouseUp() {
    document.getElementById("x1").value = rect.startX;
    document.getElementById("y1").value = rect.startY;
    document.getElementById("x2").value = rect.startX + rect.w;
    document.getElementById("y2").value = rect.startY + rect.h;
    document.getElementById("height1").value = rect.w;
    document.getElementById("width1").value = rect.h;
    drag = false;

    if (rect.w == null) {
    console.log('no area selected');
    }
    console.log("up: => " + rect.startX + ":" + rect.startY + ":"
    + rect.w + ":" + rect.h);

    }
    function mouseMove(e) {
    if (drag) {
    rect.w = (e.pageX - this.offsetLeft + xScroll) - rect.startX;
    rect.h = (e.pageY - this.offsetTop + yScroll) - rect.startY;
    ctx.drawImage(imageObj, 0, 0);
    draw();
    }

    }
    function draw() {
    ctx.setLineDash([ 6 ])
    ctx.strokeRect(rect.startX, rect.startY, rect.w, rect.h)
    }
    init();
    </script>
    <div style="margin-left: 780px; margin-top: -286px;">
    <input name="x1" id="x1">X1</input></br> <input name="y1" id="y1">Y1</input></br>
    <input name="x2" id="x2">X2</input></br> <input name="y2" id="y2">Y2</input></br>
    <input name="height1" id="height1">H</input></br> <input name="width1"
    id="width1">W</input>
    </div>

    </body>
    </html>









    share|improve this question


























      up vote
      -2
      down vote

      favorite









      up vote
      -2
      down vote

      favorite












      hi im trying to scroll the div with mouse drag event with canvas
      selection area but unable to do it properly.That div contains an image
      and im drawing a canvas selection with mouseup/move/down events on
      image. wanted to select the area with drag with autoscroll of div.
      Somehow ive managed to scroll the div with drag but on next selection
      the div is getting scroll with wrong position.




          <!DOCTYPE HTML>
      <html>
      <head>
      <style>
      body {
      margin: 0px;
      padding: 0px;
      }
      </style>
      </head>
      <body>
      <div id="outerContainer"
      style="width: 600px; height: 400px; overflow: scroll; border: thick; border-style: solid;">
      <canvas id="myCanvas" width="1700" height="2200"></canvas>
      </div>
      <script>
      var canvas = document.getElementById('myCanvas');
      var ctx = canvas.getContext('2d');
      var imageObj = new Image();

      imageObj.onload = function() {
      ctx.drawImage(imageObj, 0, 0);
      };
      imageObj.src = 'image4.jpg';
      rect = {}, drag = false;
      var xScroll;
      var yScroll;
      function init() {
      canvas.addEventListener('mousedown', mouseDown, false);
      canvas.addEventListener('mouseup', mouseUp, false);
      canvas.addEventListener('mousemove', mouseMove, false);
      }
      function clearDraw() {
      const ctx = canvas.getContext('2d');
      ctx.save();
      ctx.globalCompositeOperation = 'copy';
      ctx.strokeStyle = 'transparent';
      ctx.beginPath();
      ctx.lineTo(0, 0);
      ctx.stroke();
      ctx.restore();
      }
      function clearDrawUpdated() {
      const ctx = canvas.getContext('2d');
      ctx.save();
      ctx.setTransform(1, 0, 0, 1, 0, 0);
      ctx.clearRect(rect.startX, rect.startY, rect.w, rect.h);
      }
      function mouseDown(e) {
      rect = {};
      yScroll = document.getElementById("outerContainer").scrollTop;
      xScroll = document.getElementById("outerContainer").scrollLeft;
      rect.startX = e.pageX - this.offsetLeft + xScroll;
      rect.startY = e.pageY - this.offsetTop + yScroll;
      console.log("down:: => e.pageX: " + e.pageX + ": this.offsetLeft: "
      + this.offsetLeft);
      drag = true;
      }
      function mouseUp() {
      document.getElementById("x1").value = rect.startX;
      document.getElementById("y1").value = rect.startY;
      document.getElementById("x2").value = rect.startX + rect.w;
      document.getElementById("y2").value = rect.startY + rect.h;
      document.getElementById("height1").value = rect.w;
      document.getElementById("width1").value = rect.h;
      drag = false;

      if (rect.w == null) {
      console.log('no area selected');
      }
      console.log("up: => " + rect.startX + ":" + rect.startY + ":"
      + rect.w + ":" + rect.h);

      }
      function mouseMove(e) {
      if (drag) {
      rect.w = (e.pageX - this.offsetLeft + xScroll) - rect.startX;
      rect.h = (e.pageY - this.offsetTop + yScroll) - rect.startY;
      ctx.drawImage(imageObj, 0, 0);
      draw();
      }

      }
      function draw() {
      ctx.setLineDash([ 6 ])
      ctx.strokeRect(rect.startX, rect.startY, rect.w, rect.h)
      }
      init();
      </script>
      <div style="margin-left: 780px; margin-top: -286px;">
      <input name="x1" id="x1">X1</input></br> <input name="y1" id="y1">Y1</input></br>
      <input name="x2" id="x2">X2</input></br> <input name="y2" id="y2">Y2</input></br>
      <input name="height1" id="height1">H</input></br> <input name="width1"
      id="width1">W</input>
      </div>

      </body>
      </html>









      share|improve this question
















      hi im trying to scroll the div with mouse drag event with canvas
      selection area but unable to do it properly.That div contains an image
      and im drawing a canvas selection with mouseup/move/down events on
      image. wanted to select the area with drag with autoscroll of div.
      Somehow ive managed to scroll the div with drag but on next selection
      the div is getting scroll with wrong position.




          <!DOCTYPE HTML>
      <html>
      <head>
      <style>
      body {
      margin: 0px;
      padding: 0px;
      }
      </style>
      </head>
      <body>
      <div id="outerContainer"
      style="width: 600px; height: 400px; overflow: scroll; border: thick; border-style: solid;">
      <canvas id="myCanvas" width="1700" height="2200"></canvas>
      </div>
      <script>
      var canvas = document.getElementById('myCanvas');
      var ctx = canvas.getContext('2d');
      var imageObj = new Image();

      imageObj.onload = function() {
      ctx.drawImage(imageObj, 0, 0);
      };
      imageObj.src = 'image4.jpg';
      rect = {}, drag = false;
      var xScroll;
      var yScroll;
      function init() {
      canvas.addEventListener('mousedown', mouseDown, false);
      canvas.addEventListener('mouseup', mouseUp, false);
      canvas.addEventListener('mousemove', mouseMove, false);
      }
      function clearDraw() {
      const ctx = canvas.getContext('2d');
      ctx.save();
      ctx.globalCompositeOperation = 'copy';
      ctx.strokeStyle = 'transparent';
      ctx.beginPath();
      ctx.lineTo(0, 0);
      ctx.stroke();
      ctx.restore();
      }
      function clearDrawUpdated() {
      const ctx = canvas.getContext('2d');
      ctx.save();
      ctx.setTransform(1, 0, 0, 1, 0, 0);
      ctx.clearRect(rect.startX, rect.startY, rect.w, rect.h);
      }
      function mouseDown(e) {
      rect = {};
      yScroll = document.getElementById("outerContainer").scrollTop;
      xScroll = document.getElementById("outerContainer").scrollLeft;
      rect.startX = e.pageX - this.offsetLeft + xScroll;
      rect.startY = e.pageY - this.offsetTop + yScroll;
      console.log("down:: => e.pageX: " + e.pageX + ": this.offsetLeft: "
      + this.offsetLeft);
      drag = true;
      }
      function mouseUp() {
      document.getElementById("x1").value = rect.startX;
      document.getElementById("y1").value = rect.startY;
      document.getElementById("x2").value = rect.startX + rect.w;
      document.getElementById("y2").value = rect.startY + rect.h;
      document.getElementById("height1").value = rect.w;
      document.getElementById("width1").value = rect.h;
      drag = false;

      if (rect.w == null) {
      console.log('no area selected');
      }
      console.log("up: => " + rect.startX + ":" + rect.startY + ":"
      + rect.w + ":" + rect.h);

      }
      function mouseMove(e) {
      if (drag) {
      rect.w = (e.pageX - this.offsetLeft + xScroll) - rect.startX;
      rect.h = (e.pageY - this.offsetTop + yScroll) - rect.startY;
      ctx.drawImage(imageObj, 0, 0);
      draw();
      }

      }
      function draw() {
      ctx.setLineDash([ 6 ])
      ctx.strokeRect(rect.startX, rect.startY, rect.w, rect.h)
      }
      init();
      </script>
      <div style="margin-left: 780px; margin-top: -286px;">
      <input name="x1" id="x1">X1</input></br> <input name="y1" id="y1">Y1</input></br>
      <input name="x2" id="x2">X2</input></br> <input name="y2" id="y2">Y2</input></br>
      <input name="height1" id="height1">H</input></br> <input name="width1"
      id="width1">W</input>
      </div>

      </body>
      </html>






      javascript canvas mouseevent






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 8:40

























      asked Nov 11 at 23:16









      user1495298

      657




      657





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254208%2fdiv-scroll-on-mouse-drag-event-javascript%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254208%2fdiv-scroll-on-mouse-drag-event-javascript%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]