|
1 | | -var Node = { |
2 | | - paper: null, // We must get a Raphael canvas passed into the function |
3 | | - start_x: 0, |
4 | | - nname: 'default', |
5 | | - category: 'default', |
6 | | - ndescription: 'something', |
7 | | - previous: null, |
8 | | - next: null, |
9 | | - |
10 | | - makeLine: function(start, len) { |
| 1 | +function Node(paper, nodeName, startX) { |
| 2 | + var that = this; // Create a handle to the current node |
| 3 | + this.paper = paper; // Raphael canvas on which this node is drawn |
| 4 | + this.nodeName = nodeName; |
| 5 | + this.startX = startX; |
| 6 | + this.category = 'default'; |
| 7 | + this.description = 'description text'; |
| 8 | + this.previous = null; |
| 9 | + this.next = null; |
| 10 | + this.circle = null; // We will need access to the circle portion of the node so we can highlight it |
| 11 | + |
| 12 | + this.makeLine = function(start, len) { |
11 | 13 | var lparams = ['M', start, HORIZON, 'l', len, 0]; |
12 | 14 | return this.paper.path(lparams.join(' ')).attr(LINE_ATTRS); |
13 | | - }, |
| 15 | + } |
14 | 16 |
|
15 | | - makeCircle: function(x, y) { |
| 17 | + this.makeCircle = function(x, y) { |
16 | 18 | return this.paper.circle(x, y, NODE_RADIUS).attr(NODE_ATTRS); |
17 | | - }, |
18 | | - |
19 | | - segment: function() { |
20 | | - var left_l = this.makeLine(this.start_x, ARM_LEN); |
21 | | - var circle_x = this.start_x + ARM_LEN + NODE_RADIUS; |
22 | | - var circle = this.makeCircle(circle_x, HORIZON); |
23 | | - var right_l = this.makeLine(this.start_x + ARM_LEN + NODE_RADIUS*2, ARM_LEN); |
24 | | - return this.paper.set(left_l, circle, right_l); |
25 | | - }, |
26 | | - |
27 | | - highlightNode: function(){ |
28 | | - console.log('highlightNode called on ' + this); |
29 | | - request_ball.attr({'stroke-width': 0}); |
30 | | - this.attr({fill: 'red'}); |
31 | | - this.paper.text(this.start_x - 50, HORIZON, this.nname).attr({"font-size": 12}); |
32 | | - }, |
| 19 | + } |
33 | 20 |
|
34 | | - animateNext: function() { |
35 | | - var destination = null; |
| 21 | + this.draw = function() { |
| 22 | + var left_l = this.makeLine(this.startX, ARM_LEN); |
| 23 | + var circle_x = this.startX + ARM_LEN + NODE_RADIUS; |
| 24 | + this.circle = this.makeCircle(circle_x, HORIZON); |
| 25 | + var right_l = this.makeLine(this.startX + ARM_LEN + NODE_RADIUS*2, ARM_LEN); |
| 26 | + return this.paper.set(left_l, this.circle, right_l); |
| 27 | + } |
36 | 28 |
|
37 | | - if (this.next != null) { |
38 | | - console.log('go to next'); |
39 | | - destination = Raphael.animation({cx: this.next.start_x, cy: HORIZON}, SPEED, this.highlightNode); |
40 | | - request_ball.animate(destination); |
41 | | - } else if (this.previous != null) { |
42 | | - console.log('go to previous'); |
43 | | - destination = Raphael.animation({cx: this.previous.start_x, cy: HORIZON}, -SPEED, this.highlightNode); |
44 | | - request_ball.animate(destination); |
| 29 | + this.highlightNode = function() { |
| 30 | + console.log(that); |
| 31 | + that.circle.attr({fill: 'red'}); |
| 32 | + this.paper.text(that.startX + ARM_LEN + NODE_RADIUS, HORIZON, that.nodeName).attr({"font-size": 12}); |
| 33 | + if (that.next != null) { |
| 34 | + destination = that.next; |
| 35 | + } else if (that.previous != null) { |
| 36 | + destination = that.previous; |
45 | 37 | } else { |
46 | | - console.log('go to nowhere'); |
| 38 | + console.log('no where to go'); |
47 | 39 | } |
| 40 | + console.log('destination should be ' + destination.nodeName); |
48 | 41 | } |
49 | 42 | }; |
50 | 43 |
|
51 | | -function makeNodes() { |
52 | | - var p = new Raphael(document.getElementById('canvas_container'), 1000, 500); |
| 44 | +function createAnimation() { |
53 | 45 | LINE_ATTRS = {stroke: '#aaa', 'stroke-width': 5}; |
54 | 46 | NODE_ATTRS = {stroke: '#aaa', 'stroke-width': 2, fill: 'white'}; |
55 | 47 | NODE_RADIUS = 50; |
56 | 48 | SPEED = 500; |
57 | 49 | HORIZON = 200; |
58 | 50 | ARM_LEN = 50; |
59 | 51 |
|
60 | | - function main() { |
61 | | - request_ball = p.circle(-50, HORIZON, 5).attr({fill: 'red'}); |
| 52 | + var p = new Raphael(document.getElementById('canvas_container'), 1000, 500); |
| 53 | + var requestBall = p.circle(0, HORIZON, 5).attr({fill: 'red'}); |
62 | 54 |
|
| 55 | + function makeNodes() { |
63 | 56 | for(var i = 0; i < 4; i+=1) { |
64 | | - var prev = null; |
65 | | - if (i > 0) { |
66 | | - prev = node[i-1]; |
67 | | - } |
68 | | - |
69 | | - node[i] = Object.create(Node, { paper: {value: p}, |
70 | | - start_x: {value: 200*i}, |
71 | | - previous: {value: prev}, |
72 | | - nname: {value: "NODE " + i} |
73 | | - }); |
74 | | - if (i > 0) { |
75 | | - node[i-1].next = node[i]; |
76 | | - } |
77 | | - node[i].segment(); |
| 57 | + // Create new node with paper, nodeName, and startX parameters |
| 58 | + node[i] = new Node(p, "NODE " + (i+1), 200*i); // cheat and display nodes as if we started counting arrays at 1 |
| 59 | + // If this isn't the first node, set it's previous attribute to previous node |
| 60 | + if (i > 0) { node[i].previous = node[i-1]; } |
| 61 | + // And update previous node to know this node as 'next' |
| 62 | + if (i > 0) { node[i-1].next = node[i]; } |
| 63 | + node[i].draw(); |
78 | 64 | }; |
79 | 65 | }; |
80 | 66 |
|
| 67 | + function animateRequestBall(destination) { |
| 68 | + var centerOfNode = destination.startX + ARM_LEN + NODE_RADIUS; |
| 69 | + var anim = Raphael.animation({cx: centerOfNode, cy: HORIZON}, SPEED, destination.highlightNode); |
| 70 | + requestBall.animate(anim); |
| 71 | + } |
| 72 | + |
81 | 73 | var node = []; |
82 | | - var i = 0; |
83 | | - main(); |
84 | | - console.log(node[0]); |
| 74 | + makeNodes(); |
| 75 | + destination = node[0]; |
85 | 76 |
|
86 | | - function start() { |
87 | | - node[i].animateNext(); |
88 | | - i++; |
| 77 | + function move() { |
| 78 | + animateRequestBall(destination); |
89 | 79 | } |
90 | 80 |
|
91 | | - $('#canvas_container').on("click", start); |
| 81 | + $('#canvas_container').on("click", move); |
92 | 82 | }; |
93 | 83 |
|
94 | | - |
95 | | - |
96 | | -$(document).ready(makeNodes); |
| 84 | +$(document).ready(createAnimation); |
0 commit comments