【问题标题】:when a group (with offset) is scaled,how to calculate its absolute position?当一个组(带偏移量)被缩放时,如何计算它的绝对位置?
【发布时间】:2019-08-06 09:29:58
【问题描述】:

例如,我想画一个带偏移量的矩形。

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

当我用代码缩放 redRect 时:

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

我希望redRect的绝对位置是一样的,但是不一样。比例尺如何影响偏移量或位置?

【问题讨论】:

    标签: javascript konvajs


    【解决方案1】:

    如果您需要矩形的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()
    }
    

    【讨论】:

    • 我的意思是当我缩放时,位置发生了变化。我希望左上角的绝对位置相同。
    • @wan 从 abs 位置的计算中可以看出,您需要更改偏移量或位置(xy)以保持 abs 位置相同。
    • 我改变x: 和y: ,x: 600-(rectscalex-1)*20, y: 60-(rectscaley-1)*10,使位置与no-相同缩放。但是当我旋转它时,位置又错了
    猜你喜欢
    • 2020-06-27
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 2012-03-16
    相关资源
    最近更新 更多