【发布时间】:2016-03-19 20:41:45
【问题描述】:
我正在编写代码以在 Fabricjs 中的所有对象之间保持相同的 strokeWidth。
我能够成功地维护普通 Objects 的 strokeWidth ,让它成为矩形或椭圆(我编写了代码来维护两者)。 但是当我使用 group 时,我无法保持 strokeWidth。
这是fiddle。
重现问题的步骤?
1)打开上面小提琴链接
2)调整圆的大小,检查边框的大小(调整大小时边框的厚度保持不变)
3)现在调整矩形和 text(group) 的大小,预期的结果是边框的粗细必须始终相同,但 不会发生。
代码: `
var gCanvas = new fabric.Canvas('canvDraw');
gCanvas.setWidth(700);
gCanvas.setHeight(700);
var text = new fabric.Text('test', { fontSize: 30, top: 100, left : 100, fill : 'red'});
// add some shapes - these are examples, others should work too
var myRectangle = new fabric.Rect({
left: 100,
top: 100,
fill: '#999999',
width: 120,
height: 120,
strokeWidth: 3,
fill: '',
stroke: '#000000'
});
var group = new fabric.Group([ myRectangle, text ], {borderColor: 'black', cornerColor: 'green', lockScalingFlip : true});
gCanvas.add(group);
var myEllipse = new fabric.Ellipse({
top: 250,
left: 100,
rx: 75,
ry: 50,
fill: '',
stroke: '#000000',
strokeWidth: 3
});
gCanvas.add(myEllipse);
gCanvas.observe('object:scaling', function (e) {
e.target.resizeToScale();
});
fabric.Object.prototype.resizeToScale = function () {
// resizes an object that has been scaled (e.g. by manipulating the handles), setting scale to 1 and recalculating bounding box where necessary
switch (this.type) {
case "ellipse":
this.rx *= this.scaleX;
this.ry *= this.scaleY;
this.width = this.rx * 2;
this.height = this.ry * 2;
this.scaleX = 1;
this.scaleY = 1;
break;
case "rect":
this.width *= this.scaleX;
this.height *= this.scaleY;
this.scaleX = 1;
this.scaleY = 1;
default:
break;
}
}
`
【问题讨论】:
标签: javascript jquery html5-canvas fabricjs