Reputation: 1243
I found another question with some code that worked for an earlier version of fabricjs but I have been struggling to make it work with 1.7.20. What am I doing wrong here? If you go to this fiddle you'll see it works when you toggle to 1.4 it works but anything further on breaks it.
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>
Thanks in advance.
Upvotes: 0
Views: 311
Reputation: 13620
You need to use width
instead of currentWidth
as there is no such property available in fabricjs. May be it was there in earlier versions and got removed later on.
var halfleft = (obj.width * obj.scaleX) / 2;
Updated fiddle - http://jsfiddle.net/759FV/148/
Upvotes: 1