we (team & I) discovered Konva and it helped us tremendously so far, thanks a lot. However we can't figure how out to create a zoom¢er effect.
We added several object to a Layer itself added to a Stage, both defined as such:
let newStageArea = new Konva.Stage({
container: "mapContainer",
width: canvasWidth ,
height: canvasHeight,
x: 0,
y: 0,
draggable: true,
});
let newLayerArea = new Konva.Layer({
width: canvasWidth,
height: canvasHeight,
x: 0,
y: 0,
//since we parse object coordinates from an external source
//that uses a centered origin we have to offset the layer
offset: {
x: -(canvasWidth / 2),
y: -(canvasHeight / 2),
},
});
We are now tying to implement a method that allows, when an object belonging to the layer is clicked, to zoom on it so that it's centered and fully visible. This is is what we have:
const handleAreaClick = (e) => {
e.evt.preventDefault();
let parkingSpace = e.target;
//Get middle of object no matter its shape
let parkingSpaceBox = parkingSpace.getClientRect({
relativeTo: layerArea,
});
let parkingSpaceCenterX = parkingSpaceBox.x + parkingSpaceBox.width / 2;
let parkingSpaceCenterY = -1 * (parkingSpaceBox.y + parkingSpaceBox.height / 2);
//Zoom¢er on object
const padding = 10;
const focusZoomScale = Math.min(
windowWidth / (parkingSpaceBox.width + padding * 2),
canvasHeight/ (parkingSpaceBox.height + padding * 2));
newLayerArea.setAttrs({
x: -parkingSpaceBox.x * focusZoomScale + padding * focusZoomScale,
y: parkingSpaceBox.y * focusZoomScale + padding * focusZoomScale,
scaleX: focusZoomScale,
scaleY: focusZoomScale
})
}
No matter how we change the x and y formulas (positive/negative...), the layer does not move to the correct position, but to the left and top of the object. However the coordinates for the middle of the object are correct.
Thanks a lot for taking the time to read through.