Set defaultCursor in fabric.js more than once

I can set canvas.defaultCursor, but I can't change it to something else once it's already set. Is this possible?

Here's my code:

var canvas = new fabric.Canvas('c');

function setCursor(cursorType) {
    if (cursorType == 'move') {
        canvas.defaultCursor = 'move';
    } else if (cursorType == 'crosshair') {
        console.log('Changing cursor to crosshair'); // this works
        canvas.defaultCursor = 'crosshair'; // this doesn't work
    }
}

setCursor('move'); // this works

// ...

setCursor('crosshair');

Answers


I do not see any problem with that changing. Look the cursor in this fiddle:

http://jsfiddle.net/v1nmtz02/4/

or try the snippet here:

  var canvas = new fabric.Canvas('canvas');
function changeCursor(cursorType) {
   canvas.defaultCursor = cursorType;
}
<script src="http://fabricjs.com/lib/fabric.js"></script>
<button onclick="changeCursor('crosshair');">crosshair</button>
<button onclick="changeCursor('move');">move</button>
<button onclick="changeCursor('default');">default</button>
<canvas id='canvas' width="550" height="550" style="border:#000 1px solid;"></canvas>

It turns out that my sample code was incomplete.

In the full code, I was changing the cursor when the user moved the mouse near an existing canvas object, and then changing it back to the default when the mouse moved away.

Here's the missing piece: when the user hovers over an object, Fabric uses hoverCursor instead of defaultCursor. In my case, hoverCursor was the same as defaultCursor. So my code did indeed change defaultCursor when it was supposed to, but the user still saw the original cursor (from hoverCursor) when moving the mouse near the object.

I was able to solve this by changing hoverCursor on the object in question.


Need Your Help

XPath concat navigation with more then one element

xml xpath concat

OK, thanks to Choroba, for the helping hand. I have another question, when i have more then one head element, like this

Directed Graph Representation in HTML

javascript java json graph treeview

I have stored a graph in a Java data structure which maps a node to a node list. The graph is more likely a tree structure. I want to represent the graph in a HTML page like a tree view. I can conv...