-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathtest_spiral.html
143 lines (114 loc) · 3.61 KB
/
test_spiral.html
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
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>dragit example with a spiral</title>
<link href="../src/dragit.css" rel="stylesheet"/>
<script src="../lib/d3.v3.js"></script>
<script src="../src/dragit.js"></script>
<style>
path.spiral {
fill: none;
stroke: #ee8d18;
stroke-width: 3px;
}
</style>
</head>
<body>
<div id="viz"></div>
<p style="clear:both"></p>
<div id="slider"></div>
<label><input type="checkbox" name="mode" value="trajectory" onclick="dragit.trajectory.toggleAll('selected');" checked> Show complete trajectory</label>
<script>
// Credits for original spiral code: http://bl.ocks.org/syntagmatic/3543186
var margin = {top: 20, right: 20, bottom: 20, left: 20},
width = 900 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;
var svg = d3.select("#viz")
.append("svg")
.attr({width: width, height: height})
var num_axes = 8,
start = 0,
end = 10;
var theta = function(r) {
return -2*Math.PI*r;
};
var arc = d3.svg.arc()
.startAngle(0)
.endAngle(2*Math.PI);
var radius = d3.scale.linear()
.domain([start, end])
.range([0, d3.min([width,height])/2-20]);
var angle = d3.scale.linear()
.domain([0,num_axes])
.range([0, 360])
var pieces = d3.range(start, end+0.001, (end-start)/1000);
var spiral = d3.svg.line.radial()
.interpolate("cardinal")
.angle(theta)
.radius(radius);
var path_spiral = svg.selectAll(".spiral")
.data([pieces])
.enter().append("path")
.attr("class", "spiral")
.attr("d", spiral)
.attr("transform", function(d) { return "translate("+width/2+", "+height/2+")rotate(" + 90 + ")" });
var time_steps = 200, nb_points = 1, current_time = 0;
var config = {
time: {min: 0, max: 20, step: 1, current: 0}
}
var total_length = path_spiral.node().getTotalLength();
var share_length = total_length / time_steps;
// Partition of the SVG shape into equal distances to create a time cube
var timecube = d3.range(nb_points).map(function(d, i) {
return d3.range(time_steps).map(function(e, j) {
var current_point = path_spiral.node(0).getPointAtLength(share_length*j);
return {x: current_point.x+width/2, y: current_point.y+height/2, t: j};
});
})
var gPoints = svg.selectAll(".points")
.data(timecube)
.enter()
.append("g")
.on("mouseenter", function(d, i) {
dragit.utils.animateTrajectory(dragit.trajectory.display(d, i), 0, 1000)
})
.on("mouseleave", dragit.trajectory.remove)
.call(dragit.object.activate)
.attr("transform", function(d) {
return "translate("+d[current_time].x+", "+d[current_time].y+")";
});
gPoints.append("circle")
.attr("class", "points")
.attr({r:10, fill:"red"})
function update(v, t) {
var previous_current = dragit.time.current;
dragit.time.current = v || dragit.time.current;
dragit.statemachine.current_id = 0;
gPoints.transition()
.duration(200)
.attr("transform", function(d) {
return "translate("+d[dragit.time.current].x+", "+d[dragit.time.current].y+")";
})
//if(dragit.time.current != dragit.time.previous) {
// gPoints.transition()
// .duration(1000)
// .attrTween("transform", dragit.utils.getSubPath(previous_current, dragit.time.current))
//}
}
function init() {
dragit.init("svg");
dragit.data = timecube.map(function(d, i) {
return d.map(function(e, i) {
return [e.x, e.y];
})
});
dragit.time = {min:0, max:time_steps, step:1, current: current_time}
dragit.evt.register("update", update);
dragit.trajectory.toggleAll('selected');
dragit.utils.slider("#slider", true)
}
init();
</script>
</body>
</html>