Disabling right click context menu on a HTML Canvas?

前端 未结 6 1744
醉梦人生
醉梦人生 2021-02-05 00:45

Making a painting app using HTML5 and Canvas.

I think I want to have a similar system to applications like Paint and Photoshop where you can have a primary and secondary

6条回答
  •  梦毁少年i
    2021-02-05 01:15

    You can use this:

    $('img').bind('contextmenu', function(e){
        return false;
    }); 
    

    See this working example!

    With the lastest jQuery:

    $('body').on('contextmenu', 'img', function(e){ return false; });
    

    Note: You should use something narrower than body if possible!

    Or without jQuery, applying on canvas:

    canvas.oncontextmenu = function(e) { e.preventDefault(); e.stopPropagation(); }
    

    EDITED

    Updated the Fiddle Example to show the contextmenu being limited to the canvas and not the image.

    JQUERY

    $('body').on('contextmenu', '#myCanvas', function(e){ return false; });
    

    HTML EXAMPLE

    
      Your browser does not support the canvas element.
    
    
    bubu
    

提交回复
热议问题