-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample2.html
More file actions
84 lines (68 loc) · 1.5 KB
/
example2.html
File metadata and controls
84 lines (68 loc) · 1.5 KB
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
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<style>
path {
fill: none;
stroke: #000;
stroke-width: 3px;
}
circle {
fill: steelblue;
stroke: #fff;
stroke-width: 3px;
}
</style>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var points = [
[480, 200],
[480, 400],
[680, 200],
[680, 400]
];
var line = d3.svg.line()
.tension(0)
//.interpolate("linear-closed");
.interpolate("basis-closed");
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 900);
svg.append("path")
.datum(points)
.style("stroke", "#ddd")
.style("stroke-dasharray", "4,4")
.attr("d", line);
var path = svg.append("path")
.datum(points)
.attr("d", line)
.call(transition2);
var circle = svg.append("circle")
.attr("r", 7)
.attr("transform", "translate(" + points[0] + ")");
transition();
function transition() {
circle.transition()
.duration(7500)
.attrTween("transform", translateAlong(path.node()));
}
function transition2(path) {
path.transition()
.duration(7500)
.attrTween("stroke-dasharray", tweenDash);
}
function tweenDash() {
var l = this.getTotalLength(),
i = d3.interpolateString(0 + "," + l, l + "," + l);
return function(t) { return i(t); };
}
function translateAlong(path) {
var l = path.getTotalLength();
return function(d, i) {
return function(t) {
var p = path.getPointAtLength(t * l);
return "translate(" + p.x + "," + p.y + ")";
};
};
}
</script>