Merge pull request #13293 from ahocevar/setstyle-reset-variables

Re-assign style variables on setStyle()
This commit is contained in:
Andreas Hocevar
2022-01-28 15:53:34 +01:00
committed by GitHub
2 changed files with 47 additions and 1 deletions

View File

@@ -456,10 +456,13 @@ class WebGLTileLayer extends BaseTileLayer {
/**
* Update the layer style. The `updateStyleVariables` function is a more efficient
* way to update layer rendering. In cases where the whole style needs to be updated,
* this method may be called instead.
* this method may be called instead. Note that calling this method will also replace
* any previously set variables, so the new style also needs to include new variables,
* if needed.
* @param {Style} style The new style.
*/
setStyle(style) {
this.styleVariables_ = style.variables || {};
this.style_ = style;
const parsedStyle = parseStyle(this.style_, this.getSourceBandCount_());
const renderer = this.getRenderer();

View File

@@ -255,6 +255,49 @@ describe('ol/layer/WebGLTile', function () {
layer.updateStyleVariables({foo: 'bam'});
expect(layer.styleVariables_.foo).to.be('bam');
});
it('also works after setStyle()', function (done) {
const layer = new WebGLTileLayer({
className: 'testlayer2',
source: new DataTileSource({
loader(z, x, y) {
return new Promise((resolve) => {
resolve(new ImageData(256, 256));
});
},
}),
});
map.addLayer(layer);
layer.setStyle({
variables: {
r: 0,
g: 255,
b: 0,
},
color: ['color', ['var', 'r'], ['var', 'g'], ['var', 'b']],
});
map.renderSync();
layer.updateStyleVariables({
r: 255,
g: 0,
b: 255,
});
expect(layer.styleVariables_['r']).to.be(255);
const targetContext = createCanvasContext2D(100, 100);
layer.on('postrender', () => {
targetContext.clearRect(0, 0, 100, 100);
targetContext.drawImage(target.querySelector('.testlayer2'), 0, 0);
});
map.once('rendercomplete', () => {
expect(Array.from(targetContext.getImageData(0, 0, 1, 1).data)).to.eql([
255, 0, 255, 255,
]);
done();
});
});
});
it('dispatches a precompose event with WebGL context', (done) => {