You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
9.6 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //subway.js
  2. // Author: Cameron Davidson-Pilon
  3. // Date: July, 2012
  4. // License: MIT
  5. // Dependencies:
  6. // Paper.js (http://paperjs.org/)
  7. // These change the subway node design. Try manipulating them to see what they do.
  8. // Note: all measurments are in pixels.
  9. var NODE_SIZE = 16; //radius of the nodes
  10. var INNER_NODE_SIZE = 7; //radius of the inner circle in the stations
  11. var CUR_NODE_SIZE = 9; // radious of the inner cicle, if is is currently selected.
  12. var LINE_WIDTH = 23; //width of the inter-station lines.
  13. // fonts
  14. var FONT_SIZE = 16;
  15. var TEXT_FONT = 'Arial';
  16. var FONT_COLOR = 'black';
  17. var CHARACTER_STYLE = {
  18. fillColor: FONT_COLOR,
  19. fontSize: FONT_SIZE,
  20. font: TEXT_FONT,
  21. };
  22. var nodes = []; // after initilization, this variable will contain all stations. Needed for clicking stations with mouse.
  23. var shiftDown = 0; //This is used in controlling the location with the keys. It is a boolean representing whether the
  24. // shift key is currently pressed down.
  25. function Line( color, name ){
  26. // the Line object creates a subway line.
  27. // color: must be a paper.js RgbColor or a keyword color, eg: 'black', 'green'.
  28. // name: the unique name of the line.
  29. this['color'] = color;
  30. this['path'] = new Path();
  31. this['path'].strokeWidth = LINE_WIDTH;
  32. this['path'].strokeColor = this['color'];
  33. this['name'] = name;
  34. }
  35. function SingleNode( line, position, neighbours, name, url, name_offset, name_angle){
  36. // the Node object is a non transitionay subway stop
  37. // line: the line the node lies on.
  38. // position: the coordinates on the canvas as Point class object.
  39. // neighbours: the neighbours of the node. It
  40. // must be the syntax {'E':Neighbour1, 'W':Neighbour2, etc.}. Note: this way we can also do things like:
  41. // {'W': Neighbour1, 'S': Neighbour1, 'E': Neighbour2}. Note all the directions need to be defined.
  42. // name: the station name to be displayed.
  43. // url: the url to go to if the SpaceBar is pressed. Leave as '' if not wanted.
  44. // name_offset: the amount to move the display named away from the center of the station. It must be a paper.js Point
  45. // object. ie. new Point( 10, 10) will position the top left corner of the text 10 pixels right and
  46. // 10 pixels below the center of the station.
  47. // name_angle: the angle the name to be at. Enter it in counterclockwise radians.
  48. this['line'] = line;
  49. this['position'] = position;
  50. this['name'] = name;
  51. this['url'] = url;
  52. this['name_angle'] = name_angle;
  53. this['neighbours'] = neighbours;
  54. nodes.push(this)
  55. //Methods
  56. this.onkeydownNode = onkeydownNode; //this controls the movement if keys are pressed.
  57. this.fillNode = function(){
  58. //this method fires when the user enters the node.
  59. this.ic.fillColor = 'black';
  60. };
  61. this.emptyNode = function(){
  62. //this method fires when the user enters the node.
  63. this.ic.fillColor = 'white';
  64. };
  65. // generate the visual shapes.
  66. var c = new Path.Circle( this['position'], NODE_SIZE);
  67. c.fillColor = this['line']['color'];
  68. var ic = new Path.Circle( this['position'], INNER_NODE_SIZE+2);
  69. ic.fillColor = 'black';
  70. this.ic = new Path.Circle( this['position'], INNER_NODE_SIZE);
  71. this.ic.fillColor = 'white';
  72. this['text'] = new PointText( this['position'] + name_offset );
  73. this['text'].content = this['name'];
  74. this['text'].characterStyle = CHARACTER_STYLE;
  75. this['text'].rotate( -this['name_angle'], this['position'] );
  76. // add it to the line.
  77. this['line']['path'].add( this['position'] );
  78. }
  79. function TransitionNode( line, line2, angle, position, neighbours, name, url,name_offset, name_angle){
  80. // the Node object is a non transitionay subway stop
  81. // line: the primary line the node lies on.
  82. // line2: the secondary line the node lies on.
  83. // angle: the angle of the above and underneath node. Equal to the angle of the two lines.
  84. // position: the coordinates on the canvas as Point class object.
  85. // neighbours: the neighbours of the node. It
  86. // must be the syntax {'E':Neighbour1, 'W':Neighbour2, etc.}. Note: this way we can also do things like:
  87. // {'W': Neighbour1, 'S': Neighbour1, 'E': Neighbour2}. Note all the directions need to be defined.
  88. // name: the station name to be displayed.
  89. // url: the url to go to if the SpaceBar is pressed. Leave as '' if not wanted.
  90. // name_offset: the amount to move the display named away from the center of the station. It must be a paper.js Point
  91. // object. ie. new Point( 10, 10) will position the top left corner of the text 10 pixels right and
  92. // 10 pixels below the center of the station.
  93. // name_angle: the angle the name to be at. Enter it in counterclockwise radians.
  94. var STROKE_WIDTH = 2; //this two control the offset and appearance of the 'underneath node'
  95. var CIRCLE_DIST = 16;
  96. //This Node is the class for line transition stations.
  97. this['line'] = line;
  98. this['line2'] = line2;
  99. this['position'] = position; //This is the position of either of the circles. The angle determines the position of the other one.
  100. this['neighbours'] = neighbours;
  101. this['angle'] = angle; //This is the angle between the two circles.
  102. this['positionAlt'] = this['position'] + new Point( CIRCLE_DIST*Math.cos( angle), CIRCLE_DIST*Math.sin( angle) );
  103. this['name'] = name;
  104. this['url'] = url;
  105. nodes.push(this);
  106. //Methods;
  107. this.onkeydownNode = onkeydownNode;
  108. this.emptyNode = function(){
  109. this.innerc.fillColor = this['path']['color'];
  110. }
  111. this.fillNode = function(){
  112. this.innerc.fillColor = 'black';
  113. }
  114. // Title to display.
  115. this['text'] = new PointText( this['position'] + name_offset );
  116. this['text'].content = this['name'];
  117. this['text'].characterStyle = CHARACTER_STYLE;
  118. this['text'].rotate( -name_angle, this.position);
  119. //generate visual shapes
  120. var c2 = new Path.Circle( this.positionAlt , NODE_SIZE);
  121. c2.strokeWidth = STROKE_WIDTH;
  122. c2.strokeColor = 'black';
  123. c2.fillColor = 'white';
  124. var c2 = new Path.Circle( this.positionAlt, CUR_NODE_SIZE+1);
  125. c2.fillColor = this.line2.color;
  126. var c1 = new Path.Circle( this.position, NODE_SIZE);
  127. c1.strokeWidth = STROKE_WIDTH;
  128. c1.fillColor = 'white';
  129. c1.strokeColor = 'black';
  130. this.innerc = new Path.Circle( this.position, CUR_NODE_SIZE);
  131. this.innerc.fillColor = this.line.color;
  132. //add it the the lines.
  133. this['line'].path.add( this['position']);
  134. this['line2'].path.add(this['positionAlt']);
  135. }
  136. function LegendItem( start, end, subwayLine ){
  137. this['line'] = new Line(subwayLine['color'], subwayLine['name']);
  138. this['line']['path'].add( start, end)
  139. var title = new PointText( end + new Point( 7,5) );
  140. title.content = subwayLine.name;
  141. title.characterStyle = {
  142. fillColor: 'black',
  143. fontSize: 14,
  144. font: TEXT_FONT
  145. };
  146. }
  147. function onKeyDown(event){
  148. var key = event.key;
  149. if ((key=="up" && shiftDown) || (key=="w" ) ){
  150. current_position.onkeydownNode('N');}
  151. else if ((key=="left" && shiftDown) || (key=="a" ) ){
  152. current_position.onkeydownNode('W');}
  153. else if ((key=="right" && shiftDown) || (key=="d" ) ){
  154. current_position.onkeydownNode('E');}
  155. else if ((key=="down" && shiftDown) || (key=="s" ) ){
  156. current_position.onkeydownNode('S');}
  157. //else if (key in oc(['enter', 'space']) ){
  158. // window.location.href=current_position.url;}
  159. else if (key=="shift"){
  160. shiftDown=1;}
  161. }
  162. function onKeyUp(event){
  163. if (event.key == "shift"){
  164. shiftDown=0;
  165. }
  166. }
  167. function onkeydownNode( keydown, node ){
  168. for ( key in this['neighbours'] ){
  169. if (key == keydown){
  170. current_position.emptyNode();
  171. current_position = this['neighbours'][key];
  172. current_position.fillNode();
  173. }
  174. }
  175. return;
  176. }
  177. function DashLine( color, point1, point2){
  178. // This creates a dashed subway line to represent either construction or leaving the webpage.
  179. // color: the color of the dashed line.
  180. // point1, point2: the beginning and end of the dashed line, as paper.js Point objects.
  181. this.color = color;
  182. this.path = new Path();
  183. this.path.style = {
  184. strokeWidth: LINE_WIDTH,
  185. strokeColor: this.color,
  186. };
  187. this.path.dashArray = [7,4];
  188. this.path.add(point1, point2);
  189. }
  190. function onMouseDown(event){
  191. // This function fires whenever the mouse is pressed down. It iteratres through the stations and stops if it finds the
  192. // cursor is over a station.
  193. var N = nodes.length;
  194. var point = event.point;
  195. for( var i=0; i<N; i++){
  196. var node_position = nodes[i].position;
  197. if (Path.Circle( node_position, NODE_SIZE ).bounds.contains(point) ){
  198. current_position.emptyNode();
  199. current_position = nodes[i];
  200. current_position.fillNode();
  201. break;
  202. }
  203. }
  204. }
  205. function onFrame(event){
  206. //This control the halo. You can choose to turn it off by removing this function, or you can create your own using
  207. //this function too.
  208. halo.strokeColor = current_position['line']['color'];
  209. halo.scale( Math.pow( 1.04, Math.sin(event.count/8 )));
  210. halo.position = current_position['position'];
  211. //test how long it takes to compute sin(x) for different values of x.
  212. }
  213. var shiftDown = 0;