Skip to content

Commit be7d735

Browse files
committed
started dnd on canvas
1 parent 061974e commit be7d735

File tree

4 files changed

+110
-27
lines changed

4 files changed

+110
-27
lines changed

public/draw.js

Lines changed: 84 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ $(function(){
1212
optionTabOpen = true;
1313
settingsTabOpen = false;
1414
setColor = '#000';
15-
defaultName = "Guest";
15+
defaultName = "Guest",
16+
draggingTool = false,
17+
dragging = false;
18+
19+
var pastDragX = 0, pastDragY = 0;
1620

1721
// Generate an unique ID
1822
var id = Math.round($.now()*Math.random());
@@ -99,41 +103,85 @@ $(function(){
99103

100104
canvas.on('mousedown',function(e){
101105
e.preventDefault();
102-
drawing = true;
103-
prev.x = e.pageX;
104-
prev.y = e.pageY;
106+
if(!draggingTool){
107+
drawing = true;
108+
prev.x = e.pageX;
109+
prev.y = e.pageY;
110+
// Hide the instructions
111+
instructions.fadeOut();
112+
} else {
113+
//we're dragging the page around
114+
dragging = true;
115+
}
116+
pastDragX = e.pageX;
117+
pastDragY = e.pageY;
105118

106-
// Hide the instructions
107-
instructions.fadeOut();
108119
});
109120

110121
doc.bind('mouseup mouseleave',function(){
111122
drawing = false;
123+
dragging = false;
112124
});
113125

114126
var lastEmit = $.now();
115127

116128
doc.on('mousemove',function(e){
117-
if($.now() - lastEmit > 10){
118-
socket.emit('mousemove',{
119-
'x': e.pageX,
120-
'y': e.pageY,
121-
'drawing': drawing,
122-
'id': id,
123-
'color': setColor
124-
});
125-
lastEmit = $.now();
126-
}
127-
128-
// Draw a line for the current user's movement, as it is
129-
// not received in the socket.on('moving') event above
130-
131-
if(drawing){
129+
if(!draggingTool){
130+
if($.now() - lastEmit > 10){
131+
socket.emit('mousemove',{
132+
'x': e.pageX,
133+
'y': e.pageY,
134+
'drawing': drawing,
135+
'id': id,
136+
'color': setColor
137+
});
138+
lastEmit = $.now();
139+
}
132140

133-
drawLine(prev.x, prev.y, e.pageX, e.pageY, setColor);
141+
// Draw a line for the current user's movement, as it is
142+
// not received in the socket.on('moving') event above
134143

135-
prev.x = e.pageX;
136-
prev.y = e.pageY;
144+
if(drawing){
145+
146+
drawLine(prev.x, prev.y, e.pageX, e.pageY, setColor);
147+
148+
prev.x = e.pageX;
149+
prev.y = e.pageY;
150+
}
151+
} else {
152+
//we're dragging!
153+
if(dragging){
154+
/*
155+
//get the current (top, left) for canvas element
156+
157+
var dX, dY;
158+
//get the difference between now and then
159+
dX = e.pageX - pastDragX;
160+
dY = e.pageY - pastDragY;
161+
162+
//we need to find out the relative x,y inside the canvas element
163+
var relX = e.pageX - curX;
164+
var relY = e.pageY - curY;
165+
166+
var cOriginX = e.pageX - relX;
167+
var cOriginY = e.pageY - relY;
168+
169+
//console.log("dX " + dX + " dY " + dY + " css curX " + curX + " curY " + curY);
170+
171+
//$('#paper').css({top: curY + dY, left: curX + dX});
172+
173+
pastDragX = e.pageX;
174+
pastDragY = e.pageY;
175+
*/
176+
var curX = parseInt($('#paper').css('left'), 10);
177+
var curY = parseInt($('#paper').css('top'), 10);
178+
var dX = e.pageX - pastDragX;
179+
var dY = e.pageY - pastDragY;
180+
$('#paper').css({top: curY + dY, left: curX + dX});
181+
// console.log(dX);
182+
pastDragX = e.pageX;
183+
pastDragY = e.pageY;
184+
}
137185
}
138186
});
139187

@@ -237,4 +285,16 @@ $(function(){
237285
$('#chatBox').val("");
238286
}
239287

288+
//enable/disable the dragging tool
289+
$('#handTool').click(function(){
290+
draggingTool = !draggingTool;
291+
if(draggingTool){
292+
$('canvas').addClass("canvas-draggable");
293+
$('#handTool').addClass('active');
294+
} else {
295+
$('canvas').removeClass("canvas-draggable");
296+
$('#handTool').removeClass('active');
297+
}
298+
});
299+
240300
});

public/hand_tool.png

6.5 KB
Loading

public/style.css

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
html{
77
background:url('bg.png') repeat #e4e4e4;
88
/* This will hide the extra canvas if the screen is small */
9-
overflow:hidden;
9+
/*overflow:hidden;*/
1010
}
1111

1212
body{
1313
font:14px/1.3 'Segoe UI',Arial, sans-serif;
1414
color:#444;
15+
position: relative;
1516
}
1617

1718
a, a:visited {
@@ -128,7 +129,7 @@ h3{
128129
}
129130

130131
#settingsPanel {
131-
position: absolute;
132+
position: fixed;
132133
right: 20px;
133134
bottom: -300px;
134135
width: 200px;
@@ -186,3 +187,24 @@ h3{
186187
#usernameInput {
187188
width: 160px;
188189
}
190+
191+
#handTool {
192+
background-image: url("hand_tool.png");
193+
width: 48px;
194+
height: 48px;
195+
cursor: pointer;
196+
}
197+
198+
.active {
199+
box-shadow: 1px 1px 10px #0C0;
200+
}
201+
202+
.canvas-draggable {
203+
cursor: move;
204+
}
205+
206+
#paper {
207+
position: absolute;
208+
left: 20px;
209+
top: 20px;
210+
}

views/main.jade

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ html
1919
li#c-black.c Black
2020
li#c-yellow.c Yellow
2121
li#c-red.c Red
22+
div#handTool
2223
div#cursors
23-
canvas#paper(width="1900" height="1000") Your browser doesn't support canvas!
24+
canvas#paper(width="900" height="900") Your browser doesn't support canvas!
2425
hgroup#instructions
2526
h1 Draw anywhere!
2627
h2 You will see everyone else who is doing the same.

0 commit comments

Comments
 (0)