Click and drag from inside canvas element highlights text outside it in some browsers
up vote
2
down vote
favorite
I have a canvas in a page with some text after it:
<canvas id="myCanvas" width="800" height="160"></canvas>
<p>Some text after the canvas</p>
I use JavaScript to draw points/lines. This one's very simple.
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var drawing = false;
canvas.onmousedown = function(e){
if( !drawing )
{
drawing = true;
ctx.beginPath();
}
}
canvas.onmousemove = function(e){
var x = e.clientX - canvas.offsetLeft;
var y = e.clientY - canvas.offsetTop;
if( drawing )
{
ctx.lineTo(x,y);
ctx.stroke();
}
}
canvas.onmouseup = function(e){
drawing = false;
}
canvas.onmouseout = function(e){
drawing = false;
}
If I click on the canvas and hold, I can move my mouse around in the canvas and draw lines. Great! However, if I keep holding and move my mouse down off the canvas and past the <p>
text, I will end up highlighting that text. Not so great! I've noticed this doesn't happen in some browsers. Here is a list of what I've tested on:
- Firefox 37: no highlighting issue exists
- IE 9: highlighting issue exists
- IE 11: highlighting issue exists
- Chrome 41: highlighting issue exists
My question: What can be done to prevent this highlighting issue from occurring?
javascript html html5 canvas
add a comment |
up vote
2
down vote
favorite
I have a canvas in a page with some text after it:
<canvas id="myCanvas" width="800" height="160"></canvas>
<p>Some text after the canvas</p>
I use JavaScript to draw points/lines. This one's very simple.
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var drawing = false;
canvas.onmousedown = function(e){
if( !drawing )
{
drawing = true;
ctx.beginPath();
}
}
canvas.onmousemove = function(e){
var x = e.clientX - canvas.offsetLeft;
var y = e.clientY - canvas.offsetTop;
if( drawing )
{
ctx.lineTo(x,y);
ctx.stroke();
}
}
canvas.onmouseup = function(e){
drawing = false;
}
canvas.onmouseout = function(e){
drawing = false;
}
If I click on the canvas and hold, I can move my mouse around in the canvas and draw lines. Great! However, if I keep holding and move my mouse down off the canvas and past the <p>
text, I will end up highlighting that text. Not so great! I've noticed this doesn't happen in some browsers. Here is a list of what I've tested on:
- Firefox 37: no highlighting issue exists
- IE 9: highlighting issue exists
- IE 11: highlighting issue exists
- Chrome 41: highlighting issue exists
My question: What can be done to prevent this highlighting issue from occurring?
javascript html html5 canvas
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a canvas in a page with some text after it:
<canvas id="myCanvas" width="800" height="160"></canvas>
<p>Some text after the canvas</p>
I use JavaScript to draw points/lines. This one's very simple.
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var drawing = false;
canvas.onmousedown = function(e){
if( !drawing )
{
drawing = true;
ctx.beginPath();
}
}
canvas.onmousemove = function(e){
var x = e.clientX - canvas.offsetLeft;
var y = e.clientY - canvas.offsetTop;
if( drawing )
{
ctx.lineTo(x,y);
ctx.stroke();
}
}
canvas.onmouseup = function(e){
drawing = false;
}
canvas.onmouseout = function(e){
drawing = false;
}
If I click on the canvas and hold, I can move my mouse around in the canvas and draw lines. Great! However, if I keep holding and move my mouse down off the canvas and past the <p>
text, I will end up highlighting that text. Not so great! I've noticed this doesn't happen in some browsers. Here is a list of what I've tested on:
- Firefox 37: no highlighting issue exists
- IE 9: highlighting issue exists
- IE 11: highlighting issue exists
- Chrome 41: highlighting issue exists
My question: What can be done to prevent this highlighting issue from occurring?
javascript html html5 canvas
I have a canvas in a page with some text after it:
<canvas id="myCanvas" width="800" height="160"></canvas>
<p>Some text after the canvas</p>
I use JavaScript to draw points/lines. This one's very simple.
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var drawing = false;
canvas.onmousedown = function(e){
if( !drawing )
{
drawing = true;
ctx.beginPath();
}
}
canvas.onmousemove = function(e){
var x = e.clientX - canvas.offsetLeft;
var y = e.clientY - canvas.offsetTop;
if( drawing )
{
ctx.lineTo(x,y);
ctx.stroke();
}
}
canvas.onmouseup = function(e){
drawing = false;
}
canvas.onmouseout = function(e){
drawing = false;
}
If I click on the canvas and hold, I can move my mouse around in the canvas and draw lines. Great! However, if I keep holding and move my mouse down off the canvas and past the <p>
text, I will end up highlighting that text. Not so great! I've noticed this doesn't happen in some browsers. Here is a list of what I've tested on:
- Firefox 37: no highlighting issue exists
- IE 9: highlighting issue exists
- IE 11: highlighting issue exists
- Chrome 41: highlighting issue exists
My question: What can be done to prevent this highlighting issue from occurring?
javascript html html5 canvas
javascript html html5 canvas
asked Apr 28 '15 at 20:11
Muhammad Abdul-Rahim
1,5741026
1,5741026
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
You can dissable text selection onDrag:
Demo: http://jsfiddle.net/zb2soupb/
.unselectable {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
canvas.onmousemove = function(e){
// Dissable text selection
document.body.classList.add('unselectable')
var x = e.clientX - canvas.offsetLeft;
var y = e.clientY - canvas.offsetTop;
if( drawing )
{
ctx.lineTo(x,y);
ctx.stroke();
}
}
canvas.onmouseup = function(e){
drawing = false;
document.body.classList.remove('unselectable')
}
This is very close, but it looks like theclassList
property is not supported in IE9, which I need to support.
– Muhammad Abdul-Rahim
Apr 29 '15 at 12:35
Use a pollyfill or use document.body.className = "unselectable". However this is the way to do it
– Mircea
Apr 29 '15 at 14:12
Right now I've gotten it resolved in everything but IE9.document.body.className = "unselectable"
indeed adds the class, but text is still able to be selected in IE9. The jsfiddle also fails in IE9. To be extra sure, I made sure it was IE9 compat mode with IE9 standards. Previously it was IE7 standards for some reason...
– Muhammad Abdul-Rahim
Apr 29 '15 at 14:14
Here you go, In IE9 just return false on mousedown: jsfiddle.net/zb2soupb/2
– Mircea
Apr 29 '15 at 14:51
Haha, just did that a few seconds before this comment came up. Now everything is working. Feels a little hack-y, but when doesn't IE feel hack-y? :P
– Muhammad Abdul-Rahim
Apr 29 '15 at 14:51
|
show 1 more comment
up vote
0
down vote
I think the best answer is to call "e.preventDefault()" inside all your canvas mouse event handlers, e.g.:
canvas.onmousedown = function(e){
e.preventDefault(); // <-- this
if( !drawing )
{
drawing = true;
ctx.beginPath();
}
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
You can dissable text selection onDrag:
Demo: http://jsfiddle.net/zb2soupb/
.unselectable {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
canvas.onmousemove = function(e){
// Dissable text selection
document.body.classList.add('unselectable')
var x = e.clientX - canvas.offsetLeft;
var y = e.clientY - canvas.offsetTop;
if( drawing )
{
ctx.lineTo(x,y);
ctx.stroke();
}
}
canvas.onmouseup = function(e){
drawing = false;
document.body.classList.remove('unselectable')
}
This is very close, but it looks like theclassList
property is not supported in IE9, which I need to support.
– Muhammad Abdul-Rahim
Apr 29 '15 at 12:35
Use a pollyfill or use document.body.className = "unselectable". However this is the way to do it
– Mircea
Apr 29 '15 at 14:12
Right now I've gotten it resolved in everything but IE9.document.body.className = "unselectable"
indeed adds the class, but text is still able to be selected in IE9. The jsfiddle also fails in IE9. To be extra sure, I made sure it was IE9 compat mode with IE9 standards. Previously it was IE7 standards for some reason...
– Muhammad Abdul-Rahim
Apr 29 '15 at 14:14
Here you go, In IE9 just return false on mousedown: jsfiddle.net/zb2soupb/2
– Mircea
Apr 29 '15 at 14:51
Haha, just did that a few seconds before this comment came up. Now everything is working. Feels a little hack-y, but when doesn't IE feel hack-y? :P
– Muhammad Abdul-Rahim
Apr 29 '15 at 14:51
|
show 1 more comment
up vote
4
down vote
accepted
You can dissable text selection onDrag:
Demo: http://jsfiddle.net/zb2soupb/
.unselectable {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
canvas.onmousemove = function(e){
// Dissable text selection
document.body.classList.add('unselectable')
var x = e.clientX - canvas.offsetLeft;
var y = e.clientY - canvas.offsetTop;
if( drawing )
{
ctx.lineTo(x,y);
ctx.stroke();
}
}
canvas.onmouseup = function(e){
drawing = false;
document.body.classList.remove('unselectable')
}
This is very close, but it looks like theclassList
property is not supported in IE9, which I need to support.
– Muhammad Abdul-Rahim
Apr 29 '15 at 12:35
Use a pollyfill or use document.body.className = "unselectable". However this is the way to do it
– Mircea
Apr 29 '15 at 14:12
Right now I've gotten it resolved in everything but IE9.document.body.className = "unselectable"
indeed adds the class, but text is still able to be selected in IE9. The jsfiddle also fails in IE9. To be extra sure, I made sure it was IE9 compat mode with IE9 standards. Previously it was IE7 standards for some reason...
– Muhammad Abdul-Rahim
Apr 29 '15 at 14:14
Here you go, In IE9 just return false on mousedown: jsfiddle.net/zb2soupb/2
– Mircea
Apr 29 '15 at 14:51
Haha, just did that a few seconds before this comment came up. Now everything is working. Feels a little hack-y, but when doesn't IE feel hack-y? :P
– Muhammad Abdul-Rahim
Apr 29 '15 at 14:51
|
show 1 more comment
up vote
4
down vote
accepted
up vote
4
down vote
accepted
You can dissable text selection onDrag:
Demo: http://jsfiddle.net/zb2soupb/
.unselectable {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
canvas.onmousemove = function(e){
// Dissable text selection
document.body.classList.add('unselectable')
var x = e.clientX - canvas.offsetLeft;
var y = e.clientY - canvas.offsetTop;
if( drawing )
{
ctx.lineTo(x,y);
ctx.stroke();
}
}
canvas.onmouseup = function(e){
drawing = false;
document.body.classList.remove('unselectable')
}
You can dissable text selection onDrag:
Demo: http://jsfiddle.net/zb2soupb/
.unselectable {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
canvas.onmousemove = function(e){
// Dissable text selection
document.body.classList.add('unselectable')
var x = e.clientX - canvas.offsetLeft;
var y = e.clientY - canvas.offsetTop;
if( drawing )
{
ctx.lineTo(x,y);
ctx.stroke();
}
}
canvas.onmouseup = function(e){
drawing = false;
document.body.classList.remove('unselectable')
}
edited Apr 28 '15 at 20:46
answered Apr 28 '15 at 20:28
Mircea
5,312175084
5,312175084
This is very close, but it looks like theclassList
property is not supported in IE9, which I need to support.
– Muhammad Abdul-Rahim
Apr 29 '15 at 12:35
Use a pollyfill or use document.body.className = "unselectable". However this is the way to do it
– Mircea
Apr 29 '15 at 14:12
Right now I've gotten it resolved in everything but IE9.document.body.className = "unselectable"
indeed adds the class, but text is still able to be selected in IE9. The jsfiddle also fails in IE9. To be extra sure, I made sure it was IE9 compat mode with IE9 standards. Previously it was IE7 standards for some reason...
– Muhammad Abdul-Rahim
Apr 29 '15 at 14:14
Here you go, In IE9 just return false on mousedown: jsfiddle.net/zb2soupb/2
– Mircea
Apr 29 '15 at 14:51
Haha, just did that a few seconds before this comment came up. Now everything is working. Feels a little hack-y, but when doesn't IE feel hack-y? :P
– Muhammad Abdul-Rahim
Apr 29 '15 at 14:51
|
show 1 more comment
This is very close, but it looks like theclassList
property is not supported in IE9, which I need to support.
– Muhammad Abdul-Rahim
Apr 29 '15 at 12:35
Use a pollyfill or use document.body.className = "unselectable". However this is the way to do it
– Mircea
Apr 29 '15 at 14:12
Right now I've gotten it resolved in everything but IE9.document.body.className = "unselectable"
indeed adds the class, but text is still able to be selected in IE9. The jsfiddle also fails in IE9. To be extra sure, I made sure it was IE9 compat mode with IE9 standards. Previously it was IE7 standards for some reason...
– Muhammad Abdul-Rahim
Apr 29 '15 at 14:14
Here you go, In IE9 just return false on mousedown: jsfiddle.net/zb2soupb/2
– Mircea
Apr 29 '15 at 14:51
Haha, just did that a few seconds before this comment came up. Now everything is working. Feels a little hack-y, but when doesn't IE feel hack-y? :P
– Muhammad Abdul-Rahim
Apr 29 '15 at 14:51
This is very close, but it looks like the
classList
property is not supported in IE9, which I need to support.– Muhammad Abdul-Rahim
Apr 29 '15 at 12:35
This is very close, but it looks like the
classList
property is not supported in IE9, which I need to support.– Muhammad Abdul-Rahim
Apr 29 '15 at 12:35
Use a pollyfill or use document.body.className = "unselectable". However this is the way to do it
– Mircea
Apr 29 '15 at 14:12
Use a pollyfill or use document.body.className = "unselectable". However this is the way to do it
– Mircea
Apr 29 '15 at 14:12
Right now I've gotten it resolved in everything but IE9.
document.body.className = "unselectable"
indeed adds the class, but text is still able to be selected in IE9. The jsfiddle also fails in IE9. To be extra sure, I made sure it was IE9 compat mode with IE9 standards. Previously it was IE7 standards for some reason...– Muhammad Abdul-Rahim
Apr 29 '15 at 14:14
Right now I've gotten it resolved in everything but IE9.
document.body.className = "unselectable"
indeed adds the class, but text is still able to be selected in IE9. The jsfiddle also fails in IE9. To be extra sure, I made sure it was IE9 compat mode with IE9 standards. Previously it was IE7 standards for some reason...– Muhammad Abdul-Rahim
Apr 29 '15 at 14:14
Here you go, In IE9 just return false on mousedown: jsfiddle.net/zb2soupb/2
– Mircea
Apr 29 '15 at 14:51
Here you go, In IE9 just return false on mousedown: jsfiddle.net/zb2soupb/2
– Mircea
Apr 29 '15 at 14:51
Haha, just did that a few seconds before this comment came up. Now everything is working. Feels a little hack-y, but when doesn't IE feel hack-y? :P
– Muhammad Abdul-Rahim
Apr 29 '15 at 14:51
Haha, just did that a few seconds before this comment came up. Now everything is working. Feels a little hack-y, but when doesn't IE feel hack-y? :P
– Muhammad Abdul-Rahim
Apr 29 '15 at 14:51
|
show 1 more comment
up vote
0
down vote
I think the best answer is to call "e.preventDefault()" inside all your canvas mouse event handlers, e.g.:
canvas.onmousedown = function(e){
e.preventDefault(); // <-- this
if( !drawing )
{
drawing = true;
ctx.beginPath();
}
}
add a comment |
up vote
0
down vote
I think the best answer is to call "e.preventDefault()" inside all your canvas mouse event handlers, e.g.:
canvas.onmousedown = function(e){
e.preventDefault(); // <-- this
if( !drawing )
{
drawing = true;
ctx.beginPath();
}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
I think the best answer is to call "e.preventDefault()" inside all your canvas mouse event handlers, e.g.:
canvas.onmousedown = function(e){
e.preventDefault(); // <-- this
if( !drawing )
{
drawing = true;
ctx.beginPath();
}
}
I think the best answer is to call "e.preventDefault()" inside all your canvas mouse event handlers, e.g.:
canvas.onmousedown = function(e){
e.preventDefault(); // <-- this
if( !drawing )
{
drawing = true;
ctx.beginPath();
}
}
answered Nov 11 at 11:26
Tim Cooper
5,51534758
5,51534758
add a comment |
add a comment |
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%2f29928987%2fclick-and-drag-from-inside-canvas-element-highlights-text-outside-it-in-some-bro%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