node.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Equation Editor</title>
  6. </head>
  7. <body style="font-size:30px; font-family: Arial;">
  8. <!-- Page -->
  9. <div style="position:absolute; width:100%; height:100%; top:0px; left:0px; overflow: hidden;">
  10. <!-- Canvas -->
  11. <canvas id="canvas" style="position:absolute; width:100%; height:100%; top:0px; left:0px;"></canvas>
  12. <!-- Buttons -->
  13. <div style="position:absolute; width:60px; height:50px; top:0px; left:10px; text-align:center; z-index:10; cursor: pointer;" onclick="window.addOperatorBlock('+');">+</div>
  14. <div style="position:absolute; width:60px; height:50px; top:50px; left:10px; text-align:center; z-index:10; cursor: pointer;" onclick="window.addOperatorBlock('-');">-</div>
  15. <div style="position:absolute; width:60px; height:50px; top:100px; left:10px; text-align:center; z-index:10; cursor: pointer;" onclick="window.addOperatorBlock('*');">x</div>
  16. <div style="position:absolute; width:60px; height:50px; top:150px; left:10px; text-align:center; z-index:10; cursor: pointer;" onclick="window.addOperatorBlock('/');">/</div>
  17. <div style="position:absolute; width:60px; height:50px; top:200px; left:10px; text-align:center; z-index:10; cursor: pointer;" onclick="window.addInputBlock();">Num</div>
  18. <div style="position:absolute; width:60px; height:50px; bottom:50px; left:10px; text-align:center; z-index:10; cursor: pointer;" onclick="window.loadFile();">Load</div>
  19. <div style="position:absolute; width:60px; height:50px; bottom:0px; left:10px; text-align:center; z-index:10; cursor: pointer;" onclick="window.saveFile();">Save</div>
  20. </div>
  21. <!-- Code -->
  22. <script type="text/javascript" src="../build/escher.js"></script>
  23. <script type="text/javascript">
  24. // Canvas
  25. var canvas = document.getElementById("canvas");
  26. canvas.width = window.innerWidth;
  27. canvas.height = window.innerHeight;
  28. // Prevent context menu events
  29. document.body.oncontextmenu = function(event)
  30. {
  31. event.preventDefault();
  32. return false;
  33. };
  34. // Resize canvas on window resize
  35. window.onresize = function()
  36. {
  37. canvas.width = window.innerWidth;
  38. canvas.height = window.innerHeight;
  39. };
  40. window.addOperatorBlock = function(symbol)
  41. {
  42. graph.addNode(new OperationNode(symbol));
  43. };
  44. window.addInputBlock = function(symbol)
  45. {
  46. graph.addNode(new NumberInputNode());
  47. };
  48. window.loadFile = function()
  49. {
  50. Escher.FileUtils.select(function(files)
  51. {
  52. if(files.length > 0)
  53. {
  54. var reader = new FileReader();
  55. reader.onload = function()
  56. {
  57. var text = reader.result;
  58. var data = JSON.parse(text);
  59. graph = Escher.Object2D.parse(data);
  60. };
  61. reader.readAsText(files[0]);
  62. }
  63. }, ".json");
  64. };
  65. window.saveFile = function()
  66. {
  67. var data = graph.serialize(true);
  68. var text = JSON.stringify(data, null, "\t");
  69. Escher.FileUtils.write("object.json", text);
  70. };
  71. class OperationNode extends Escher.Node
  72. {
  73. constructor(operation)
  74. {
  75. super();
  76. this.type = "OperationNode";
  77. this.operation = operation;
  78. this.box.set(new Escher.Vector2(-50, -40), new Escher.Vector2(50, 40));
  79. this.text = new Escher.Text();
  80. this.text.text = operation;
  81. this.text.font = "25px Arial";
  82. this.text.layer = 2;
  83. this.add(this.text);
  84. }
  85. registerSockets()
  86. {
  87. this.a = this.addInput("string", "a");
  88. this.b = this.addInput("string", "b");
  89. this.r = this.addOutput("string", "r");
  90. this.r.getValue = () =>
  91. {
  92. return "(" + this.a.getValue() + this.operation + this.b.getValue() + ")";
  93. };
  94. }
  95. serialize(recursive)
  96. {
  97. var data = super.serialize(recursive);
  98. data.operation = this.operation;
  99. return data;
  100. }
  101. parse(data, root)
  102. {
  103. super.parse(data, root);
  104. this.operation = data.operation;
  105. }
  106. }
  107. Escher.Object2D.register(OperationNode, "OperationNode");
  108. class NumberInputNode extends Escher.Node
  109. {
  110. constructor()
  111. {
  112. super();
  113. this.box.set(new Escher.Vector2(-50, -30), new Escher.Vector2(50, 30));
  114. this.div = new Escher.DOM("input");
  115. this.div.size.set(70, 20);
  116. this.div.origin.set(35, 10);
  117. this.div.element.style.pointerEvents = "auto";
  118. this.div.element.type = "number";
  119. this.div.element.style.fontFamily = "Arial";
  120. this.div.element.style.textAlign = "center";
  121. this.div.element.style.border = "1px";
  122. this.div.element.style.borderStyle = "solid";
  123. this.div.element.style.borderColor = "#000000";
  124. this.div.element.style.padding = "0px";
  125. this.add(this.div);
  126. }
  127. registerSockets()
  128. {
  129. this.out = this.addOutput("string", "v");
  130. this.out.getValue = () =>
  131. {
  132. return this.div.element.value;
  133. };
  134. }
  135. }
  136. class ResultNode extends Escher.Node
  137. {
  138. constructor()
  139. {
  140. super();
  141. this.box.set(new Escher.Vector2(-100, -20), new Escher.Vector2(100, 20));
  142. this.text = new Escher.Text();
  143. this.text.text = "";
  144. this.text.font = "12px Arial";
  145. this.text.layer = 2;
  146. this.add(this.text);
  147. }
  148. registerSockets()
  149. {
  150. this.r = this.addInput("string", "r");
  151. }
  152. onUpdate()
  153. {
  154. super.onUpdate();
  155. try
  156. {
  157. var value = this.r.getValue();
  158. this.text.text = value + " = " + eval(value);
  159. }
  160. catch(e)
  161. {
  162. this.text.text = "NaN";
  163. }
  164. }
  165. }
  166. var graph = new Escher.NodeGraph();
  167. var result = new ResultNode();
  168. graph.addNode(result);
  169. // Viewport
  170. var viewport = new Escher.Viewport(canvas);
  171. // Renderer
  172. var renderer = new Escher.Renderer(canvas);
  173. // Render loop
  174. var controls = new Escher.ViewportControls(viewport);
  175. function loop()
  176. {
  177. controls.update(renderer.pointer);
  178. renderer.update(graph, viewport);
  179. requestAnimationFrame(loop);
  180. }
  181. loop();
  182. </script>
  183. </body>
  184. </html>