-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_motion.html
More file actions
107 lines (78 loc) · 2 KB
/
Copy pathexample_motion.html
File metadata and controls
107 lines (78 loc) · 2 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<!DOCTYPE html>
<html lang="en">
<style>
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
.circle {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
</style>
<head>
<title>Test</title>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://d3js.org/d3-timer.v1.min.js"></script>
</head>
<body>
<!-- Page elements and content go here. -->
<body>
<script>
// SETUP
var x1 = [2];
var history = [];
var dat2 = [10,10,10,10,10,10,10,10,10,10,10];
var w = 1000;
var h = 400;
var drawFlag = true
var frameRate = 100
var xMax = d3.max(x1, function(d) { return d;} );
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h)
.attr("style", "outline: thin solid black;");
var points = svg.selectAll("circle")
.data(x1)
.enter()
.append("circle")
.attr("cx", function(d) {return w-20; })
.attr("cy", function(d) {return 50.;})
.attr("r", 4);
var lineFunction = d3.svg.line()
.x(function(d) { return d[0]; })
.y(function(d) { return d[1]; })
.interpolate("linear");
var path = svg.append("path")
.attr("d", lineFunction(history))
.attr("stroke", "black")
.attr("stroke-width", 2)
.attr("fill", "none");
// ANIMATE
var anim = d3.interval(function (elapsed) {if (drawFlag) {draw(elapsed);}},frameRate);
function draw(elapsed) {
// Take step in simulations
//x1.forEach(function(d) {
// d = d + Math.floor(Math.random() *h)});
x1[0]=x1[0] + (Math.random()-0.5)*50 ;
history.push([w-20,x1[0]+100])
for(var i=0;i<history.length-1;i++){
history[i][0]=history[i][0] - 10 ;
}
console.log(history)
// update point data
points.data(x1);
// animate step
points
.transition()
.duration(frameRate/5.)
.ease("linear")
.attr("cy", function(d) {return d+100;})
path.transition().duration(frameRate).attr("d", lineFunction(history))
}
</script>
</body>
</html>