Skip to content

Commit

Permalink
Keep print area centered when resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
perliedman committed Dec 27, 2024
1 parent 730ac79 commit a51562b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/ol/ExtentInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default class ExtentInteraction extends OlExtentInteraction {
super(options);
this.create_ = options?.create;
this.cursor_ = options?.cursor || "auto";
this.keepCentered_ = !!options?.keepCentered;
}

handleEvent(mapBrowserEvent) {
Expand Down Expand Up @@ -123,7 +124,25 @@ export default class ExtentInteraction extends OlExtentInteraction {
]);
return true;
} else {
return super.handleDragEvent(mapBrowserEvent);
if (this.keepCentered_ && this.pointerHandler_) {
const prevExtent = this._dragStartExtent;
const center = getCenter(prevExtent);
const prevWidth = getWidth(prevExtent);
const prevHeight = getHeight(prevExtent);
const pixelCoordinate = mapBrowserEvent.coordinate;
const modifiedExtent = this.pointerHandler_(pixelCoordinate);
const widthDelta = getWidth(modifiedExtent) - prevWidth;
const heightDelta = getHeight(modifiedExtent) - prevHeight;
this.setExtent([
center[0] - prevWidth / 2 - widthDelta,
center[1] - prevHeight / 2 - heightDelta,
center[0] + prevWidth / 2 + widthDelta,
center[1] + prevHeight / 2 + heightDelta,
]);
this.createOrUpdatePointerFeature_(pixelCoordinate);
} else {
return super.handleDragEvent(mapBrowserEvent);
}
}
}
}
1 change: 1 addition & 0 deletions src/tools/PrintArea.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default function PrintArea() {
extent: initialExtent,
boxStyle,
pointerStyle: new Style(),
keepCentered: true,
});

const rectCoords = [];
Expand Down

0 comments on commit a51562b

Please sign in to comment.