map.on('load', () => {
const tb = new Threebox(map, gl, {
defaultLights: true,
enableSelectingObjects: true,
enableTooltips: true,
enableHelpTooltips: true
});
map.addLayer({
id: 'custom-layer',
type: 'custom',
renderingMode: '3d',
onAdd: function(map, gl) {
// Create permanent label
const cityLabel = tb.label({
htmlElement: '<div class="city-label">San Francisco</div>',
cssClass: 'city-label',
alwaysVisible: true
});
cityLabel.setCoords([-122.4194, 37.7749, 100]);
tb.add(cityLabel);
// Load model with dynamic tooltip
tb.loadObj({
obj: '/models/building.glb',
type: 'gltf',
scale: 1,
units: 'meters'
}, (building) => {
building.setCoords([-122.4194, 37.7749, 0]);
tb.add(building);
// Add label
building.addLabel(
'<div class="building-name">Office Building</div>',
false, // not always visible
building.center,
1.0
);
// Show label and tooltip on hover
building.addEventListener('ObjectMouseOver', () => {
if (building.label) {
building.label.visible = true;
}
// Add dynamic tooltip
const info = `
<strong>Office Building</strong><br>
Height: ${building.modelHeight.toFixed(1)}m<br>
<em>Click for details</em>
`;
building.addTooltip(info, true);
building.tooltip.visible = true;
});
building.addEventListener('ObjectMouseOut', () => {
if (building.label && !building.selected) {
building.label.visible = false;
}
if (building.tooltip && !building.selected) {
building.tooltip.visible = false;
}
});
// Keep label visible when selected
building.addEventListener('SelectedChange', (e) => {
if (building.label) {
building.label.visible = e.detail.selected;
}
});
});
},
render: function(gl, matrix) {
tb.update();
}
});
});