2016-09-26 62 views
0

我無法理解代碼中這些行的執行情況。無法理解JavaScript中的以下代碼行

this.screenVector = new THREE.Vector3(0,0,0); this.screenVector.copy(this.position);
this.screenVector.project(camera); 項目(相機)的含義與其所做的一樣,以及screenVector的含義。

function labelBox(Ncardinal, radius, domElement) 
    { 
    this.screenVector = new THREE.Vector3(0, 0, 0); 
    this.position = convertlatlonToVec3(Ncardinal.lat,Ncardinal.lon).multiplyScalar(radius); 
    this.box = document.createElement('div'); 
    a = document.createElement('a'); 
    a.innerHTML = Ncardinal.name; 
    a.href ='http://www.google.de'; 
    this.box.className = "spritelabel"; 
    this.box.appendChild(a); 

    this.domElement = domElement; 
    this.domElement.appendChild(this.box); 
    } 

labelBox.prototype.update = function() 
{ 
this.screenVector.copy(this.position); 
this.screenVector.project(camera); 

var posx = Math.round((this.screenVector.x + 1)* this.domElement.offsetWidth/2); 
var posy = Math.round((1 - this.screenVector.y)* this.domElement.offsetHeight/2); 

var boundingRect = this.box.getBoundingClientRect(); 

//update the box overlays position 
this.box.style.left = (posx - boundingRect.width) + 'px'; 
this.box.style.top = posy + 'px'; 

this.occludeLabel(this.box, this.marker); 

}; 
+0

投影向量以屏幕空間。要做到這一點,你需要知道相機參數。 – gaitat

+0

請參閱http://stackoverflow.com/questions/27409074/three-js-converting-3d-position-to-2d-screen-position-r69 – WestLangley

+0

此文檔是https://threejs.org/docs/ #Reference/Math/Vector3它從世界座標向屏幕座標投影,修改它所調用的矢量。 –

回答