Joshua Moerman
12 years ago
4 changed files with 151 additions and 23 deletions
@ -0,0 +1,66 @@ |
|||||
|
var line = { |
||||
|
starting_point: {x: 0, y: 0}, |
||||
|
end_point: {x: 0, y: 0} |
||||
|
}; |
||||
|
|
||||
|
var drawing = { |
||||
|
active: false, |
||||
|
draw_start: null, |
||||
|
draw_move: null, |
||||
|
draw_end: null, |
||||
|
draw_cancel: null |
||||
|
}; |
||||
|
|
||||
|
function down(e){ |
||||
|
line.starting_point.x = e.pageX - this.offsetLeft; |
||||
|
line.starting_point.y = e.pageY - this.offsetTop; |
||||
|
line.end_point.x = e.pageX - this.offsetLeft; |
||||
|
line.end_point.y = e.pageY - this.offsetTop; |
||||
|
drawing.active = true; |
||||
|
|
||||
|
drawing.draw_start(line); |
||||
|
} |
||||
|
|
||||
|
function up(e){ |
||||
|
drawing.active = false; |
||||
|
|
||||
|
drawing.draw_end(line); |
||||
|
} |
||||
|
|
||||
|
function move(e){ |
||||
|
if(drawing.active){ |
||||
|
line.end_point.x = e.pageX - this.offsetLeft; |
||||
|
line.end_point.y = e.pageY - this.offsetTop; |
||||
|
|
||||
|
drawing.draw_move(line); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function out(e){ |
||||
|
drawing.active = false; |
||||
|
drawing.draw_cancel(line); |
||||
|
} |
||||
|
|
||||
|
function init_drawing(element, draw_start, draw_move, draw_end, draw_cancel){ |
||||
|
drawing.draw_start = draw_start; |
||||
|
drawing.draw_move = draw_move; |
||||
|
drawing.draw_end = draw_end; |
||||
|
drawing.draw_cancel = draw_cancel; |
||||
|
|
||||
|
// Desktop clients
|
||||
|
element.addEventListener('mousedown', down, false); |
||||
|
element.addEventListener('mouseup', up, false); |
||||
|
element.addEventListener('mousemove', move, false); |
||||
|
element.addEventListener('mouseout', out, false); |
||||
|
|
||||
|
// Phones/etc clients
|
||||
|
element.addEventListener('touchstart', down, false); |
||||
|
element.addEventListener('touchend', up, false); |
||||
|
element.addEventListener('touchmove', move, false); |
||||
|
element.addEventListener('touchcancel', out, false); |
||||
|
} |
||||
|
|
||||
|
// To prevent scrolling on devices
|
||||
|
function prevent_scrolling(event){ |
||||
|
event.preventDefault(); |
||||
|
} |
@ -0,0 +1,22 @@ |
|||||
|
function createSVG(element){ |
||||
|
return document.createElementNS('http://www.w3.org/2000/svg', element); |
||||
|
} |
||||
|
|
||||
|
function addLine(line, svg_parent){ |
||||
|
var svgline = createSVG('line'); |
||||
|
svgline.setAttribute('x1', line.starting_point.x); |
||||
|
svgline.setAttribute('y1', line.starting_point.y); |
||||
|
svgline.setAttribute('x2', line.end_point.x); |
||||
|
svgline.setAttribute('y2', line.end_point.y); |
||||
|
|
||||
|
svg_parent.appendChild(svgline); |
||||
|
return svgline; |
||||
|
} |
||||
|
|
||||
|
function updateLine(svgline, abstractline){ |
||||
|
svgline.setAttribute('x1', abstractline.starting_point.x); |
||||
|
svgline.setAttribute('y1', abstractline.starting_point.y); |
||||
|
svgline.setAttribute('x2', abstractline.end_point.x); |
||||
|
svgline.setAttribute('y2', abstractline.end_point.y); |
||||
|
return svgline; |
||||
|
} |
@ -0,0 +1,15 @@ |
|||||
|
|
||||
|
function init_websocket(statusbar, open, message){ |
||||
|
websocket = new WebSocket(wsURI); |
||||
|
|
||||
|
websocket.onclose = function (evt) { statusbar.innerHTML = 'DISCONNECTED'; }; |
||||
|
websocket.onerror = function (evt) { statusbar.innerHTML = 'ERROR: ' + evt.data; }; |
||||
|
|
||||
|
websocket.onopen = function (evt) { |
||||
|
statusbar.innerHTML = 'CONNECTED'; |
||||
|
open(evt); |
||||
|
}; |
||||
|
websocket.onmessage = function (evt) { |
||||
|
message(evt); |
||||
|
}; |
||||
|
} |
Reference in new issue