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>
javascript canvas mouseevent
add a comment |
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>
javascript canvas mouseevent
add a comment |
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>
javascript canvas mouseevent
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
javascript canvas mouseevent
edited Nov 19 at 8:40
asked Nov 11 at 23:16
user1495298
657
657
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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