中心fabricjs对象onclick?

时间:2017-11-15 21:41:45

标签: javascript fabricjs

我发现了另一个问题,其中一些代码适用于早期版本的fabricjs,但我一直在努力使其适用于1.7.20。我在这做错了什么? If you go to this fiddle当你切换到1.4时,你会发现它有效,但是进一步的操作会打破它。

var canvas = new fabric.Canvas('c');
var text = new fabric.IText('Text', {
  left: 10,
  top: 10,
});
canvas.add(text);

document.getElementById('center').addEventListener('click', function(e) {
  var obj = canvas.getActiveObject();
  var leftcenter = canvas.width / 2
  var halfleft = obj.currentWidth / 2;
  obj.set("left", leftcenter - halfleft);
  obj.set("top", obj.get('top'));
  obj.setCoords();
  canvas.renderAll();
});
<button id="center">Fit To left center</button>

提前致谢。

1 个答案:

答案 0 :(得分:1)

您需要使用public class CustomDataGridView : DataGridView { private const int BORDERWIDTH = 2; public CustomDataGridView() { VerticalScrollBar.Visible = true; VerticalScrollBar.VisibleChanged += VerticalScrollBarOnVisibleChanged; ClientSize = new Size(ClientSize.Width - 21, ClientSize.Height); } private void VerticalScrollBarOnVisibleChanged(object sender, EventArgs eventArgs) { if (!VerticalScrollBar.Visible) { int width = VerticalScrollBar.Width; VerticalScrollBar.Location = new Point(ClientRectangle.Width - width - BORDERWIDTH, 1); VerticalScrollBar.Size = new Size(width, ClientRectangle.Height - BORDERWIDTH); ClientSize = new Size(ClientSize.Width, ClientSize.Height); VerticalScrollBar.Show(); } } } 代替width,因为fabricjs中没有此类属性。可能是它在早期版本中存在并且稍后被删除。

var halfleft =(obj.width * obj.scaleX)/ 2;

更新了小提琴 - http://jsfiddle.net/759FV/148/