graphics123
graphics123

Reputation: 1271

Unable to maintain strokeWidth for fabricjs group element

Hi I am working on fabricjs library and stuck with a bug.

I am drawing a group of Text and Rect and then re-sizing it. But I see that the size of group @borders increase with re-sizing . But I want the strokeWidth to be same through out .

Here's the Fiddle!.

Code : `

var gCanvas = new fabric.Canvas('canvDraw');
 gCanvas.setWidth(500);
 gCanvas.setHeight(500);

var myEllipse = new fabric.Ellipse({
top: 250,
left: 100,
rx: 75,
ry: 50,
fill: '#999999',
stroke: '#000000',
strokeWidth: 2
});

var myText = new fabric.Text("Some text", {
top: 250,
left: 250,
});


// set up a listener for the event where the object has been modified
gCanvas.observe('object:modified', function (e) {
var objects = gCanvas.getObjects();
for (i in objects) {
    console.log(objects[i]);
    objects[i].strokeWidth = 2;
}
}); 
var group = new fabric.Group([ myEllipse, myText ], {borderColor: 'black', cornerColor: 'green'});
gCanvas.add(group);`

Upvotes: 2

Views: 961

Answers (1)

AndreaBogazzi
AndreaBogazzi

Reputation: 14731

Se updated fiddle http://jsfiddle.net/6CDFr/264/ for dynamic strokewidth change.

gCanvas.observe('object:modified', function (e) {
var myObject = e.target;

if (!myObject._objects) {
  return;
}
for (i in myObject._objects) {
    objects[i].strokeWidth = 2 / Math.sqrt(myObject.scaleX * myObject.scaleY);
}
});

Be carefull i removed a couple of canvas.add(); You should not add objects both to canvas and to group.

Mantain also TEXT dimensions:

gCanvas.observe('object:added', function (e) {
    if (e.target.type === 'text' || e.target.type === 'i-text') {
        e.target.originalFontsize = e.target.fontSize;
    }
});
gCanvas.observe('object:modified', function (e) {
var myObject = e.target;

if (!myObject._objects) {
  return;
}
for (i in myObject._objects) {
    var obj = myObject._objects[i];
    var scaleFactor = Math.sqrt(myObject.scaleX * myObject.scaleY);
    obj.strokeWidth = 2 / scaleFactor;
    if (obj.type === 'text' || obj.type === 'i-text') {
        obj.fontSize = obj.originalFontsize / scaleFactor;
    }
}
});

see updated fiddle http://jsfiddle.net/6CDFr/269/

Upvotes: 1

Related Questions