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?










share|improve this question


























    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?










    share|improve this question
























      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?










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 28 '15 at 20:11









      Muhammad Abdul-Rahim

      1,5741026




      1,5741026
























          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')
          }





          share|improve this answer























          • 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










          • 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


















          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();
          }
          }





          share|improve this answer





















            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%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

























            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')
            }





            share|improve this answer























            • 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










            • 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















            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')
            }





            share|improve this answer























            • 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










            • 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













            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')
            }





            share|improve this answer














            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')
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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 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










            • 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










            • 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












            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();
            }
            }





            share|improve this answer

























              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();
              }
              }





              share|improve this answer























                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();
                }
                }





                share|improve this answer












                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();
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 11:26









                Tim Cooper

                5,51534758




                5,51534758






























                    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%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





















































                    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

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python