forked from devsar/d3talk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbezier.html
207 lines (175 loc) · 4.55 KB
/
bezier.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script type="text/javascript" src="d3/d3.js"></script>
<link type="text/css" rel="stylesheet" href="style.css"/>
<style type="text/css">
.curve, .line {
fill: none;
stroke-width: 1.5px;
}
.curve {
stroke: #000;
stroke-opacity: .5;
stroke-width: 3px;
}
circle {
stroke: none;
}
.control {
fill: #ccc;
stroke: #aaa;
}
text {
font-size: 11px;
}
</style>
</head>
<body>
<div id="body">
<div id="footer">
Bézier Curves by Jason Davies
<div class="hint">drag control points to reshape</div>
<div style="font-size:12px;line-height:60px;"><input type="checkbox" id="play"> <label for="play">animate <i>t</i></label></div>
</div>
</div>
<script type="text/javascript">
// Implementation by Jason Davies
// http://www.jasondavies.com/animated-bezier/
var w = 250,
h = 600,
t = .5,
delta = .01,
padding = 30,
bezier = {},
line = d3.svg.line().x(x).y(y),
n = 4,
orders = d3.range(2, n + 2),
color = d3.scale.category10(),
stop = true,
last = 0;
var points = [
{"x":0,"y":496},
{"x":230,"y":319},
{"x":84,"y":92},
{"x":0,"y":244},
{"x":67,"y":310},
];
var vis = d3.select("#body").append("div")
.style("margin-left", "20px")
.selectAll("div")
.data(orders)
.enter().append("div")
.style("width", w + 2 * padding + "px")
.style("height", h + 2 * padding + "px")
.style("display", "inline-block")
.append("svg:svg")
.attr("width", w + 2 * padding)
.attr("height", h + 2 * padding)
.append("svg:g")
.attr("transform", "translate(" + padding + "," + padding + ")");
vis.selectAll("circle.control")
.data(function(d) { return points.slice(0, d) })
.enter().append("svg:circle")
.attr("class", "control")
.attr("r", 7)
.attr("cx", x)
.attr("cy", y)
.call(d3.behavior.drag()
.on("dragstart", function(d) {
this.__origin__ = [d.x, d.y];
})
.on("drag", function(d) {
d.x = Math.min(w, Math.max(0, this.__origin__[0] += d3.event.dx));
d.y = Math.min(h, Math.max(0, this.__origin__[1] += d3.event.dy));
bezier = {};
update();
vis.selectAll("circle.control")
.attr("cx", x)
.attr("cy", y);
})
.on("dragend", function() {
delete this.__origin__;
}));
vis.selectAll("text.controltext")
.data(function(d) { return points.slice(0, d); })
.enter().append("svg:text")
.attr("class", "controltext")
.attr("dx", "10px")
.attr("dy", ".4em")
.text(function(d, i) { return "P" + i });
update();
d3.select("input[type=checkbox]").on("change", function() {
if (!(stop = !this.checked)) {
last = 0;
d3.timer(start);
}
});
function start(elapsed) {
t = (t + (elapsed - last) / 5000) % 1;
last = elapsed;
update();
return stop;
}
function update() {
var interpolation = vis.selectAll("g")
.data(function(d) { return getLevels(d, t); });
interpolation.enter().append("svg:g")
.style("pointer-events", "none")
.style("fill", function(d, i) { return color(i); })
.style("stroke", function(d, i) { return color(i); });
var circle = interpolation.selectAll("circle")
.data(Object);
circle.enter().append("svg:circle")
.attr("r", 4);
circle
.attr("cx", x)
.attr("cy", y);
var path = interpolation.selectAll("path")
.data(function(d) { return [d]; });
path.enter().append("svg:path")
.attr("class", "line");
path.attr("d", line);
var curve = vis.selectAll("path.curve")
.data(getCurve);
curve.enter().append("svg:path")
.attr("class", "curve");
curve.attr("d", line);
vis.selectAll("text.controltext")
.attr("x", x)
.attr("y", y);
}
function interpolate(d, p) {
if (arguments.length < 2) p = t;
var r = [];
for (var i=1; i<d.length; i++) {
var d0 = d[i-1], d1 = d[i];
r.push({x: d0.x + (d1.x - d0.x) * p, y: d0.y + (d1.y - d0.y) * p});
}
return r;
}
function getLevels(d, t_) {
if (arguments.length < 2) t_ = t;
var x = [points.slice(0, d)];
for (var i=1; i<d; i++) {
x.push(interpolate(x[x.length-1], t_));
}
return x;
}
function getCurve(d) {
var curve = bezier[d];
if (!curve) {
curve = bezier[d] = [];
for (var t_=0; t_<=1; t_+=delta) {
var x = getLevels(d, t_);
curve.push(x[x.length-1][0]);
}
}
return [curve.slice(0, t / delta + 1)];
}
function x(d) { return d.x; }
function y(d) { return d.y; }
</script>
</body>
</html>