javascript 对象以编程方式移动后的 Fabricjs 在其新位置上不可选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20162080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:54:02  来源:igfitidea点击:

Fabricjs after object move programmatically its not selectable on its new position

javascriptfabricjs

提问by Thirumalai murugan

I have tried to move the object programmatically and get success, but after the object is moved by programmatic way, its not able to select the object by selecting the object current position, still the object is selectable from by its old position I have tried with canvas.calcOffset();still its not working.

我试图以编程方式移动对象并获得成功,但是在以编程方式移动对象后,它无法通过选择对象当前位置来选择对象,仍然可以通过我尝试过的旧位置选择对象canvas.calcOffset();仍然无法正常工作。

How can I make the object selectable in its current position the code I have used as follows

如何使对象在其当前位置可选择我使用的代码如下

Javascript

Javascript

 var canvas=new fabric.Canvas('canvas');
 canvas.add(new fabric.Rect({                    
                    left:100,
                    top: 100,
                    width: 75,
                    height: 50,
                    fill: 'white',
                    stroke: 'black',
                    strokeWidth: 3,
                    padding: 10,
                    selectable: true
             }));


function changePosition()
{
    canvas.item(0).set({left:300});
    canvas.renderAll();
    canvas.calcOffset();
}

HTML

HTML

<div>
<canvas id="canvas" width="400" height="400" style="border:1px solid red"/>
</div>
<input type="button" onclick="changePosition()" value="Change Possition"/>

Jsfiddle

提琴手

Steps to reproduce the error

重现错误的步骤

  1. Click the Change Positionbutton
  2. Try selecting the rectangle on its current position, and move to the cursor to the plave where the object was previously there you will be able to select the object
  1. 点击Change Position按钮
  2. 尝试选择当前位置上的矩形,然后将光标移动到对象之前所在的平面上,您将能够选择该对象

回答by Innodel

you need to fire setCoords() method once if you are changing object's position programmatically. This will set Coords of your object to new position.

如果您以编程方式更改对象的位置,则需要触发一次 setCoords() 方法。这会将对象的坐标设置为新位置。