-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabricjs_viewport_grabnmove.js
59 lines (58 loc) · 1.77 KB
/
fabricjs_viewport_grabnmove.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
window.Workspace = window.fabric && (function(fabric){
// see below
var fastCanvas = fabric.fastCanvas(fabric.CanvasWithViewport);
function Workspace(el){
this.el = $(el);
this.scene = new fastCanvas(el);
}
var mouseDown = false;
var spacePressed = false;
var lastEv;
Workspace.prototype.enableGrabbing = function(){
var that = this;
$(document)
.on('mousemove.spaceGrab', function(ev){
lastEv = ev;
})
.on('mousedown.spaceGrab', function(ev){
mouseDown = true;
})
.on('mouseup.spaceGrab', function(ev){
mouseDown = false;
})
.on('keydown.spaceGrab', _.throttle(function(ev){
if(!spacePressed){
spacePressed = ev.keyCode===32 && mouseDown;
if(spacePressed){
that.scene.selection =!spacePressed;
that.scene.isGrabMode = spacePressed;
that.scene._setCursor('grabbing');
that.scene.__onMouseUp(lastEv);
that.scene._onMouseDownInGrabMode(lastEv);
that.scene.renderTop();
}
}
if(spacePressed) ev.preventDefault();
},5))
.on('keyup.spaceGrab', _.throttle(function(){
if(spacePressed){
spacePressed = false;
that.scene._setCursor('auto');
that.scene.isGrabMode = spacePressed;
that.scene.selection = !spacePressed;
that.scene.__onMouseUp(lastEv);
that.scene._onMouseUpInGrabMode(lastEv);
}
},5));
};
Workspace.prototype.disableGrabbing = function(){
$(document)
.off('mousemove.spaceGrab')
.off('mousedown.spaceGrab')
.off('mouseup.spaceGrab')
.off('keydown.spaceGrab')
.off('keyup.spaceGrab');
};
console.log('rrrr')
return Workspace;
})(window.fabric);