javascript - 如何避免标签在 JavaScript 中重叠?

标签 javascript css math 3d three.js

我已将图像纹理化为 3D 球体(如下所示),并且还添加了重叠的平面几何(标签)。如何使用 three.js 库分隔标签? enter image description here 2

labelBox.prototype.update = function()
{

camera.updateMatrixWorld();
this.position.project(camera);

var screenvector = new THREE.Vector3();
screenvector.copy(this.position);
oldlabelpos.push(screenvector);
//this.position.normalize();

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

var boundingRect = this.box.getBoundingClientRect();  //getBoundingClientRect() method returns the 
                                              //size of element (here it is box) and its position relative to viewport
//update the box overlays position
this.box.style.left = this.posx + 'px';
this.box.style.top = this.posy  + 'px';

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

labelBox.prototype.LabelUpdateFn = function()
{       
    //var labels =  

document.getElementById('MovingLabel1');//.getElementsByClassName('spritelabel'); //labels = $(root).find(".spritelabel");

    for(var k=0; k<oldlabelpos.length; k++)
        {
            this.reposition = new THREE.Vector3();
            this.reposition = convertlatlonToVec3(oldlabelpos[k].x, 
            oldlabelpos[k].y).multiplyScalar(radius);
            this.reposition.normalize();

            camera.updateMatrixWorld();
            this.reposition.unproject(camera);

            this.newposx = Math.round((this.reposition.x + 1)* 
            this.domElement.offsetWidth/2 + webglviewport.left) - this.posx;
            this.newposy = Math.round((1 - this.reposition.y)* 
            this.domElement.offsetHeight/2) - this.posy;

            var boundingRect = this.box.getBoundingClientRect();

            this.box.style.left = (this.newposx + boundingRect.width) + 'px';
            this.box.style.top = this.newposy + 'px';

            //$(this.box).css(left) = 

            this.occludeLabel(this.box, this.marker);
            //var updatedpos = {updateposx, updateposy};

            //newlabelpos.push(updatedpos);             
        }                       
}

最佳答案

如果您在 3D 中说“避免标签重叠”,您想要检查视口(viewport)的“裁剪”。 例如:看这个 article

关于javascript - 如何避免标签在 JavaScript 中重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39590214/

相关文章:

javascript - React-router-dom 重定向不起作用 - 显示错误

python - 您将如何实现除数函数?

javascript - 如何避免重复相同的功能以允许单击链接时使用修饰键?

javascript - 子主题后插件不工作

javascript - 是否有一种 Javascript 方法可以实现嵌套括号的连接?

javascript - 过滤十六进制颜色

python - 从 .html 文件读取内容的简单 Flask 应用程序。外部样式表被阻止?

html - 链接中带有 CSS 的垂直文本

python - 获得 'large' 错误,这些错误似乎太大而不能单独来自舍入

javascript - Math.abs() 限制小数位数