Skip to content

Commit

Permalink
Add documentation of the public facing fog APIs + revise defaults (#1…
Browse files Browse the repository at this point in the history
…0545)

* Add documentation of the public facing APIs

* Fixup missing `}`

* Update fog range docs

* Disable haze by default and fix mixed up haze properties

* Update src/style-spec/reference/v8.json

Co-authored-by: Ricky Reusser <[email protected]>

* Update src/style-spec/reference/v8.json

Co-authored-by: Ricky Reusser <[email protected]>

* Update src/style-spec/reference/v8.json

Co-authored-by: Ricky Reusser <[email protected]>

* Update src/style-spec/reference/v8.json

Co-authored-by: Ricky Reusser <[email protected]>

* Update src/ui/map.js

Co-authored-by: Ricky Reusser <[email protected]>

Co-authored-by: Ricky Reusser <[email protected]>
  • Loading branch information
karimnaaji and rreusser committed May 6, 2021
1 parent 3534fa5 commit c0b1dc9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 45 deletions.
6 changes: 3 additions & 3 deletions debug/fog.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
this.enableSky = true;
this.terrainExaggeration = 1.5;
this.style = 'satellite-streets-v11';
this.fogRangeStart = 1.5;
this.fogRangeEnd = 2.0;
this.fogRangeStart = 2;
this.fogRangeEnd = 12.0;
this.fogColor = [255, 255, 255];
this.fogHazeColor = [109, 123, 180];
this.fogHazeEnergy = 1.0;
this.fogHazeEnergy = 0.8;
this.fogStrength = 1.0;
this.fogSkyBlend = 0.1;
this.showTileBoundaries = false;
Expand Down
56 changes: 19 additions & 37 deletions src/style-spec/reference/v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
},
"fog": {
"type": "fog",
"doc": ""
"doc": "A global effect that fades layers and markers based on their distance to the camera. The fog can be used to approximate the effect of atmosphere on distant objects and enhance the depth perception of the map when used with terrain or 3D features."
},
"sources": {
"required": true,
Expand Down Expand Up @@ -3689,8 +3689,8 @@
"range": {
"type": "array",
"default": [
5,
10
2,
12
],
"minimum": 0,
"maximum": 20,
Expand All @@ -3704,17 +3704,14 @@
"zoom"
]
},
"doc": "",
"doc": "The near and far limits of the fog layer. Units are relative to the map height, so that 2.0 corresponds to twice the map height, in pixels.",
"example": [
5,
10
2,
12
],
"sdk-support": {
"basic functionality": {
"js": "",
"android": "",
"ios": "",
"macos": ""
"js": "2.3.0"
}
}
},
Expand All @@ -3729,13 +3726,10 @@
]
},
"transition": true,
"doc": "",
"doc": "The color of the fog. If provided as an `rgba` color, the alpha component is unused.",
"sdk-support": {
"basic functionality": {
"js": "",
"android": "",
"ios": "",
"macos": ""
"js": "2.3.0"
}
}
},
Expand All @@ -3750,13 +3744,10 @@
]
},
"transition": true,
"doc": "",
"doc": "The color of the haze component of the fog, used only when haze energy is nonzero. If provided as an `rgba` color, the alpha component is unused.",
"sdk-support": {
"basic functionality": {
"js": "",
"android": "",
"ios": "",
"macos": ""
"js": "2.3.0"
}
}
},
Expand All @@ -3772,20 +3763,17 @@
]
},
"transition": true,
"doc": "",
"doc": "Controls the strength of the fog effect. Reducing the fog strength pushes the fog farther toward the horizon, leaving more visible space for haze, if enabled. When haze energy is zero, this will usually be 1.0.",
"sdk-support": {
"basic functionality": {
"js": "",
"android": "",
"ios": "",
"macos": ""
"js": "2.3.0"
}
}
},
"haze-energy": {
"type": "number",
"property-type": "data-constant",
"default": 1.0,
"default": 0.0,
"minimum": 0.0,
"expression": {
"interpolated": true,
Expand All @@ -3794,20 +3782,17 @@
]
},
"transition": true,
"doc": "",
"doc": "Controls the strength of haze in the fog layer. Haze is an additive effect which approximates the look of an atmosphere. Increasing this strengthens the haze. Reducing the fog strength pushes fog toward the horizon, making haze visible.",
"sdk-support": {
"basic functionality": {
"js": "",
"android": "",
"ios": "",
"macos": ""
"js": "2.3.0"
}
}
},
"sky-blend": {
"type": "number",
"property-type": "data-constant",
"default": 1,
"default": 0.1,
"minimum": 0,
"maximum": 1,
"expression": {
Expand All @@ -3817,13 +3802,10 @@
]
},
"transition": true,
"doc": "",
"doc": "The sky blend allows to blend the effect of the fog above the horizon line. A value of 1 will entirely blend the fog onto the sky, while a value of 0 will leave the sky unaffected by fog.",
"sdk-support": {
"basic functionality": {
"js": "",
"android": "",
"ios": "",
"macos": ""
"js": "2.3.0"
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/style/fog.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ type Props = {|
"sky-blend": DataConstantProperty<number>,
|};

const properties: Properties<Props> = new Properties({
const fogProperties: Properties<Props> = new Properties({
"range": new DataConstantProperty(styleSpec.fog.range),
"color": new DataConstantProperty(styleSpec.fog.color),
"haze-color": new DataConstantProperty(styleSpec.fog["haze-color"]),
"haze-energy": new DataConstantProperty(styleSpec.fog["strength"]),
"strength": new DataConstantProperty(styleSpec.fog["haze-energy"]),
"haze-energy": new DataConstantProperty(styleSpec.fog["haze-energy"]),
"strength": new DataConstantProperty(styleSpec.fog["strength"]),
"sky-blend": new DataConstantProperty(styleSpec.fog["sky-blend"]),
});

Expand All @@ -41,7 +41,7 @@ class Fog extends Evented {

constructor(fogOptions?: FogSpecification) {
super();
this._transitionable = new Transitionable(properties);
this._transitionable = new Transitionable(fogProperties);
this.set(fogOptions);
this._transitioning = this._transitionable.untransitioned();
}
Expand Down
32 changes: 31 additions & 1 deletion src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -2175,17 +2175,47 @@ class Map extends Camera {
return this.style ? this.style.getTerrain() : null;
}

/**
* Sets the fog property of the style.
* @param fog The fog properties to set. Must conform the [Mapbox Style Specification](https://docs.mapbox.com/mapbox-gl-js/style-spec/root/#fog).
* If `null` or `undefined` is provided, this function call removes the fog from the map.
* @returns {Map} `this`
* @example
* map.setFog({
* "range": [1.0, 12.0],
* "color": 'white',
* "haze-color": 'rgba(109, 123, 180, 1.0)',
* "strength": 1.0,
* "haze-energy": 1.0,
* "sky-blend": 0.1
* });
*/
setFog(fog: FogSpecification) {
this._lazyInitEmptyStyle();
this.style.setFog(fog);
return this._update(true);
}

// NOTE: Make fog non-optional
/**
* Returns the fog specification or `null` if fog is not set on the map.
*
* @returns {Object} fog Fog specification properties of the style.
*/
getFog(): Fog | null {
return this.style ? this.style.getFog() : null;
}

/**
* Returns the fog opacity for a given geolocation.
*
* An opacity of 0 means that there is no fog contribution for the given geolocation
* while a fog opacity of 1.0 means the geolocation is fully obscured by the fog effect.
*
* If there is no fog set on the map, this function will simply return 0.
*
* @param {LngLatLike} lnglat The geographical location to evaluate the fog on.
* @returns {number} A value between 0 and 1 representing the fog opacity, where 1 means fully within, and 0 means not affected by the fog effect.
*/
getFogOpacity(lnglat: LngLatLike): number {
if (!this.style || !this.style.fog) return 0.0;
return this.style.fog.getOpacityAtLatLng(LngLat.convert(lnglat), this.transform);
Expand Down

0 comments on commit c0b1dc9

Please sign in to comment.