简体   繁体   English

当一个具有偏移量的组被缩放时,如何计算其绝对位置?

[英]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时:

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? 我希望redRect的绝对位置是相同的,但事实并非如此,比例如何影响偏移量或位置?

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): 如果你需要的绝对位置top-left的矩形的角落,你可以做到这一点(的情况下,如果形状不旋转):

// 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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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