canvas.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. ;/*
  2. (define script-page ::String "
  3. ; // */
  4. var scaleX = 1.5;
  5. var scaleY = 1.5;
  6. var displaceX = 500;
  7. var displaceY = 500;
  8. var bgColor = null;
  9. var lineColor = null;
  10. var lineWidth = null;
  11. var pageId = null;
  12. function drawLine(lineObject) {
  13. var x1 = lineObject.x1 * scaleX + displaceX;
  14. var y1 = -lineObject.y1 * scaleY + displaceY;
  15. var x2 = lineObject.x2 * scaleX + displaceX;
  16. var y2 = -lineObject.y2 * scaleY + displaceY;
  17. var c = document.getElementById('LineCanvas');
  18. var ctx = c.getContext('2d');
  19. ctx.beginPath();
  20. ctx.strokeStyle = lineColor;
  21. ctx.lineWidth = lineWidth;
  22. ctx.moveTo(x1,y1);
  23. ctx.lineTo(x2,y2);
  24. ctx.stroke();
  25. }
  26. function redrawTurtle(redrawObject) {
  27. var x = redrawObject.x * scaleX + displaceX;
  28. var y = -redrawObject.y * scaleY + displaceY;
  29. var c=document.getElementById('TurtleCanvas');
  30. var ctx=c.getContext('2d');
  31. ctx.clearRect(0, 0, c.width, c.height);
  32. ctx.beginPath();
  33. var theta1 = redrawObject.facing / 180 * Math.PI;
  34. var theta2 = theta1 + 2.5;
  35. var theta3 = theta1 - 2.5;
  36. ctx.beginPath();
  37. ctx.moveTo(Math.cos(theta1)*12+x, Math.sin(-theta1)*12+y);
  38. ctx.lineTo(Math.cos(theta2)*12+x, Math.sin(-theta2)*12+y);
  39. ctx.lineTo(Math.cos(theta3)*12+x, Math.sin(-theta3)*12+y);
  40. ctx.closePath();
  41. ctx.lineWidth = lineWidth;
  42. ctx.strokeStyle = lineColor;
  43. ctx.stroke();
  44. ctx.fillStyle = bgColor;
  45. ctx.fill();
  46. }
  47. function deleteTurtle() {
  48. var c=document.getElementById('TurtleCanvas');
  49. var ctx=c.getContext('2d');
  50. ctx.clearRect(0, 0, c.width, c.height);
  51. }
  52. function drawTurtleLine(lineObject) {
  53. var dy = lineObject.y2 - lineObject.y1;
  54. var dx = lineObject.x2 - lineObject.x1;
  55. var facingAmount = 180 * Math.atan(dy/dx) / Math.PI;
  56. if (lineObject.x1 > lineObject.x2) {
  57. facingAmount = facingAmount + 180;
  58. } else if (facingAmount < 0) {
  59. facingAmount = facingAmount + 360;
  60. }
  61. if (lineObject.shown) {
  62. redrawTurtle({
  63. x: lineObject.x2,
  64. y: lineObject.y2,
  65. facing: facingAmount
  66. });
  67. } else {
  68. deleteTurtle();
  69. }
  70. if (lineObject.penDown) {
  71. drawLine(lineObject);
  72. }
  73. }
  74. function clearAll() {
  75. var c=document.getElementById('LineCanvas');
  76. var ctx=c.getContext('2d');
  77. ctx.clearRect(0, 0, c.width, c.height);
  78. }
  79. function setBgColor(bgColorObject) {
  80. var c=document.getElementById('BgCanvas');
  81. var ctx=c.getContext('2d');
  82. ctx.fillStyle = bgColorObject.color;
  83. ctx.fillRect(0, 0, c.width, c.height);
  84. bgColor = bgColorObject.color;
  85. }
  86. function setLineColor(lineColorObject) {
  87. lineColor = lineColorObject.color;
  88. }
  89. function setLineWidth(lineWidthObject) {
  90. lineWidth = lineWidthObject.width;
  91. }
  92. function turtleDo(instruction) {
  93. if (instruction.type === 'line') {
  94. drawLine(instruction);
  95. } else if (instruction.type === 'turtleLine') {
  96. drawTurtleLine(instruction);
  97. } else if (instruction.type === 'rotate') {
  98. redrawTurtle(instruction);
  99. } else if (instruction.type === 'clear') {
  100. clearAll();
  101. } else if (instruction.type === 'bgColor') {
  102. setBgColor(instruction);
  103. } else if (instruction.type === 'lineColor') {
  104. setLineColor(instruction);
  105. } else if (instruction.type === 'lineWidth') {
  106. setLineWidth(instruction);
  107. } else if (instruction.type === 'hideTurtle') {
  108. deleteTurtle();
  109. } else if (instruction.type === 'showTurtle') {
  110. redrawTurtle(instruction);
  111. } else {
  112. console.error('unknown: ', instruction);
  113. }
  114. }
  115. setBgColor({color: 'black'});
  116. setLineColor({color: 'white'});
  117. setLineWidth({width: 1.0});
  118. setTimeout(function() {
  119. $.post('/api/NewId', '', function(response) {
  120. pageId = response;
  121. });
  122. }, 250);
  123. setInterval(function() {
  124. if (pageId != null) {
  125. $.post('/api/PullBuffer', String(pageId), function(response) {
  126. var json = JSON.parse(response);
  127. if (json == 'CLEAR') {
  128. clearAll()
  129. } else {
  130. for (var i = 0; i < json.length; i += 1) {
  131. turtleDo(json[i]);
  132. }
  133. }
  134. }, 'text');
  135. }
  136. }, 250);
  137. " ;; "
  138. ;/*
  139. )
  140. ;*/