Merge pull request #10083 from MoonE/example-webgl

webgl points layer example fixes
This commit is contained in:
Olivier Guyot
2019-10-03 17:43:22 +02:00
committed by GitHub
4 changed files with 48 additions and 48 deletions

View File

@@ -241,10 +241,6 @@ Support for the `OES_element_index_uint` WebGL extension is mandatory for WebGL
Layer opacity must be a number.
### 65
A symbol literal representation must be defined on the style supplied to a `WebGLPointsLayer` instance.
### 66
`forEachFeatureAtCoordinate` cannot be used on a WebGL layer if the hit detection logic has not been enabled.

View File

@@ -24,15 +24,13 @@ experimental: true
<div id="map" class="map"></div>
<label>Choose a predefined style from the list below or edit it as JSON manually.</label><br>
<select id="style-select">
<option>Predefined styles</option>
<option value="icons">Icons</option>
<option value="triangles">Triangles, color related to population</option>
<option value="circles">Circles, size related to population</option>
</select>
<textarea style="width: 100%; height: 20rem; font-family: monospace; font-size: small;" id="style-editor"></textarea>
<small id="style-valid" style="display: none; color: forestgreen">
✓ style is valid
</small>
<small id="style-invalid" style="display: none; color: grey">
✗ style not yet valid...
<small>
<span id="style-valid" style="display: none; color: forestgreen">✓ style is valid</span>
<span id="style-invalid" style="display: none; color: grey"><span>style not yet valid...</span></span>
&nbsp;
</small>

View File

@@ -46,8 +46,6 @@ const predefinedStyles = {
}
}
};
let literalStyle = predefinedStyles['circles'];
let pointsLayer;
const map = new Map({
layers: [
@@ -62,45 +60,57 @@ const map = new Map({
})
});
const editor = document.getElementById('style-editor');
editor.value = JSON.stringify(literalStyle, null, 2);
function refreshLayer() {
if (pointsLayer) {
map.removeLayer(pointsLayer);
}
let literalStyle;
let pointsLayer;
function refreshLayer(newStyle) {
const previousLayer = pointsLayer;
pointsLayer = new WebGLPointsLayer({
source: vectorSource,
style: literalStyle
style: newStyle
});
map.addLayer(pointsLayer);
if (previousLayer) {
map.removeLayer(previousLayer);
}
literalStyle = newStyle;
}
function setStyleStatus(valid) {
document.getElementById('style-valid').style.display = valid ? 'initial' : 'none';
document.getElementById('style-invalid').style.display = !valid ? 'initial' : 'none';
const spanValid = document.getElementById('style-valid');
const spanInvalid = document.getElementById('style-invalid');
function setStyleStatus(errorMsg) {
const isError = typeof errorMsg === 'string';
spanValid.style.display = errorMsg === null ? 'initial' : 'none';
spanInvalid.firstElementChild.innerText = isError ? errorMsg : '';
spanInvalid.style.display = isError ? 'initial' : 'none';
}
const editor = document.getElementById('style-editor');
editor.addEventListener('input', function() {
const textStyle = editor.value;
if (JSON.stringify(JSON.parse(textStyle)) === JSON.stringify(literalStyle)) {
return;
}
try {
literalStyle = JSON.parse(textStyle);
refreshLayer();
setStyleStatus(true);
const newLiteralStyle = JSON.parse(textStyle);
if (JSON.stringify(newLiteralStyle) !== JSON.stringify(literalStyle)) {
refreshLayer(newLiteralStyle);
}
setStyleStatus(null);
} catch (e) {
setStyleStatus(false);
setStyleStatus(e.message);
}
});
refreshLayer();
const select = document.getElementById('style-select');
select.addEventListener('change', function() {
select.value = 'circles';
function onSelectChange() {
const style = select.value;
literalStyle = predefinedStyles[style];
editor.value = JSON.stringify(literalStyle, null, 2);
refreshLayer();
});
const newLiteralStyle = predefinedStyles[style];
editor.value = JSON.stringify(newLiteralStyle, null, 2);
try {
refreshLayer(newLiteralStyle);
setStyleStatus();
} catch (e) {
setStyleStatus(e.message);
}
}
onSelectChange();
select.addEventListener('change', onSelectChange);

View File

@@ -4,7 +4,6 @@
import {assign} from '../obj.js';
import WebGLPointsLayerRenderer from '../renderer/webgl/PointsLayer.js';
import {getSymbolFragmentShader, getSymbolVertexShader, parseSymbolStyle} from '../webgl/ShaderBuilder.js';
import {assert} from '../asserts.js';
import Layer from './Layer.js';
@@ -72,24 +71,21 @@ class WebGLPointsLayer extends Layer {
super(baseOptions);
/**
* @type {import('../style/LiteralStyle.js').LiteralStyle}
* @private
* @type {import('../webgl/ShaderBuilder.js').StyleParseResult}
*/
this.style = options.style;
assert(this.style.symbol !== undefined, 65);
this.parseResult_ = parseSymbolStyle(options.style.symbol);
}
/**
* @inheritDoc
*/
createRenderer() {
const parseResult = parseSymbolStyle(this.style.symbol);
return new WebGLPointsLayerRenderer(this, {
vertexShader: getSymbolVertexShader(parseResult.params),
fragmentShader: getSymbolFragmentShader(parseResult.params),
uniforms: parseResult.uniforms,
attributes: parseResult.attributes
vertexShader: getSymbolVertexShader(this.parseResult_.params),
fragmentShader: getSymbolFragmentShader(this.parseResult_.params),
uniforms: this.parseResult_.uniforms,
attributes: this.parseResult_.attributes
});
}
}