Hi,
- I can't make a complete working example now, I'll try to illustrate key things.
- Please take into account that I'm new to web development.
- My element works in narrow range of heights and distances, I hide it when it's out of the range, so I didn't need to think about a number of interesting things such as crossing the horizon.
Feel free to ask, I will try to share more details if necessary.
1. The element below will be our custom billboard.
In html section,
<div id="myBillboard">
<!--
Any html billboard content here (I call this element "billboard").
In my case, here is html with placeholders which will be replaced
by real data when necessary.
-->
</div>
The <div> is positioned over Cesium canvas. Fragment of css for it:
#myBillboard {
position: absolute;
z-index: 2; /* it must be higher then canvas */
background-image: url('images/bb-bk.png' ); /* in my case this is a balloon-shaped semi-transparent PNG */
background-repeat: no-repeat;
display: none; /* initially hidden */
pointer-events: none; /* transparent for mouse events */
/* ... */
}
2. Jacascript. Somewhere close to the beginning of code
var bb = document.getElementById('myBillboard'); // bb is my billboard
// position in world (Cartesian3) coordinates.
bb.position = Cesium.Cartesian3.fromDegrees(50, 50, 0, ellipsoid);
bb.scrx = -99; // screen coords. initialized by "impossible" value
bb.scry = -99;
// Above parameters: position, scrx and scry were added to the object for futher use. position
// can be changed at any moment
3. We need to correctly place our bb on the screen when camera changes. AFAIK Cesium API doesn't provide special camera tracking events, I use timer for this.
setInterval(function()
{
// transfer bb.pos (world coord) to screen coord.
var newScr = Cesium.SceneTransforms.wgs84ToWindowCoordinates(scene, bb.pos);
// round off (floor())
newScr.x |= 0;
newScr.y |= 0;
// work only if something changed
if ( bb.scrx != newScr.x || bb.scry != newScr.y )
{
bb.scrx = newScr.x;
bb.scry = newScr.y;
// move the bb.
// Our billboard has a hot spot which points exactly to its world position (bb.pos) on the screen.
// We set screen coordinates of bb's left-top corner and have to calculate them.
// bbWidth and bbHeight are numerical constants in my case, I set them explicitly at start.
// In my case bb has a baloon-like semi-transparent background image. Its hotspot is in the
// middle of bottom edge of the element's rectangle:
bb.style.left = (bb.scrx - bbWidth / 2) + 'px';
bb.style.top = (bb.scry - bbHeight) + 'px';
}
,50
});
4. To turn bb visiblility on: bb.style.display = 'block';
and off: bb.style.display = 'none';