All files / src/app/editor editor.component.base.ts

0% Statements 0/29
0% Branches 0/4
0% Functions 0/10
0% Lines 0/29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                                                                                                                                                                                                   
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;
  }
 
 
}