-
Notifications
You must be signed in to change notification settings - Fork 9
/
pointables.html
130 lines (121 loc) · 3.31 KB
/
pointables.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
<!DOCTYPE html>
<meta charset="utf-8">
<title>Pointables</title>
<style>
body {
width: 980px;
margin: 20px auto;
font-size: 15px;
font-family: sans-serif;
text-align: center;
}
svg {
width:980px;
height: 500px;
position: absolute;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis .label {
font-weight: bold;
font-size: 1.5em;
}
</style>
<script src="js/lib/d3.v3.js"></script>
<script src="js/lib/leap.js"></script>
<h1>Pointables</h1>
<svg></svg>
<canvas id="fingers" height=500 width=980></canvas>
<p>
Black Dots - X/Y position<br/>
Black Dot Size - Distance from [0,*,0] (doesn't use Y position)<br/>
Gray bars - Pointable length<br/>
Numbers - Pointable id
</p>
<script>
var margin = { top: 20, right: 20, bottom: 20, left: 40 };
var width = 980 - margin.right - margin.left;
var height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear().range([0,width]).domain([-200,200]);
var y = d3.scale.linear().range([height,0]).domain([0,400]);
var xAxis = d3.svg.axis().scale(x).orient("bottom");
var yAxis = d3.svg.axis().scale(y).orient("left");
var svg = d3.select("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
// Tick marks
svg
.append("g")
.attr("transform", "translate(0," + height + ")")
.attr("class", "x axis")
.call(xAxis);
svg
.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("X");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Y")
// most active location above leap sensor
var center = [0,150,0];
// connect leap motion
var controller = new Leap.Controller();
controller.connect();
controller.onFrame(function() {
var obj = controller.frame();
fingers(obj.pointables);
});
// finger lengths
var canvas = document.getElementById("fingers");
canvas.width = width;
canvas.height = height;
d3.select("canvas")
.style("margin-top", margin.top + "px")
.style("margin-right", margin.right + "px")
.style("margin-bottom", margin.bottom + "px")
.style("margin-left", margin.left + "px");
var ctx = canvas.getContext("2d");
ctx.textAlign = "center";
ctx.textBaseline = "bottom";
ctx.font = "18pt sans-serif";
function fingers(pointables) {
ctx.clearRect(0,0,width,height);
pointables.map(function(p) {
var pos = p.tipPosition;
var dist = 900/Math.sqrt( Math.pow(pos[0]-center[0], 2) +
Math.pow(pos[2]-center[2], 2) + 0.00001 );
var r = d3.min([dist, 30]);
// finger lengths
ctx.fillStyle = "rgba(0,0,0,0.4)";
ctx.fillRect(x(pos[0])-r/2,height,r,-2*p.length);
ctx.fillText(p.length.toPrecision(2), x(pos[0]), height-2*p.length-r/2);
// dots
ctx.fillStyle = "rgba(0,0,0,0.8)";
ctx.fillRect(x(pos[0])-r/2, y(pos[1])-r/2, r, r);
ctx.fillText(p.id, x(pos[0]), y(pos[1]) - r);
});
};
</script>