import 'fabric';
declare const fabric: any;
export class EditorComponentBase {
// Block "Add text"
constructor(){}
private extend(obj, id) {
obj.toObject = (function (toObject) {
return function () {
return fabric.util.object.extend(toObject.call(this), {
id: id
});
};
})(obj.toObject);
}
private randomId() {
return Math.floor(Math.random() * 999999) + 1;
}
public createText(textString) {
const text = new fabric.Text(textString, {
left: 10,
top: 10,
fontFamily: 'helvetica',
angle: 0,
fill: '#000000',
scaleX: 0.5,
scaleY: 0.5,
fontWeight: '',
hasRotatingPoint: true
});
this.extend(text, this.randomId());
return text;
}
public createShape(objects) {
const group = new fabric.Group();
objects.forEach(function (object) {
group.addWithUpdate(object);
});
this.extend(group, this.randomId());
group.setCoords();
return group;
}
public createImage(image) {
image.set({
left: 10,
top: 10,
angle: 0,
padding: 10,
cornersize: 10,
hasRotatingPoint: true
});
image.setWidth(200);
image.setHeight(200);
this.extend(image, this.randomId());
return image;
}
createFigure(figure) {
let add: any;
switch (figure) {
case 'rectangle':
add = new fabric.Rect({
width: 200, height: 100, left: 10, top: 10, angle: 0,
fill: '#3f51b5'
});
break;
case 'square':
add = new fabric.Rect({
width: 100, height: 100, left: 10, top: 10, angle: 0,
fill: '#4caf50'
});
break;
case 'triangle':
add = new fabric.Triangle({
width: 100, height: 100, left: 10, top: 10, fill: '#2196f3'
});
break;
case 'circle':
add = new fabric.Circle({
radius: 50, left: 10, top: 10, fill: '#ff5722'
});
break;
}
this.extend(add, this.randomId());
return add;
}
}
|