-
Notifications
You must be signed in to change notification settings - Fork 0
/
draggableCanvas.js
124 lines (110 loc) · 4.31 KB
/
draggableCanvas.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* @param {} -
* @returns {} -
*/
var DraggableCanvas = Object.create({}, {
'extend': {
value: draggableCanvas
}
});
function draggableCanvas(canvas, options) {
// Validate that the canvas parameter is indeed an existing canvas element
if (canvas.nodeName !== 'CANVAS') {
console.log('ERROR: The element provided is not a canvas element.');
return;
}
// Track cursor position
var cursor = { x: undefined, y: undefined },
mouseDown = false,
dragKeyDown = false,
alwaysDraggable = false,
autoDrag = true,
onDropCallback;
// Define the canvas object interface
var properties = {
onDrag: {
value: function (options, callback) {
var dragKeyCode;
if (typeof options === 'function') {
callback = options;
dragKeyCode = 16;
} else if (typeof options === 'object') {
if (options.alwaysDraggable) {
alwaysDraggable = true;
dragKeyDown = true;
canvas.style.cursor = 'move';
}
if (typeof options.autoDrag === 'boolean') {
autoDrag = options.autoDrag;
}
}
document.addEventListener('keydown', function (event) {
if (event.keyCode === dragKeyCode) {
var marginTop = parseInt(canvas.style.top) || 0,
marginLeft = parseInt(canvas.style.left) || 0;
cursor.x = event.clientX - marginLeft;
cursor.y = event.clientY - marginTop;
canvas.style.cursor = 'move';
dragKeyDown = true;
}
});
document.addEventListener('keyup', function (event) {
if (event.keyCode === dragKeyCode) {
canvas.style.cursor = 'auto';
dragKeyDown = false;
}
});
canvas.addEventListener('mousedown', function (event) {
var marginTop = parseInt(canvas.style.top) || 0,
marginLeft = parseInt(canvas.style.left) || 0;
if (dragKeyDown) {
canvas.style.cursor = 'move';
}
cursor.x = event.clientX - marginLeft;
cursor.y = event.clientY - marginTop;
mouseDown = true;
});
document.addEventListener('mousemove', function (event) {
var currentX = event.clientX,
currentY = event.clientY,
top = Math.floor(currentY - cursor.y),
left = Math.floor(currentX - cursor.x);
if (dragKeyDown && mouseDown) {
if (autoDrag) {
canvas.style.top = top + 'px';
canvas.style.left = left + 'px';
}
callback(top, left, currentX, currentY);
}
});
document.addEventListener('mouseup', function (event) {
mouseDown = false;
if (!alwaysDraggable) {
canvas.style.cursor = 'auto';
}
if (typeof onDropCallback === 'function') {
onDropCallback(event.clientX, event.clientY);
}
});
}
},
onDrop: {
value: function (callback) {
onDropCallback = callback;
}
},
setSize: {
writable: true,
value: function (newWidth, newHeight) {
canvas.width = newWidth || window.innerWidth;
canvas.height = newHeight || window.innerHeight;
}
}
}
Object.defineProperties(canvas, properties);
var extendedCanvas = Object.create({}, properties);
if (typeof options === 'object' && options.autoDrag) {
extendedCanvas.onDrag(options, function () {});
}
return extendedCanvas;
}