All files / src/app/editor fabric.custom.ts

0% Statements 0/55
0% Branches 0/16
0% Functions 0/17
0% Lines 0/55
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156                                                                                                                                                                                                                                                                                                                       
import 'fabric';
declare const fabric: any;
 
export class FabricCustom {
 
  private constructor() { /* noop */ }
 
  //TODO: selectable, evented, retextName возможно не нужны
  public static  initCustomization() {
    fabric.Object.prototype.transparentCorners = false;
    fabric.Object.prototype.cornerStyle = 'circle';
    fabric.Text.prototype.objectCaching = false;   // Отключить кеширование для текста
 
    fabric.Object.NUM_FRACTION_DIGITS = 5;
 
 
    fabric.ActiveSelection.prototype.toFrameGroup = (function(toFrameGroup) {
        return function() {
        const objects = this._objects.concat();
        this._objects = [];
        const options = fabric.Object.prototype.toObject.call(this);
        const newGroup = new fabric.FrameGroup([]);
        delete options.type;
        newGroup.set(options);
        objects.forEach(function(object) {
        object.canvas.remove(object);
        object.group = newGroup;
        });
        newGroup._objects = objects;
        if (!this.canvas) {
        return newGroup;
        }
        const canvas = this.canvas;
        canvas.add(newGroup);
        canvas._activeObject = newGroup;
        newGroup.setCoords();
        return newGroup;
      };
      })(fabric.ActiveSelection.prototype.toFrameGroup);
 
 
    fabric.Image.prototype.toObject = (function(toObject) {
      return function() {
        return fabric.util.object.extend(toObject.call(this), {
          selectable: this.selectable,
          evented: this.evented,
          lockMovementX: this.lockMovementX,
          lockMovementY: this.lockMovementY,
          retextName: this.retextName
        });
      };
    })(fabric.Image.prototype.toObject);
 
 
    fabric.Group.prototype.toObject = (function(toObject) {
      return function() {
        return fabric.util.object.extend(toObject.call(this), {
          selectable: this.selectable,
          evented: this.evented,
          lockMovementX: this.lockMovementX,
          lockMovementY: this.lockMovementY
        });
      };
    })(fabric.Group.prototype.toObject);
 
    fabric.Rect.prototype.toObject = (function(toObject) {
      return function() {
        return fabric.util.object.extend(toObject.call(this), {
          selectable: this.selectable,
          evented: this.evented,
          lockMovementX: this.lockMovementX,
          lockMovementY: this.lockMovementY
        });
      };
    })(fabric.Rect.prototype.toObject);
 
 
    fabric.Circle.prototype.toObject = (function(toObject) {
      return function() {
        return fabric.util.object.extend(toObject.call(this), {
          selectable: this.selectable,
          evented: this.evented,
          lockMovementX: this.lockMovementX,
          lockMovementY: this.lockMovementY
        });
      };
    })(fabric.Circle.prototype.toObject);
 
    fabric.Path.prototype.toObject = (function(toObject) {
      return function() {
        return fabric.util.object.extend(toObject.call(this), {
          selectable: this.selectable,
          evented: this.evented,
          lockMovementX: this.lockMovementX,
          lockMovementY: this.lockMovementY
        });
      };
    })(fabric.Path.prototype.toObject);
 
  /*
    if (isRasterizeSvg) {
      fabric.Image.prototype.getSvgSrc = function() {
        return this.toDataURLforSVG();
      };
 
      fabric.Image.prototype.toDataURLforSVG = function(options) {
        var el = fabric.util.createCanvasElement();
        el.width = this._element.naturalWidth;
        el.height = this._element.naturalHeight;
        el.getContext("2d").drawImage(this._element, 0, 0);
        var data = el.toDataURL(options);
        return data;
      };
    }
  */
 
    fabric.Pattern.prototype.toSVG = (function(toSVG) {
      return function(object) {
 
        let patternSource = typeof this.source === 'function' ? this.source() : this.source,
          patternWidth = patternSource.width / object.width,
          patternHeight = patternSource.height / object.height,
          patternOffsetX = this.offsetX / object.width,
          patternOffsetY = this.offsetY / object.height,
          patternImgSrc = '';
        if (this.repeat === 'repeat-x' || this.repeat === 'no-repeat') {
          patternHeight = 1;
        }
        if (this.repeat === 'repeat-y' || this.repeat === 'no-repeat') {
 
          patternWidth = 1;
        }
        if (patternSource.src) {
          patternImgSrc = patternSource.src;
        } else if (patternSource.toDataURL) {
          patternImgSrc = patternSource.toDataURL();
        }
 
        return '<pattern id="SVGID_' + this.id +
          '" x="' + 0 +
          '" y="' + 0 +
          '" width="' + patternWidth +
          '" height="' + patternHeight + '">\n' +
          '<image x="' + patternSource.width * patternOffsetX + '" y="' + patternSource.height * patternOffsetX + '"' +
          ' width="' + (patternSource.width) +
          '" height="' + (patternSource.height) +
          '" xlink:href="' + patternImgSrc +
          '"></image>\n' +
          '</pattern>\n';
 
      };
    })(fabric.Pattern.prototype.toSVG);
  }
 
}