COMMUNITY

Cesium Library with Haxe

Hello,
did anyone here create a Cesium ( http://Cesiumjs.org ) based or Cesium enhanced Javascript App?
I am trying to set it up but it seems that I get lost in being new to Cesium and Haxe …
Thanks for any help!

From the tutorial it looks usable with Haxe, though there are a few gotchas no matter whether you write externs or try to go dynamic.

For quick and dirty you can use Dynamic;

// get a global reference as `Dynamic`
var Cesium = (js.Browser.window : Dynamic).Cesium;
// now you can access simple variables and functions like:
Cesium.Ion.defaultAccessToken = 'your_access_token';

// however to "construct" an object you need the `js.Syntax` escape route:
var viewer = js.Syntax.construct('Cesium.Viewer', 'cesiumContainer', {
    terrainProvider: Cesium.createWorldTerrain()
});

This produces the following JS:

var Cesium = window.Cesium;
Cesium.Ion.defaultAccessToken = "your_access_token";
var viewer = new Cesium.Viewer("cesiumContainer",{ terrainProvider : Cesium.createWorldTerrain()});

You can write that until you decide to go further and write externs, which can take more time:

If you do that there is a class gotcha: the Cesium API exposes functions (Cesium.createWorldTerrain) and classes (Cesium.Viewer) which can’t be expressed directly in Haxe so it would be something like:

package cesium;

@:extern @:native('Cesium')
class Cesium {
    static public function createWorldTerrain(?options): cesium.CesiumTerrainProvider; // or just Dynamic
    ...
}

@:extern @:native('Cesium.Viewer')
class Viewer {
     public function new(dom: haxe.extern.EitherType<String, js.html.HtmlElement>, ?options);
     ...
}

which you would use like:

var viewer = new cesium.Viewer("cesiumContainer", {
    terrainProvider : cesium.Cesium.createWorldTerrain()
});

If you’re enthusiatic about it, I also see the Cesium is opensource and has extensive jsdoc coverage, so you may want to try automating the externs production for instance with a tool like https://github.com/hexonaut/js2hx or https://github.com/jdonaldson/jsdoc3-hxtern - though those tools are old/incomplete they could be a good start to get most of the externs scaffolded.

Thanks a lot Philippe,
that really gave me an insight.
However since I do not feel too enthusiastic at the moment I’d rather skip the externs :slight_smile:
For now I guess I will finish the project much quicker in javascript directly.