Don't compute value more that once

This commit is contained in:
Frederic Junod
2018-11-12 11:38:20 +01:00
parent 647421f07b
commit 5eed22286b
2 changed files with 12 additions and 14 deletions

View File

@@ -45,8 +45,8 @@ class CanvasLayerRenderer extends LayerRenderer {
*/ */
clip(context, frameState, extent) { clip(context, frameState, extent) {
const pixelRatio = frameState.pixelRatio; const pixelRatio = frameState.pixelRatio;
const width = frameState.size[0] * pixelRatio; const halfWidth = (frameState.size[0] * pixelRatio) / 2;
const height = frameState.size[1] * pixelRatio; const halfHeight = (frameState.size[1] * pixelRatio) / 2;
const rotation = frameState.viewState.rotation; const rotation = frameState.viewState.rotation;
const topLeft = getTopLeft(extent); const topLeft = getTopLeft(extent);
const topRight = getTopRight(extent); const topRight = getTopRight(extent);
@@ -59,14 +59,14 @@ class CanvasLayerRenderer extends LayerRenderer {
applyTransform(frameState.coordinateToPixelTransform, bottomLeft); applyTransform(frameState.coordinateToPixelTransform, bottomLeft);
context.save(); context.save();
rotateAtOffset(context, -rotation, width / 2, height / 2); rotateAtOffset(context, -rotation, halfWidth, halfHeight);
context.beginPath(); context.beginPath();
context.moveTo(topLeft[0] * pixelRatio, topLeft[1] * pixelRatio); context.moveTo(topLeft[0] * pixelRatio, topLeft[1] * pixelRatio);
context.lineTo(topRight[0] * pixelRatio, topRight[1] * pixelRatio); context.lineTo(topRight[0] * pixelRatio, topRight[1] * pixelRatio);
context.lineTo(bottomRight[0] * pixelRatio, bottomRight[1] * pixelRatio); context.lineTo(bottomRight[0] * pixelRatio, bottomRight[1] * pixelRatio);
context.lineTo(bottomLeft[0] * pixelRatio, bottomLeft[1] * pixelRatio); context.lineTo(bottomLeft[0] * pixelRatio, bottomLeft[1] * pixelRatio);
context.clip(); context.clip();
rotateAtOffset(context, rotation, width / 2, height / 2); rotateAtOffset(context, rotation, halfWidth, halfHeight);
} }
/** /**
@@ -79,10 +79,10 @@ class CanvasLayerRenderer extends LayerRenderer {
dispatchComposeEvent_(type, context, frameState, opt_transform) { dispatchComposeEvent_(type, context, frameState, opt_transform) {
const layer = this.getLayer(); const layer = this.getLayer();
if (layer.hasListener(type)) { if (layer.hasListener(type)) {
const width = frameState.size[0] * frameState.pixelRatio; const halfWidth = (frameState.size[0] * frameState.pixelRatio) / 2;
const height = frameState.size[1] * frameState.pixelRatio; const halfHeight = (frameState.size[1] * frameState.pixelRatio) / 2;
const rotation = frameState.viewState.rotation; const rotation = frameState.viewState.rotation;
rotateAtOffset(context, -rotation, width / 2, height / 2); rotateAtOffset(context, -rotation, halfWidth, halfHeight);
const transform = opt_transform !== undefined ? const transform = opt_transform !== undefined ?
opt_transform : this.getTransform(frameState, 0); opt_transform : this.getTransform(frameState, 0);
const render = new CanvasImmediateRenderer( const render = new CanvasImmediateRenderer(
@@ -91,7 +91,7 @@ class CanvasLayerRenderer extends LayerRenderer {
const composeEvent = new RenderEvent(type, render, frameState, const composeEvent = new RenderEvent(type, render, frameState,
context, null); context, null);
layer.dispatchEvent(composeEvent); layer.dispatchEvent(composeEvent);
rotateAtOffset(context, rotation, width / 2, height / 2); rotateAtOffset(context, rotation, halfWidth, halfHeight);
} }
} }

View File

@@ -161,10 +161,9 @@ class CanvasVectorLayerRenderer extends CanvasLayerRenderer {
const viewHints = frameState.viewHints; const viewHints = frameState.viewHints;
const snapToPixel = !(viewHints[ViewHint.ANIMATING] || viewHints[ViewHint.INTERACTING]); const snapToPixel = !(viewHints[ViewHint.ANIMATING] || viewHints[ViewHint.INTERACTING]);
const width = frameState.size[0] * pixelRatio; const halfWidth = (frameState.size[0] * pixelRatio) / 2;
const height = frameState.size[1] * pixelRatio; const halfHeight = (frameState.size[1] * pixelRatio) / 2;
rotateAtOffset(replayContext, -rotation, rotateAtOffset(replayContext, -rotation, halfWidth, halfHeight);
width / 2, height / 2);
replayGroup.replay(replayContext, transform, rotation, skippedFeatureUids, snapToPixel); replayGroup.replay(replayContext, transform, rotation, skippedFeatureUids, snapToPixel);
if (vectorSource.getWrapX() && projection.canWrapX() && if (vectorSource.getWrapX() && projection.canWrapX() &&
!containsExtent(projectionExtent, extent)) { !containsExtent(projectionExtent, extent)) {
@@ -189,8 +188,7 @@ class CanvasVectorLayerRenderer extends CanvasLayerRenderer {
startX -= worldWidth; startX -= worldWidth;
} }
} }
rotateAtOffset(replayContext, rotation, rotateAtOffset(replayContext, rotation, halfWidth, halfHeight);
width / 2, height / 2);
if (hasRenderListeners) { if (hasRenderListeners) {
this.dispatchRenderEvent(replayContext, frameState, transform); this.dispatchRenderEvent(replayContext, frameState, transform);