Default CORS request mode for tiles used in WebGL rendering

This commit is contained in:
Tim Schaub
2022-01-11 12:39:48 -07:00
parent 1f761d943f
commit e71a8b65e1
6 changed files with 25 additions and 5 deletions

View File

@@ -17,7 +17,6 @@ const imagery = new TileLayer({
attributions: attributions:
'<a href="https://www.maptiler.com/copyright/" target="_blank">&copy; MapTiler</a> ' + '<a href="https://www.maptiler.com/copyright/" target="_blank">&copy; MapTiler</a> ' +
'<a href="https://www.openstreetmap.org/copyright" target="_blank">&copy; OpenStreetMap contributors</a>', '<a href="https://www.openstreetmap.org/copyright" target="_blank">&copy; OpenStreetMap contributors</a>',
crossOrigin: '',
maxZoom: 20, maxZoom: 20,
}), }),
}); });

View File

@@ -33,7 +33,6 @@ const layer = new TileLayer({
'https://api.maptiler.com/tiles/terrain-rgb/{z}/{x}/{y}.png?key=' + key, 'https://api.maptiler.com/tiles/terrain-rgb/{z}/{x}/{y}.png?key=' + key,
tileSize: 512, tileSize: 512,
maxZoom: 12, maxZoom: 12,
crossOrigin: 'anonymous',
}), }),
style: { style: {
variables: { variables: {
@@ -56,7 +55,6 @@ const map = new Map({
source: new XYZ({ source: new XYZ({
url: 'https://api.maptiler.com/maps/streets/{z}/{x}/{y}.png?key=' + key, url: 'https://api.maptiler.com/maps/streets/{z}/{x}/{y}.png?key=' + key,
attributions: attributions, attributions: attributions,
crossOrigin: 'anonymous',
tileSize: 512, tileSize: 512,
maxZoom: 22, maxZoom: 22,
}), }),

View File

@@ -50,7 +50,6 @@ const shadedRelief = new TileLayer({
opacity: 0.3, opacity: 0.3,
source: new XYZ({ source: new XYZ({
url: 'https://{a-d}.tiles.mapbox.com/v3/aj.sf-dem/{z}/{x}/{y}.png', url: 'https://{a-d}.tiles.mapbox.com/v3/aj.sf-dem/{z}/{x}/{y}.png',
crossOrigin: 'anonymous',
}), }),
style: { style: {
variables: variables, variables: variables,

View File

@@ -22,7 +22,6 @@ const layer = new TileLayer({
variables: variables, variables: variables,
}, },
source: new XYZ({ source: new XYZ({
crossOrigin: 'anonymous', // TODO: determine if we can avoid this
attributions: attributions, attributions: attributions,
url: 'https://api.maptiler.com/tiles/satellite/{z}/{x}/{y}.jpg?key=' + key, url: 'https://api.maptiler.com/tiles/satellite/{z}/{x}/{y}.jpg?key=' + key,
maxZoom: 20, maxZoom: 20,

View File

@@ -175,6 +175,12 @@ class TileTexture extends EventTarget {
if (this.loaded) { if (this.loaded) {
this.uploadTile_(); this.uploadTile_();
} else { } else {
if (tile instanceof ImageTile) {
const image = tile.getImage();
if (image instanceof Image && !image.crossOrigin) {
image.crossOrigin = 'anonymous';
}
}
tile.addEventListener(EventType.CHANGE, this.handleTileChange_); tile.addEventListener(EventType.CHANGE, this.handleTileChange_);
} }
} }

View File

@@ -77,6 +77,25 @@ describe('ol/webgl/TileTexture', function () {
expect(tileTexture.loaded).to.be(true); expect(tileTexture.loaded).to.be(true);
}); });
it('sets anonymous cors mode for image tiles by default', function () {
const tile = new ImageTile([0, 0, 0], TileState.IDLE);
tileTexture.setTile(tile);
const image = tile.getImage();
expect(image.crossOrigin).to.be('anonymous');
});
it('resepects any existing cors mode', function () {
const tile = new ImageTile(
[0, 0, 0],
TileState.IDLE,
'https://example.com/tile.png',
'use-credentials'
);
tileTexture.setTile(tile);
const image = tile.getImage();
expect(image.crossOrigin).to.be('use-credentials');
});
it('registers and unregisters change listener', function () { it('registers and unregisters change listener', function () {
const tile = tileTexture.tile; const tile = tileTexture.tile;
expect(tile.getListeners('change').length).to.be(2); expect(tile.getListeners('change').length).to.be(2);