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);
}
}
|