Guest User

Untitled

a guest
Aug 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. var point = require('./point'),
  2. shape = require('./shape')
  3.  
  4. /*
  5. BoardElement Factory
  6. */
  7.  
  8. var BoardElement = function(board, element, options) {
  9. this.element = element;
  10. this.options = options;
  11. return new this[element](board, options);
  12. }
  13.  
  14. BoardElement.prototype = (function() {
  15.  
  16. /*--
  17. Interface Element {
  18. public void constructor(JSXGraph board, Object options)
  19. public object draw()
  20. }
  21. *--*/
  22.  
  23. /*
  24. Options: {
  25. center: [float, float],
  26. radius: float
  27. }
  28. */
  29. var circleElement = function(board, options) {
  30. this.options = options;
  31. this.board = board;
  32. };
  33.  
  34. circleElement.prototype.draw = function() {
  35. var p1 = new point(this.board, this.options.center).add();
  36. return new shape(this.board, "circle", [p1, this.options.radius, [p1]]).add();
  37. };
  38.  
  39. //-----------------------------------------------------------------------
  40.  
  41. /*
  42. Options: {
  43. point 1: [float, float],
  44. point 2: [float, float],
  45. point 3: [float, float]
  46. }
  47. */
  48. var angleElement = function(board, options) {
  49. this.options = options;
  50. this.board = board;
  51. };
  52.  
  53. angleElement.prototype.draw = function() {
  54. var p1 = new point(this.board, this.options.point1).add();
  55. var p2 = new point(this.board, this.options.point2).add();
  56. var p3 = new point(this.board, this.options.point3).add();
  57.  
  58. return new shape(this.board, "angle", [p1, p2, p3, [p1, p2, p3]]).add();
  59. };
  60.  
  61. //-----------------------------------------------------------------------
  62.  
  63. /*
  64. Options: {
  65. point 1: [float, float],
  66. point 2: [float, float],
  67. point 3: [float, float]
  68. }
  69. */
  70. var arcElement = function(board, options) {
  71. this.options = options;
  72. this.board = board;
  73. };
  74.  
  75. arcElement.prototype.draw = function() {
  76. var p1 = new point(this.board, this.options.point1).add();
  77. var p2 = new point(this.board, this.options.point2).add();
  78. var p3 = new point(this.board, this.options.point3).add();
  79.  
  80. return new shape(this.board, "arc", [p1, p2, p3, [p1, p2, p3]]).add();
  81. };
  82.  
  83. //-----------------------------------------------------------------------
  84.  
  85. /*
  86. Options: {
  87. point 1: [float, float],
  88. point 2: [float, float],
  89. point 3: [float, float]
  90. }
  91. */
  92. var ellipseElement = function(board, options) {
  93. this.options = options;
  94. this.board = board;
  95. };
  96.  
  97. ellipseElement.prototype.draw = function() {
  98. // curve points
  99. var p1 = new point(this.board, this.options.point1).add();
  100. var p2 = new point(this.board, this.options.point2).add();
  101. var p3 = new point(this.board, this.options.point3).add();
  102.  
  103. return new shape(this.board, "ellipse", [p1, p2, p3, [p1, p2, p3]]).add();
  104.  
  105. };
  106.  
  107. //-----------------------------------------------------------------------
  108.  
  109. /*
  110. Options: {
  111. point 1: [float, float],
  112. point 2: [float, float]
  113. }
  114. */
  115. var segmentElement = function(board, options) {
  116. this.options = options;
  117. this.board = board;
  118. };
  119.  
  120. segmentElement.prototype.draw = function() {
  121. var p1 = new point(this.board, this.options.point1).add();
  122. var p2 = new point(this.board, this.options.point2).add();
  123.  
  124. return new shape(this.board, "segment", [p1, p2, [p1, p2]]).add();
  125.  
  126. };
  127.  
  128. //-----------------------------------------------------------------------
  129.  
  130. /*
  131. Options: {
  132. point 1: [float, float],
  133. point 2: [float, float]
  134. }
  135. */
  136. var lineElement = function(board, options) {
  137. this.options = options;
  138. this.board = board;
  139. };
  140.  
  141. lineElement.prototype.draw = function() {
  142. var p1 = new point(this.board, this.options.point1).add();
  143. var p2 = new point(this.board, this.options.point2).add();
  144.  
  145. return new shape(this.board, "line", [p1, p2, [p1, p2]]).add();
  146. };
  147.  
  148. //-----------------------------------------------------------------------
  149.  
  150. /*
  151. Options: {
  152. point 1: [float, float],
  153. ...
  154. }
  155. */
  156. var polygonElement = function(board, options) {
  157. this.options = options;
  158. this.board = board;
  159. };
  160.  
  161. polygonElement.prototype.draw = function() {
  162.  
  163. var vertices = [];
  164. for(i in this.options) {
  165. vertices.push(new point(this.board, this.options[i]).add());
  166. }
  167. vertices.push(vertices);
  168. var polygon = new shape(this.board, "polygon", vertices).add();
  169. polygon.hasInnerPoints = true;
  170. return polygon;
  171. };
  172.  
  173. //-----------------------------------------------------------------------
  174.  
  175. /*
  176. Options: {
  177. point: [float, float]
  178. }
  179. */
  180. var pointElement = function(board, options) {
  181. this.options = options;
  182. this.board = board;
  183. };
  184.  
  185. pointElement.prototype.draw = function() {
  186.  
  187. return new point(this.board, this.options.point).add();
  188.  
  189. };
  190.  
  191. //-----------------------------------------------------------------------
  192.  
  193. /*
  194. Options: {
  195. options: [Function, X, Y]
  196. }
  197. */
  198.  
  199. var functionElement = function(board, options) {
  200. this.options = options;
  201. this.board = board;
  202. }
  203.  
  204. functionElement.prototype.draw = function() {
  205. var curve = new shape(this.board, 'functiongraph', this.options.concat(this.options[0]), {
  206. fixed: true,
  207. strokewidth: 2
  208. });
  209. curve = curve.add();
  210.  
  211. return curve;
  212. };
  213.  
  214. //-----------------------------------------------------------------------
  215.  
  216. /*
  217. Options: {
  218. position: [float, float],
  219. size: int,
  220. text: string
  221. }
  222. */
  223.  
  224. var textElement = function(board, options) {
  225. this.options = options;
  226. this.board = board;
  227. };
  228.  
  229. textElement.prototype.draw = function() {
  230. return new shape(this.board, "text",
  231. [this.options.position[0],
  232. this.options.position[1],
  233. this.options.text,
  234. [this.options.position[0],
  235. this.options.position[1]]],
  236. {
  237. fontSize: this.options.size
  238. }).add();
  239. };
  240.  
  241. return {
  242. Constructor: BoardElement,
  243. circle: circleElement,
  244. angle: angleElement,
  245. arc: arcElement,
  246. ellipse: ellipseElement,
  247. segment: segmentElement,
  248. func: functionElement,
  249. line: lineElement,
  250. polygon: polygonElement,
  251. point: pointElement,
  252. text: textElement
  253. };
  254.  
  255. })();
  256.  
  257. module.exports = BoardElement;
Add Comment
Please, Sign In to add comment