/
actions.js
156 lines (129 loc) · 5.06 KB
/
actions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var Action = function () {
};
Action.prototype.getCoords = function (count, pos) {
var theta = tau / 8 + (tau / 4 * pos);
return [Math.cos(theta) * 100 / Math.sqrt(2), Math.sin(theta) * 100 / Math.sqrt(2)];
};
Action.prototype.getX = function (count, pos) {
return this.getCoords(count, pos)[0];
};
Action.prototype.getY = function (count, pos) {
return this.getCoords(count, pos)[1];
};
Action.prototype.calculateEndingPositions = function() {
throw "Not implemented.";
};
var Wait = function (duration, radius) {
Action.call(this);
this.duration = duration;
this.radius = radius;
};
Wait.prototype = Object.create(Action.prototype);
Wait.prototype.calculateEndingPositions = function() {
if(this.startingPositions === undefined) {
this.startingPositions = [];
for (var i = 0; i < 4; i++) {
this.startingPositions.push(this.getCoords(0, i));
}
}
this.endingPositions = this.startingPositions;
};
var Rotate = function (duration, direction, speed, radius) {
Action.call(this);
this.radius = radius;
this.direction = direction;
this.speed = speed;
this.duration = duration;
};
Rotate.prototype = Object.create(Action.prototype);
Rotate.prototype.getCoords = function (count, pos) {
var startingPosition = this.startingPositions[pos];
var theta_delta = tau * this.speed * this.direction * count;
var init_theta = cartesianToPolar([0,0], startingPosition)[1];
var theta = init_theta + theta_delta;
return [Math.cos(theta) * this.radius, Math.sin(theta) * this.radius];
};
Rotate.prototype.calculateEndingPositions = function() {
this.endingPositions = [];
for(var i = 0; i < 4; i++) {
this.endingPositions.push(this.getCoords(this.duration, i));
}
};
var Spin = function (duration, direction, speed, radius) {
Action.call(this);
this.duration = duration;
this.direction = direction;
this.speed = speed;
this.radius = radius;
};
Spin.prototype = Object.create(Action.prototype);
Spin.prototype.getCoords = function (count, pos) {
var startingPosition = this.startingPositions[pos];
var partnerStartingPosition = this.startingPositions[pos ^ 1];
var cx = (startingPosition[0] + partnerStartingPosition[0]) / 2;
var cy = (startingPosition[1] + partnerStartingPosition[1]) / 2;
var polar = cartesianToPolar([cx, cy], startingPosition);
var theta_delta = tau * this.speed * count * this.direction;
var theta_end = polar[1] + theta_delta;
return [Math.cos(theta_end) * this.radius + cx, Math.sin(theta_end) * this.radius + cy];
};
Spin.prototype.calculateEndingPositions = function() {
this.endingPositions = [];
for(var i = 0; i < 4; i++) {
this.endingPositions.push(this.getCoords(this.duration, i));
}
};
var Approach = function(duration, radius) {
Action.call(this);
this.duration = duration;
this.radius = radius;
};
Approach.prototype = Object.create(Action.prototype);
Approach.prototype.getCoords = function(count, pos) {
return this.interpolators[pos](count/this.duration);
};
Approach.prototype.calculateEndingPositions = function() {
this.endingPositions = [];
this.interpolators = [];
for(var pos = 0; pos < 4; pos++) {
var partnerPos = pos ^ 1;
var centerX = (this.startingPositions[pos][0] + this.startingPositions[partnerPos][0]) / 2;
var centerY = (this.startingPositions[pos][1] + this.startingPositions[partnerPos][1]) / 2;
var polar = cartesianToPolar([centerX, centerY], this.startingPositions[pos]);
var target = polarToCartesian([this.radius, polar[1]], [centerX, centerY]);
this.endingPositions.push(target);
this.interpolators.push(d3.interpolate(this.startingPositions[pos],target));
}
};
var Retreat = function(duration, radius) {
Action.call(this);
this.duration = duration;
this.radius = radius;
};
Retreat.prototype = Object.create(Action.prototype);
Retreat.prototype.getCoords = function(count, pos) {
return this.interpolators[pos](count/this.duration);
};
Retreat.prototype.calculateEndingPositions = function() {
this.endingPositions = [];
this.interpolators = [];
for(var pos = 0; pos < 4; pos++) {
var partnerPos = pos ^ 1;
var centerX = (this.startingPositions[pos][0] + this.startingPositions[partnerPos][0]) / 2;
var centerY = (this.startingPositions[pos][1] + this.startingPositions[partnerPos][1]) / 2;
var polar = cartesianToPolar([centerX, centerY], this.startingPositions[pos]);
var target = polarToCartesian([this.radius, polar[1]], [centerX, centerY]);
this.endingPositions.push(target);
this.interpolators.push(d3.interpolate(this.startingPositions[pos],target));
}
};
function cartesianToPolar(center, point) {
var dx = point[0] - center[0],
dy = point[1] - center[1],
dist = Math.sqrt(dx * dx + dy * dy),
atan = Math.atan2(dy, dx);
return [dist, atan];
}
function polarToCartesian(polar, center) {
return [Math.cos(polar[1]) * polar[0] + center[0], Math.sin(polar[1]) * polar[0] + center[1]];
}