How to prevent text select outside HTML5 canvas on double-click?

后端 未结 3 1244
一向
一向 2021-02-14 19:16

(In every browser I\'ve tried) double-clicking on an HTML5 canvas selects any text immediately following the canvas element. I\'d prefer to keep the clicks confined to the canv

3条回答
  •  青春惊慌失措
    2021-02-14 19:33

    Firstly let me note that your canvas is not filling the width of the page, it is only 100 pixels wide. Width and height canvas attributes always parse to pixels, so writing width="100%" just means 100 pixels as far as the Canvas tag is concerned.

    To answer your question, write in javasript:

    //give your canvas an id, I used 'can'    
    var canvas = document.getElementById('can');
    canvas.onselectstart = function () { return false; }
    

    The double-click text problem will no longer occur.

提交回复
热议问题