diff --git a/docs/RENDERER-CONTRACT.md b/docs/RENDERER-CONTRACT.md index 3ff74eabb..5554b3fa3 100644 --- a/docs/RENDERER-CONTRACT.md +++ b/docs/RENDERER-CONTRACT.md @@ -352,7 +352,8 @@ leaves the base as it was; a later `setBase`, or disposal, aborts the load and the overtaken call changes nothing, resolving with `imagery: []` and `settled: false`), draws it as Web Mercator imagery under the base's other imagery (`src/render/cesium/vectorBase.js`, 256 px tiles to -level 20), and credits each source's attribution as plain text. The stub +level 20, transparent where the style paints no opaque background; a tile +whose request is cancelled, its layer removed, is not painted), and credits each source's attribution as plain text. The stub loads the style and paints nothing. The check `globe.base.vector` serves a style and a tile from data URLs and reads the drawn fill. diff --git a/src/render/cesium/vectorBase.js b/src/render/cesium/vectorBase.js index bdec1929c..24faa1faa 100644 --- a/src/render/cesium/vectorBase.js +++ b/src/render/cesium/vectorBase.js @@ -28,13 +28,36 @@ export function vectorBaseProvider(style) { rectangle: tilingScheme.rectangle, credit: undefined, errorEvent: new Cesium.Event(), - hasAlphaChannel: false, + // Where the style paints no opaque background the base below (or the + // globe's own colour) shows through, never black. + hasAlphaChannel: true, tileDiscardPolicy: undefined, proxy: undefined, getTileCredits: () => undefined, pickFeatures: () => undefined, - requestImage: (x, y, level) => - paintVectorTile(style, level, x, y, { size: VECTOR_TILE_PX }), + // A tile whose request was cancelled (its layer removed) is not + // painted once its source tiles arrive. + requestImage: (x, y, level, request) => + paintVectorTile(style, level, x, y, { + size: VECTOR_TILE_PX, + signal: cancelledBy(request), + }), + }; +} + +/** + * An `AbortSignal`-like view of a Cesium request: aborted once the request + * is cancelled. Undefined without a request. + */ +export function cancelledBy(request) { + if (!request) return undefined; + return { + get aborted() { + return request.cancelled === true; + }, + get reason() { + return new DOMException('The tile request was cancelled', 'AbortError'); + }, }; } diff --git a/src/render/cesium/vectorBase.test.mjs b/src/render/cesium/vectorBase.test.mjs index 5618c8dd2..1471f2a3f 100644 --- a/src/render/cesium/vectorBase.test.mjs +++ b/src/render/cesium/vectorBase.test.mjs @@ -16,10 +16,23 @@ test('the vector base is Web Mercator imagery painted from its style', () => { }); assert.ok(provider.tilingScheme instanceof Cesium.WebMercatorTilingScheme); assert.equal(provider.tileWidth, VECTOR_TILE_PX); - assert.equal(provider.hasAlphaChannel, false); + // A style with no (or a translucent) background is transparent there. + assert.equal(provider.hasAlphaChannel, true); assert.equal(typeof provider.requestImage, 'function'); }); +test('a tile whose Cesium request was cancelled is not painted', async () => { + const provider = vectorBaseProvider({ + layers: [], + sources: {}, + tiles: new Map(), + }); + await assert.rejects( + provider.requestImage(0, 0, 0, { cancelled: true }), + (error) => error.name === 'AbortError', + ); +}); + test('a source’s attribution is credited as plain text', () => { assert.equal( attributionText(