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.

463 lines
16 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //subway.js
  2. // Author: Cameron Davidson-Pilon
  3. // Date: Sept, 2012
  4. // License: MIT
  5. // Code available at https://github.com/Valloway/Subwayjs
  6. // Dependencies:
  7. // Paper.js (http://paperjs.org/)
  8. // These change the subway node design. Try manipulating them to see what they do.
  9. // Note: all measurments are in pixels.
  10. var NODE_SIZE = 16; //radius of the nodes
  11. var INNER_NODE_SIZE = 7; //radius of the inner circle in the stations
  12. var CUR_NODE_SIZE = 9; // radious of the inner cicle, if is is currently selected.
  13. var LINE_WIDTH = 18; //width of the inter-station lines.
  14. // fonts
  15. var FONT_SIZE = 18;
  16. var TEXT_FONT = 'Carme';
  17. var FONT_COLOR = 'black';
  18. var LINE_EMPH = 5;
  19. var CHARACTER_STYLE = {
  20. fillColor: FONT_COLOR,
  21. fontSize: FONT_SIZE,
  22. font: TEXT_FONT,
  23. };
  24. var nodes = []; // after initilization, this variable will contain all stations. Needed for clicking stations with mouse.
  25. var shiftDown = 0; //This is used in controlling the location with the keys. It is a boolean representing whether the
  26. // shift key is currently pressed down.
  27. function Line( color, name ){
  28. // the Line object creates a subway line.
  29. // color: must be a paper.js RgbColor or a keyword color, eg: 'black', 'green'.
  30. // name: the unique name of the line.
  31. this['color'] = color;
  32. this['path'] = new Path();
  33. this['path'].strokeWidth = LINE_WIDTH;
  34. this['path'].strokeColor = this['color'];
  35. this['name'] = name;
  36. }
  37. function SingleNode( line, position, neighbours, name, url, name_offset, name_angle){
  38. // the Node object is a non transitionay subway stop
  39. // line: the line the node lies on.
  40. // position: the coordinates on the canvas as Point class object.
  41. // neighbours: the neighbours of the node. It
  42. // must be the syntax {'E':Neighbour1, 'W':Neighbour2, etc.}. Note: this way we can also do things like:
  43. // {'W': Neighbour1, 'S': Neighbour1, 'E': Neighbour2}. Note all the directions need to be defined.
  44. // name: the station name to be displayed.
  45. // url: the url to go to if the SpaceBar is pressed. Leave as '' if not wanted.
  46. // name_offset: the amount to move the display named away from the center of the station. It must be a paper.js Point
  47. // object. ie. new Point( 10, 10) will position the top left corner of the text 10 pixels right and
  48. // 10 pixels below the center of the station.
  49. // name_angle: the angle the name to be at. Enter it in counterclockwise radians.
  50. this['line'] = line;
  51. this['position'] = position;
  52. this['name'] = name;
  53. this['url'] = url;
  54. this['name_angle'] = name_angle;
  55. this['neighbours'] = neighbours;
  56. nodes.push(this)
  57. //Methods
  58. this.onkeydownNode = onkeydownNode; //this controls the movement if keys are pressed.
  59. this.fillNode = function(){
  60. //this method fires when the user enters the node.
  61. move_infobox(this)
  62. this.ic.fillColor = 'black';
  63. this['line']['path'].strokeWidth = LINE_WIDTH + LINE_EMPH;
  64. // this['line']['path'].strokeColor.alpha= .9;
  65. };
  66. this.emptyNode = function(){
  67. //this method fires when the user enters the node.
  68. this.ic.fillColor = 'white';
  69. this['line']['path'].strokeWidth = LINE_WIDTH;
  70. //this['line']['path'].strokeColor.alpha= .4;
  71. };
  72. // generate the visual shapes.
  73. var c = new Path.Circle( this['position'], NODE_SIZE);
  74. c.fillColor = this['line']['color'];
  75. var ic = new Path.Circle( this['position'], INNER_NODE_SIZE+2);
  76. ic.fillColor = 'black';
  77. this.ic = new Path.Circle( this['position'], INNER_NODE_SIZE);
  78. this.ic.fillColor = 'white';
  79. this['text'] = new PointText( this['position'] + name_offset );
  80. this['text'].content = this['name'];
  81. this['text'].characterStyle = CHARACTER_STYLE;
  82. this['text'].rotate( -this['name_angle'], this['position'] );
  83. // add it to the line.
  84. this['line']['path'].add( this['position'] );
  85. }
  86. function TransitionNode( line, line2, angle, position, neighbours, name, url,name_offset, name_angle){
  87. // the Node object is a non transitionay subway stop
  88. // line: the primary line the node lies on.
  89. // line2: the secondary line the node lies on.
  90. // angle: the angle of the above and underneath node. Equal to the angle of the two lines.
  91. // position: the coordinates on the canvas as Point class object.
  92. // neighbours: the neighbours of the node. It
  93. // must be the syntax {'E':Neighbour1, 'W':Neighbour2, etc.}. Note: this way we can also do things like:
  94. // {'W': Neighbour1, 'S': Neighbour1, 'E': Neighbour2}. Note all the directions need to be defined.
  95. // name: the station name to be displayed.
  96. // url: the url to go to if the SpaceBar is pressed. Leave as '' if not wanted.
  97. // name_offset: the amount to move the display named away from the center of the station. It must be a paper.js Point
  98. // object. ie. new Point( 10, 10) will position the top left corner of the text 10 pixels right and
  99. // 10 pixels below the center of the station.
  100. // name_angle: the angle the name to be at. Enter it in counterclockwise radians.
  101. var STROKE_WIDTH = 2; //this two control the offset and appearance of the 'underneath node'
  102. var CIRCLE_DIST = 16;
  103. //This Node is the class for line transition stations.
  104. this['line'] = line;
  105. this['line2'] = line2;
  106. this['position'] = position; //This is the position of either of the circles. The angle determines the position of the other one.
  107. this['neighbours'] = neighbours;
  108. this['angle'] = angle; //This is the angle between the two circles.
  109. this['positionAlt'] = this['position'] + new Point( CIRCLE_DIST*Math.cos( angle), CIRCLE_DIST*Math.sin( angle) );
  110. this['name'] = name;
  111. this['url'] = url;
  112. nodes.push(this);
  113. //Methods;
  114. this.onkeydownNode = onkeydownNode;
  115. this.emptyNode = function(){
  116. this.innerc.fillColor = this['line']['color'];
  117. this['line']['path'].strokeWidth = LINE_WIDTH;
  118. this['line2']['path'].strokeWidth = LINE_WIDTH;
  119. //this['line2']['path'].strokeColor.alpha = .4;
  120. //this['line']['path'].strokeColor.alpha = .4;
  121. }
  122. this.fillNode = function(){
  123. this.innerc.fillColor = 'black';
  124. this['line']['path'].strokeWidth = LINE_WIDTH + LINE_EMPH;
  125. this['line2']['path'].strokeWidth = LINE_WIDTH + LINE_EMPH;
  126. move_infobox(this)
  127. //this['line2']['path'].strokeColor.alpha = .9;
  128. //this['line']['path'].strokeColor.alpha= .9;
  129. }
  130. // Title to display.
  131. this['text'] = new PointText( this['position'] + name_offset );
  132. this['text'].content = this['name'];
  133. this['text'].characterStyle = CHARACTER_STYLE;
  134. this['text'].rotate( -name_angle, this.position);
  135. //generate visual shapes
  136. var c2 = new Path.Circle( this.positionAlt , NODE_SIZE);
  137. c2.strokeWidth = STROKE_WIDTH;
  138. c2.strokeColor = 'black';
  139. c2.fillColor = 'white';
  140. var c2 = new Path.Circle( this.positionAlt, CUR_NODE_SIZE+1);
  141. c2.fillColor = this.line2.color;
  142. var c1 = new Path.Circle( this.position, NODE_SIZE);
  143. c1.strokeWidth = STROKE_WIDTH;
  144. c1.fillColor = 'white';
  145. c1.strokeColor = 'black';
  146. this.innerc = new Path.Circle( this.position, CUR_NODE_SIZE);
  147. this.innerc.fillColor = this.line.color;
  148. //add it the the lines.
  149. this['line'].path.add( this['position']);
  150. this['line2'].path.add(this['positionAlt']);
  151. }
  152. function move_infobox(node){
  153. var x = node['position'].x;
  154. var y = node['position'].y;
  155. infobox.style.left = x + 20;
  156. infobox.style.top = y - 20 ;
  157. document.getElementById('infobox-text').innerHTML = document.getElementById( toHTMLname(node['name']) ).innerHTML; //might be easier to have a property in the object to store this.
  158. return;
  159. }
  160. function toHTMLname( name ){
  161. //var htmlname = name.replace(//g, '' );
  162. var htmlname = name.replace(/[.'-*\s]/g, '').toLowerCase()
  163. console.log( htmlname );
  164. return htmlname.toLowerCase();
  165. }
  166. function LegendItem( start, end, subwayLine ){
  167. this['line'] = new Line(subwayLine['color'], subwayLine['name']);
  168. this['line']['path'].add( start, end)
  169. var title = new PointText( end + new Point( 7,5) );
  170. title.content = subwayLine.name;
  171. title.characterStyle = {
  172. fillColor: 'black',
  173. fontSize: 14,
  174. font: TEXT_FONT
  175. };
  176. }
  177. function onKeyDown(event){
  178. var key = event.key;
  179. if ((key=="up" && shiftDown) || (key=="w" ) ){
  180. current_position.onkeydownNode('N');}
  181. else if ((key=="left" && shiftDown) || (key=="a" ) ){
  182. current_position.onkeydownNode('W');}
  183. else if ((key=="right" && shiftDown) || (key=="d" ) ){
  184. current_position.onkeydownNode('E');}
  185. else if ((key=="down" && shiftDown) || (key=="s" ) ){
  186. current_position.onkeydownNode('S');}
  187. //else if (key in oc(['enter', 'space']) ){
  188. // window.location.href=current_position.url;}
  189. else if (key=="shift"){
  190. shiftDown=1;}
  191. }
  192. function onKeyUp(event){
  193. if (event.key == "shift"){
  194. shiftDown=0;
  195. }
  196. }
  197. function onkeydownNode( keydown, node ){
  198. for ( key in this['neighbours'] ){
  199. if (key == keydown){
  200. current_position.emptyNode();
  201. current_position = this['neighbours'][key];
  202. current_position.fillNode();
  203. }
  204. }
  205. return;
  206. }
  207. function DashLine( color, point1, point2){
  208. this['color'] = color;
  209. this['path'] = new Path();
  210. this['path'].style = {
  211. strokeWidth: LINE_WIDTH,
  212. strokeColor: this.color,
  213. };
  214. this['path'].dashArray = [7,4];
  215. this['path'].add(point1, point2);
  216. }
  217. function onMouseDown(event){
  218. // This function fires whenever the mouse is pressed down. It iteratres through the stations and stops if it finds the
  219. // cursor is over a station.
  220. var N = nodes.length;
  221. var point = event.point;
  222. for( var i=0; i<N; i++){
  223. var node_position = nodes[i].position;
  224. if (Path.Circle( node_position, NODE_SIZE ).bounds.contains(point) ){
  225. current_position.emptyNode();
  226. current_position = nodes[i];
  227. current_position.fillNode();
  228. break;
  229. }
  230. }
  231. }
  232. function onMouseMove(event){
  233. // This function fires whenever the mouse moves. Creates hover pointer
  234. var N = nodes.length;
  235. var point = event.point;
  236. for( var i=0; i<N; i++){
  237. var node_position = nodes[i].position;
  238. if (Path.Circle( node_position, NODE_SIZE ).bounds.contains(point) ){
  239. document.body.style.cursor = 'pointer';
  240. return;
  241. }
  242. }
  243. document.body.style.cursor = 'default';
  244. }
  245. function onFrame(event){
  246. //This control the halo. You can choose to turn it off by removing this function, or you can create your own using
  247. //this function too.
  248. halo.strokeColor = current_position['line']['color'];
  249. halo.scale( Math.pow( 1.04, Math.sin(event.count/5 )));
  250. halo.position = current_position['position'];
  251. //test how long it takes to compute sin(x) for different values of x.
  252. }
  253. H_CANVAS = 800;
  254. W_CANVAS = 1000;
  255. offset = new Point( 20 ,20);
  256. H_SLC = 250
  257. W_SLC = 450
  258. SLC_delta = 190;
  259. //create the river
  260. river = new Path()
  261. river.add( new Point(W_SLC - 60 + 90, H_SLC+SLC_delta + 200) )
  262. river.add( new Point(W_SLC - 60 - 20, H_SLC+SLC_delta) )
  263. river.add( new Point(W_SLC - 60, H_SLC) )
  264. river.add( new Point(W_SLC - 120, H_SLC - 30) )
  265. river.add( new Point(W_SLC - 190, H_SLC - 30) )
  266. river.add( new Point( W_SLC - 2*SLC_delta/2 - 60, H_SLC - 1*SLC_delta/2) )
  267. river.add( new Point( W_SLC - 2*SLC_delta/2, H_SLC - 1*SLC_delta/2 - 60 ) )
  268. river.add( new Point( W_SLC - 1*SLC_delta/2-20, 0) )
  269. river.smooth();
  270. river.strokeColor = "rgb(200,200,255)";
  271. river.strokeWidth = 12;
  272. //create lines
  273. RingLine = new Line( '#af1b8b', 'Ring Line');
  274. NWLine = new Line( '#b3d529', 'SLC Line');
  275. NLine = new Line( '#ababab', 'CIF Line');
  276. DCLine = new Line( '#9AE4E8', 'DC Line');
  277. EVLine = new Line( 'rgb(23,45,100)', 'EV Line');
  278. var dCIF = new DashLine( NLine['color'], new Point( W_SLC+SLC_delta, H_SLC-160)+ new Point( 0, -75), new Point( W_SLC+SLC_delta, H_SLC-160) );
  279. var dUWP = new DashLine( NWLine['color'], new Point( W_SLC+2*SLC_delta + 100, H_SLC+SLC_delta + 55)+ new Point( +75, 0), new Point( W_SLC+2*SLC_delta + 100, H_SLC+SLC_delta + 55) );
  280. clv = new SingleNode( NWLine, new Point( W_SLC - 3*SLC_delta/2, H_SLC - 2*SLC_delta/2), null, 'CLV', '/', new Point(25, 0), 0);
  281. rev = new SingleNode( NWLine, new Point( W_SLC - 2*SLC_delta/2, H_SLC - 1*SLC_delta/2), null, 'REV', '/', new Point(15,-18), 0);
  282. v1 = new SingleNode( NWLine, new Point( W_SLC - 1*SLC_delta/2, H_SLC - 1*SLC_delta/2), null, 'V1', '/', new Point(15,-18), 0);
  283. slc = new TransitionNode( RingLine, NWLine,3.8, new Point( W_SLC, H_SLC ), null, 'SLC','/', new Point( -65, 15), 0 );
  284. dp2 = new TransitionNode( NLine, EVLine, -4.2, new Point( W_SLC+SLC_delta, H_SLC + SLC_delta/2), null, '', '/', new Point(20, -7), 0);
  285. dp = new TransitionNode( NLine, NWLine, .2, new Point( W_SLC+SLC_delta, H_SLC + SLC_delta/2), null, 'DP Library', '/', new Point(24, -15), 0);
  286. cif = new SingleNode( NLine, new Point( W_SLC+SLC_delta, H_SLC-160), null, 'CIF', '/', new Point(23,5), 0);
  287. bmh = new TransitionNode( RingLine, NLine, 4.75, new Point( W_SLC+SLC_delta, H_SLC-70 ), null, 'BMH','/', new Point(25, -10), 0 );
  288. mc = new TransitionNode( NLine, DCLine, 0, new Point( W_SLC+SLC_delta, H_SLC), null, 'MC', '/', new Point(20, 34), 0 );
  289. dc = new TransitionNode( RingLine, DCLine, -3.1, new Point( W_SLC+2*SLC_delta, H_SLC), null, 'DC','/', new Point(20,25), 0 );
  290. engplus = new SingleNode( DCLine, new Point( W_SLC+2.5*SLC_delta, H_SLC), null, 'ENG-Plus', '/', new Point(20,25), 0);
  291. engminus = new TransitionNode( RingLine, NWLine, .5, new Point( W_SLC+2*SLC_delta, H_SLC+SLC_delta), null, 'ENG-Minus','/', new Point( 20, -10), 0 );
  292. uwp = new SingleNode( NWLine, new Point( W_SLC+2*SLC_delta + 100, H_SLC+SLC_delta + 55), null, 'UW Place', '', new Point(18, 28), 0);
  293. sch2 = new TransitionNode( RingLine, EVLine, -2, new Point( W_SLC+SLC_delta, H_SLC+SLC_delta+70), null, '','/', new Point(-175, 30), 0 );
  294. sch = new TransitionNode( RingLine, NLine, 1.3, new Point( W_SLC+SLC_delta, H_SLC+SLC_delta+70), null, 'South Campus Hall','/', new Point(-175, 30), 0 );
  295. seagram = new SingleNode( NLine, new Point( W_SLC + 1.5*SLC_delta, H_SLC+SLC_delta+55 + SLC_delta/2), null, 'Seagram','/', offset, 0 );
  296. ev = new TransitionNode( RingLine, EVLine, -1.8, new Point(W_SLC, H_SLC+SLC_delta), null, 'EV*', '', new Point(-50, 18), 0);
  297. health = new SingleNode( EVLine, new Point( W_SLC - 1*SLC_delta/2- 20, H_SLC+SLC_delta - 1*SLC_delta/2 ), null, 'College', '/', new Point(-30,-22), 0);
  298. RingLine.path.closePath();
  299. clv.neighbours = { 'S':rev, 'E': rev};
  300. seagram.neighbours = { 'N':sch, 'W': sch};
  301. uwp.neighbours = { 'N':engminus, 'W': engminus};
  302. rev.neighbours = { 'N':clv, 'E':v1, 'W':clv};
  303. v1.neighbours = { 'W':rev, 'S':slc, 'E':slc};
  304. slc.neighbours = { 'S':ev, 'W':v1, 'N':bmh, 'E':dp};
  305. ev.neighbours = {'N': slc, 'E':sch, 'S':sch, 'W': health};
  306. dp.neighbours = {'W':slc, 'S': sch, 'N': mc, 'E':engminus};
  307. bmh.neighbours = {'W':slc, 'S': mc, 'N': cif, 'E':dc};
  308. dc.neighbours = {'W':mc, 'S': engminus, 'N': bmh, 'E':engplus};
  309. engminus.neighbours = {'W':dp, 'S': sch, 'N':dc, 'E':uwp};
  310. sch.neighbours = {'W':ev, 'S': seagram, 'N':dp, 'E':engminus};
  311. engplus.neighbours = {'W':dc};
  312. mc.neighbours = {'S': dp, 'N': bmh, 'E':dc};
  313. cif.neighbours = {'S':bmh};
  314. health.neighbours = {'S':ev, 'E': ev};
  315. //legend
  316. var lg1 = new LegendItem( new Point( 60,300), new Point( 170,300), RingLine);
  317. var lg2 = new LegendItem( new Point( 60,330), new Point( 170,330), NWLine);
  318. var lg3 = new LegendItem( new Point( 60,360), new Point( 170,360), NLine);
  319. var lg4 = new LegendItem( new Point( 60,390), new Point( 170,390), DCLine);
  320. var lg4 = new LegendItem( new Point( 60,420), new Point( 170,420), EVLine);
  321. // title
  322. var title = new PointText( new Point(60,500) );
  323. title.content = "University of Waterloo \nProposed Subway Map";
  324. title.characterStyle = {
  325. fillColor: 'black',
  326. fontSize: 25,
  327. font: "Helvetica"
  328. };
  329. var description = new PointText( new Point(60,560) );
  330. description.content = "This is a map based on a proposed subway system\n underneath the University of Waterloo.\n Hold [shift] and use your arrow keys to move around, \n or press your WASD keys.";
  331. description.characterStyle = {
  332. fillColor: 'black',
  333. fontSize: 14,
  334. font: "Arial"
  335. };
  336. var shiftDown = 0;
  337. current_position = slc;
  338. current_position.fillNode();
  339. // form the halo
  340. halo = new Path.Circle( current_position.position, NODE_SIZE );
  341. halo.strokeColor = current_position.line.color;
  342. halo.strokeWidth = 3;