Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to disable the context menu on a canvas element?

I'm using jqPlot to create a graph inside a div tag. jqPlot used the canvas element to render the graph. I'm trying to add some interactivity to the graph, i.e. zooming, where the user can zoom in and out using the using the left and right mouse-click, respectively. I want to disable the context menu when the user right-clicks on the graph. I've tried several approaches, including...

Adding the the oncontextmenu attribute to the parent div containing the graph canvas:

<div id="myGraph" style="width: 908px; height: 600px; " class="jqplot-target" align="center" oncontextmenu="return false"></div>

Dynamically applying the oncontextmenu param to all the children after jqPlot renders the graph:

//Code to render the graph
//...
$("#myGraph").children().attr("oncontextmenu", "return false");

Using jQuery's approach instead of regular JS:

$("#myGraph").children().bind("contextmenu",function(e){
        return false;
    });

And using a custom plug-in from this great site and applying it's disable function as follows:

$("#plot_testEventHistogram").children().disableContextMenu();

But none of these approaches seem to work, I still get a context menu when I right-click on the graph. It's not a showstopper, but is very annoying and would be nice if it can be effectively disabled

like image 405
kingrichard2005 Avatar asked Aug 25 '11 20:08

kingrichard2005


People also ask

How do I disable right click on canvas?

You can use this: $('img'). bind('contextmenu', function(e){ return false; });

How do I disable right click in react?

To override the default browser right-click menu, the first step is to prevent the default right-click behavior. This can be done by listening to the contextmenu event and using the preventDefault() method on the event.

What is getContext in canvas?

getContext() method returns a drawing context on the canvas, or null if the context identifier is not supported, or the canvas has already been set to a different context mode.


1 Answers

canvas.oncontextmenu = function() {
    return false;
}

http://jsfiddle.net/9ZWv2/

Works in chrome

like image 142
HyderA Avatar answered Nov 15 '22 05:11

HyderA