Minimize canvas size for rotated views

This commit is contained in:
Andreas Hocevar
2022-07-08 17:47:49 +02:00
parent 22dba410ad
commit 6ffbfa1a4a
2 changed files with 17 additions and 20 deletions

View File

@@ -168,18 +168,17 @@ class CanvasImageLayerRenderer extends CanvasLayerRenderer {
const viewState = frameState.viewState; const viewState = frameState.viewState;
const viewCenter = viewState.center; const viewCenter = viewState.center;
const viewResolution = viewState.resolution; const viewResolution = viewState.resolution;
const size = frameState.size;
const scale = const scale =
(pixelRatio * imageResolution) / (viewResolution * imagePixelRatio); (pixelRatio * imageResolution) / (viewResolution * imagePixelRatio);
let width = Math.round(size[0] * pixelRatio); const extent = frameState.extent;
let height = Math.round(size[1] * pixelRatio); const resolution = viewState.resolution;
const rotation = viewState.rotation; const rotation = viewState.rotation;
if (rotation) { // desired dimensions of the canvas in pixels
const size = Math.round(Math.sqrt(width * width + height * height)); const width = Math.round((getWidth(extent) / resolution) * imagePixelRatio);
width = size; const height = Math.round(
height = size; (getHeight(extent) / resolution) * imagePixelRatio
} );
// set forward and inverse pixel transforms // set forward and inverse pixel transforms
composeTransform( composeTransform(

View File

@@ -18,8 +18,10 @@ import {
containsCoordinate, containsCoordinate,
createEmpty, createEmpty,
equals, equals,
getHeight,
getIntersection, getIntersection,
getTopLeft, getTopLeft,
getWidth,
intersects, intersects,
} from '../../extent.js'; } from '../../extent.js';
import {cssOpacity} from '../../css.js'; import {cssOpacity} from '../../css.js';
@@ -263,6 +265,14 @@ class CanvasTileLayerRenderer extends CanvasLayerRenderer {
const tileResolution = tileGrid.getResolution(z); const tileResolution = tileGrid.getResolution(z);
let extent = frameState.extent; let extent = frameState.extent;
const resolution = frameState.viewState.resolution;
const tilePixelRatio = tileSource.getTilePixelRatio(pixelRatio);
// desired dimensions of the canvas in pixels
const width = Math.round((getWidth(extent) / resolution) * tilePixelRatio);
const height = Math.round(
(getHeight(extent) / resolution) * tilePixelRatio
);
const layerExtent = const layerExtent =
layerState.extent && fromUserExtent(layerState.extent, projection); layerState.extent && fromUserExtent(layerState.extent, projection);
if (layerExtent) { if (layerExtent) {
@@ -272,18 +282,6 @@ class CanvasTileLayerRenderer extends CanvasLayerRenderer {
); );
} }
const tilePixelRatio = tileSource.getTilePixelRatio(pixelRatio);
// desired dimensions of the canvas in pixels
let width = Math.round(frameState.size[0] * tilePixelRatio);
let height = Math.round(frameState.size[1] * tilePixelRatio);
if (rotation) {
const size = Math.round(Math.sqrt(width * width + height * height));
width = size;
height = size;
}
const dx = (tileResolution * width) / 2 / tilePixelRatio; const dx = (tileResolution * width) / 2 / tilePixelRatio;
const dy = (tileResolution * height) / 2 / tilePixelRatio; const dy = (tileResolution * height) / 2 / tilePixelRatio;
const canvasExtent = [ const canvasExtent = [