Skip to content

Commit 37020ca

Browse files
committed
animation mostly works - except for logic error in traversing the return trip
1 parent 98a3ddc commit 37020ca

File tree

1 file changed

+55
-67
lines changed

1 file changed

+55
-67
lines changed

our_script.js

Lines changed: 55 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,84 @@
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) {
1113
var lparams = ['M', start, HORIZON, 'l', len, 0];
1214
return this.paper.path(lparams.join(' ')).attr(LINE_ATTRS);
13-
},
15+
}
1416

15-
makeCircle: function(x, y) {
17+
this.makeCircle = function(x, y) {
1618
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+
}
3320

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+
}
3628

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;
4537
} else {
46-
console.log('go to nowhere');
38+
console.log('no where to go');
4739
}
40+
console.log('destination should be ' + destination.nodeName);
4841
}
4942
};
5043

51-
function makeNodes() {
52-
var p = new Raphael(document.getElementById('canvas_container'), 1000, 500);
44+
function createAnimation() {
5345
LINE_ATTRS = {stroke: '#aaa', 'stroke-width': 5};
5446
NODE_ATTRS = {stroke: '#aaa', 'stroke-width': 2, fill: 'white'};
5547
NODE_RADIUS = 50;
5648
SPEED = 500;
5749
HORIZON = 200;
5850
ARM_LEN = 50;
5951

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'});
6254

55+
function makeNodes() {
6356
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();
7864
};
7965
};
8066

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+
8173
var node = [];
82-
var i = 0;
83-
main();
84-
console.log(node[0]);
74+
makeNodes();
75+
destination = node[0];
8576

86-
function start() {
87-
node[i].animateNext();
88-
i++;
77+
function move() {
78+
animateRequestBall(destination);
8979
}
9080

91-
$('#canvas_container').on("click", start);
81+
$('#canvas_container').on("click", move);
9282
};
9383

94-
95-
96-
$(document).ready(makeNodes);
84+
$(document).ready(createAnimation);

0 commit comments

Comments
 (0)