简体   繁体   中英

when a group (with offset) is scaled,how to calculate its absolute position?

For example, I want to draw a rect with offset.

var redRect = new Konva.Rect({
    x: 600,
    y: 60,
    width: 22,
    height: 40,
    fill: 'red',
    stroke: 'black',
    strokeWidth: 1,
    offset: {
        x: -20,
        y: -10
    }
});

When I scaled the redRect with the code:

redRect.scaleX(50/22);
redRect.scaleY(100/40);

I want the absolute position of the redRect is the same, but it is not.How the scale affects the offset or the position?

If you need the absolute position of top-left corner of the rectangle you can do this (in case if the shape is not rotated):

// calculate the position of the origin of the shape
const absPos = redRect.getAbsolutePosition();

// calculate the position of top-left corner taking offset into account
const topLeftPos = {
  x: absPos.x - shape.offsetX() * shape.scaleX(),
  y: absPos.y - shape.offsetY() * shape.scaleY()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM