main.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. function start(wsrpc){
  2. // attach on windows resize
  3. var canvas = document.getElementById("screen")
  4. var ctx = canvas.getContext("2d");
  5. ctx.lineWidth = 2;
  6. function handleResize(evt){
  7. if (ctx.canvas.width != window.innerWidth ||
  8. ctx.canvas.height != window.innerHeight) {
  9. console.log("Readapt canvas");
  10. ctx.canvas.width = window.innerWidth;
  11. ctx.canvas.height = window.innerHeight;
  12. }
  13. wsrpc.call("OnResize",ctx.canvas.width,ctx.canvas.height);
  14. }
  15. window.onresize = handleResize
  16. handleResize();
  17. // Get Canvas ctx
  18. console.log("Connected");
  19. wsrpc.export({
  20. ping: function() {
  21. console.log("Client sent ping");
  22. },
  23. DrawArc: function(response, x,y,radius,startAngle,endAngle) {
  24. //console.log("Drawing arc",x,y,radius,startAngle,endAngle);
  25. x = x - canvas.offsetLeft
  26. y = y - canvas.offsetTop
  27. ctx.beginPath();
  28. ctx.arc(x,y,radius,startAngle,endAngle,false);
  29. ctx.stroke();
  30. },
  31. Line: function(response, x1,y1,x2,y2) {
  32. x1 = x1 - canvas.offsetLeft
  33. y1 = y1 - canvas.offsetTop
  34. x2 = x2 - canvas.offsetLeft
  35. y2 = y2 - canvas.offsetTop
  36. ctx.beginPath();
  37. ctx.moveTo(x1,y1);
  38. ctx.lineTo(x2,y2);
  39. ctx.stroke();
  40. },
  41. Clear: function(response) {
  42. ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height);
  43. }
  44. // Canvas stuff
  45. //
  46. })
  47. // Events
  48. window.onmousemove = function(evt) {
  49. wsrpc.call("OnMouseMove",evt.pageX,evt.pageY);
  50. }
  51. }
  52. window.onload = function() {
  53. wsrpc = new WsRpc();
  54. wsrpc.onopen = start;
  55. wsrpc.connect("ws://" + location.host + "/wsrpc");
  56. }