Clip tile layers by extent
This commit is contained in:
@@ -85,6 +85,39 @@ class CanvasLayerRenderer extends LayerRenderer {
|
||||
rotateAtOffset(context, rotation, halfWidth, halfHeight);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {CanvasRenderingContext2D} context Context.
|
||||
* @param {import("../../PluggableMap.js").FrameState} frameState Frame state.
|
||||
* @param {import("../../extent.js").Extent} extent Clip extent.
|
||||
* @protected
|
||||
*/
|
||||
clipUnrotated(context, frameState, extent) {
|
||||
const topLeft = getTopLeft(extent);
|
||||
const topRight = getTopRight(extent);
|
||||
const bottomRight = getBottomRight(extent);
|
||||
const bottomLeft = getBottomLeft(extent);
|
||||
|
||||
applyTransform(frameState.coordinateToPixelTransform, topLeft);
|
||||
applyTransform(frameState.coordinateToPixelTransform, topRight);
|
||||
applyTransform(frameState.coordinateToPixelTransform, bottomRight);
|
||||
applyTransform(frameState.coordinateToPixelTransform, bottomLeft);
|
||||
|
||||
const inverted = invertTransform(this.pixelTransform_.slice());
|
||||
|
||||
applyTransform(inverted, topLeft);
|
||||
applyTransform(inverted, topRight);
|
||||
applyTransform(inverted, bottomRight);
|
||||
applyTransform(inverted, bottomLeft);
|
||||
|
||||
context.save();
|
||||
context.beginPath();
|
||||
context.moveTo(Math.round(topLeft[0]), Math.round(topLeft[1]));
|
||||
context.lineTo(Math.round(topRight[0]), Math.round(topRight[1]));
|
||||
context.lineTo(Math.round(bottomRight[0]), Math.round(bottomRight[1]));
|
||||
context.lineTo(Math.round(bottomLeft[0]), Math.round(bottomLeft[1]));
|
||||
context.clip();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import("../../render/EventType.js").default} type Event type.
|
||||
* @param {CanvasRenderingContext2D} context Context.
|
||||
|
||||
@@ -143,12 +143,10 @@ class CanvasTileLayerRenderer extends CanvasLayerRenderer {
|
||||
const tileResolution = tileGrid.getResolution(z);
|
||||
let extent = frameState.extent;
|
||||
|
||||
if (layerState.extent !== undefined) {
|
||||
if (layerState.extent) {
|
||||
extent = getIntersection(extent, layerState.extent);
|
||||
}
|
||||
|
||||
// TODO: clip by layer extent
|
||||
|
||||
const tilePixelRatio = tileSource.getTilePixelRatio(pixelRatio);
|
||||
|
||||
// desired dimensions of the canvas in pixels
|
||||
@@ -235,6 +233,10 @@ class CanvasTileLayerRenderer extends CanvasLayerRenderer {
|
||||
context.clearRect(0, 0, width, height);
|
||||
}
|
||||
|
||||
if (layerState.extent) {
|
||||
this.clipUnrotated(context, frameState, layerState.extent);
|
||||
}
|
||||
|
||||
this.preRender(context, frameState, pixelTransform);
|
||||
|
||||
this.renderedTiles.length = 0;
|
||||
@@ -285,6 +287,10 @@ class CanvasTileLayerRenderer extends CanvasLayerRenderer {
|
||||
|
||||
this.postRender(context, frameState, pixelTransform);
|
||||
|
||||
if (layerState.extent) {
|
||||
context.restore();
|
||||
}
|
||||
|
||||
const opacity = layerState.opacity;
|
||||
if (opacity !== canvas.style.opacity) {
|
||||
canvas.style.opacity = opacity;
|
||||
|
||||
Reference in New Issue
Block a user