-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlabels.js
64 lines (56 loc) · 1.74 KB
/
labels.js
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
var THREE = require('three');
var Labels = {
projector: new THREE.Projector(),
toXYCoords: function(pos, camera) {
var vector = pos.clone();
this.projector.projectVector(vector, camera);
vector.x = (vector.x + 1)/2 * window.innerWidth;
vector.y = -(vector.y - 1)/2 * window.innerHeight;
return vector;
},
labels: [],
updateLabels: function(camera, intersects){
var that = this;
this.labels.forEach(function(item){
var newPos = that.toXYCoords(item.v, camera);
item.text.style.top = newPos.y + 'px';
item.text.style.left = newPos.x + 'px';
if(newPos.z > 1.0){
item.text.style.display = 'none';
} else {
item.text.style.display = 'block';
}
});
},
addLabel: function(vector, name, description, type, append){
var l = {};
var text2 = document.createElement('div');
if( description ){
text2.className = "label described "+type;
text2.innerHTML = "<h3>"+name+"</h3><p class='description'>"+description+"</p>";
text2.onmousedown = function(e){
var positiveSelect = this.className.indexOf("selected") === -1;
Array.prototype.forEach.call(document.getElementsByClassName("selected"), function(el) {
el.className = el.className.substring(0,el.className.length - 9);
});
if( positiveSelect ){
this.className = this.className + " selected";
} else {
this.className = this.className.split(" ").filter(function(clazz){ return clazz !== "selected"; }).join(" ");
}
e.stopPropagation();
};
} else {
text2.className = "label";
text2.innerHTML = "<h3>"+name+"</h3>";
}
l.text = text2;
l.v = vector;
this.labels.push(l);
if(append !== false){
document.getElementById("container").appendChild(l.text);
}
return l.text;
}
};
module.exports = Labels;