angular-fabric-js
Version:
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 9.0.7.
599 lines (593 loc) • 23.2 kB
JavaScript
import { __decorate } from 'tslib';
import { ViewChild, Component, NgModule } from '@angular/core';
import { fabric } from 'fabric';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { ColorPickerModule } from 'ngx-color-picker';
var FabricjsEditorComponent = /** @class */ (function () {
function FabricjsEditorComponent() {
this.props = {
canvasFill: '#ffffff',
canvasImage: '',
id: null,
opacity: null,
fill: null,
fontSize: null,
lineHeight: null,
charSpacing: null,
fontWeight: null,
fontStyle: null,
textAlign: null,
fontFamily: null,
TextDecoration: ''
};
this.url = '';
this.size = {
width: 500,
height: 300
};
this.globalEditor = false;
this.textEditor = false;
this.imageEditor = false;
this.figureEditor = false;
}
FabricjsEditorComponent.prototype.ngAfterViewInit = function () {
var _this = this;
// setup front side canvas
this.canvas = new fabric.Canvas(this.htmlCanvas.nativeElement, {
hoverCursor: 'pointer',
selection: true,
selectionBorderColor: 'blue',
isDrawingMode: true
});
this.canvas.on({
'object:moving': function (e) { },
'object:modified': function (e) { },
'object:selected': function (e) {
var selectedObject = e.target;
_this.selected = selectedObject;
selectedObject.hasRotatingPoint = true;
selectedObject.transparentCorners = false;
selectedObject.borderColor = 'blue';
selectedObject.cornerColor = 'green';
selectedObject.cornerSize = 6;
selectedObject.cornerStyle = 'circle';
selectedObject.padding = 10;
_this.resetPanels();
if (selectedObject.type !== 'group' && selectedObject) {
_this.getId();
_this.getOpacity();
switch (selectedObject.type) {
case 'rect':
case 'circle':
case 'triangle':
_this.figureEditor = true;
_this.getFill();
break;
case 'i-text':
_this.textEditor = true;
_this.getLineHeight();
_this.getCharSpacing();
_this.getBold();
_this.getFill();
_this.getTextDecoration();
_this.getTextAlign();
_this.getFontFamily();
break;
case 'image':
break;
}
}
},
'selection:cleared': function (e) {
_this.selected = null;
_this.resetPanels();
}
});
this.canvas.setWidth(this.size.width);
this.canvas.setHeight(this.size.height);
// get references to the html canvas element & its context
this.canvas.on('mouse:down', function (e) {
var canvasElement = document.getElementById('canvas');
});
};
/*------------------------Block elements------------------------*/
// Block "Size"
FabricjsEditorComponent.prototype.changeSize = function () {
this.canvas.setWidth(this.size.width);
this.canvas.setHeight(this.size.height);
};
// Block "Add text"
FabricjsEditorComponent.prototype.addText = function () {
if (this.textString) {
var text = new fabric.IText(this.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());
this.canvas.add(text);
this.selectItemAfterAdded(text);
this.textString = '';
}
};
// Block "Add images"
FabricjsEditorComponent.prototype.getImgPolaroid = function (event) {
var _this = this;
var el = event.target;
fabric.loadSVGFromURL(el.src, function (objects, options) {
var image = fabric.util.groupSVGElements(objects, options);
image.set({
left: 10,
top: 10,
angle: 0,
padding: 10,
cornerSize: 10,
hasRotatingPoint: true,
});
_this.extend(image, _this.randomId());
_this.canvas.add(image);
_this.selectItemAfterAdded(image);
});
};
// Block "Upload Image"
FabricjsEditorComponent.prototype.addImageOnCanvas = function (url) {
var _this = this;
if (url) {
fabric.Image.fromURL(url, function (image) {
image.set({
left: 10,
top: 10,
angle: 0,
padding: 10,
cornerSize: 10,
hasRotatingPoint: true
});
image.scaleToWidth(200);
image.scaleToHeight(200);
_this.extend(image, _this.randomId());
_this.canvas.add(image);
_this.selectItemAfterAdded(image);
});
}
};
FabricjsEditorComponent.prototype.readUrl = function (event) {
var _this = this;
if (event.target.files && event.target.files[0]) {
var reader = new FileReader();
reader.onload = function (readerEvent) {
_this.url = readerEvent.target.result;
};
reader.readAsDataURL(event.target.files[0]);
}
};
FabricjsEditorComponent.prototype.removeWhite = function (url) {
this.url = '';
};
// Block "Add figure"
FabricjsEditorComponent.prototype.addFigure = function (figure) {
var add;
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());
this.canvas.add(add);
this.selectItemAfterAdded(add);
};
/*Canvas*/
FabricjsEditorComponent.prototype.cleanSelect = function () {
this.canvas.discardActiveObject().renderAll();
};
FabricjsEditorComponent.prototype.selectItemAfterAdded = function (obj) {
this.canvas.discardActiveObject().renderAll();
this.canvas.setActiveObject(obj);
};
FabricjsEditorComponent.prototype.setCanvasFill = function () {
if (!this.props.canvasImage) {
this.canvas.backgroundColor = this.props.canvasFill;
this.canvas.renderAll();
}
};
FabricjsEditorComponent.prototype.extend = function (obj, id) {
obj.toObject = (function (toObject) {
return function () {
return fabric.util.object.extend(toObject.call(this), {
id: id
});
};
})(obj.toObject);
};
FabricjsEditorComponent.prototype.setCanvasImage = function () {
var self = this;
if (this.props.canvasImage) {
this.canvas.setBackgroundImage(this.props.canvasImage, this.canvas.renderAll.bind(this.canvas), {
// width: this.size.width,
// height: this.size.height,
originX: 'left',
originY: 'top',
crossOrigin: 'anonymous'
});
}
};
FabricjsEditorComponent.prototype.randomId = function () {
return Math.floor(Math.random() * 999999) + 1;
};
/*------------------------Global actions for element------------------------*/
FabricjsEditorComponent.prototype.getActiveStyle = function (styleName, object) {
object = object || this.canvas.getActiveObject();
if (!object) {
return '';
}
if (object.getSelectionStyles && object.isEditing) {
return (object.getSelectionStyles()[styleName] || '');
}
else {
return (object[styleName] || '');
}
};
FabricjsEditorComponent.prototype.setActiveStyle = function (styleName, value, object) {
object = object || this.canvas.getActiveObject();
if (!object) {
return;
}
if (object.setSelectionStyles && object.isEditing) {
var style = {};
style[styleName] = value;
if (typeof value === 'string') {
if (value.includes('underline')) {
object.setSelectionStyles({ underline: true });
}
else {
object.setSelectionStyles({ underline: false });
}
if (value.includes('overline')) {
object.setSelectionStyles({ overline: true });
}
else {
object.setSelectionStyles({ overline: false });
}
if (value.includes('line-through')) {
object.setSelectionStyles({ linethrough: true });
}
else {
object.setSelectionStyles({ linethrough: false });
}
}
object.setSelectionStyles(style);
object.setCoords();
}
else {
if (typeof value === 'string') {
if (value.includes('underline')) {
object.set('underline', true);
}
else {
object.set('underline', false);
}
if (value.includes('overline')) {
object.set('overline', true);
}
else {
object.set('overline', false);
}
if (value.includes('line-through')) {
object.set('linethrough', true);
}
else {
object.set('linethrough', false);
}
}
object.set(styleName, value);
}
object.setCoords();
this.canvas.renderAll();
};
FabricjsEditorComponent.prototype.getActiveProp = function (name) {
var object = this.canvas.getActiveObject();
if (!object) {
return '';
}
return object[name] || '';
};
FabricjsEditorComponent.prototype.setActiveProp = function (name, value) {
var object = this.canvas.getActiveObject();
if (!object) {
return;
}
object.set(name, value).setCoords();
this.canvas.renderAll();
};
FabricjsEditorComponent.prototype.clone = function () {
var activeObject = this.canvas.getActiveObject();
var activeGroup = this.canvas.getActiveObjects();
if (activeObject) {
var clone = void 0;
switch (activeObject.type) {
case 'rect':
clone = new fabric.Rect(activeObject.toObject());
break;
case 'circle':
clone = new fabric.Circle(activeObject.toObject());
break;
case 'triangle':
clone = new fabric.Triangle(activeObject.toObject());
break;
case 'i-text':
clone = new fabric.IText('', activeObject.toObject());
break;
case 'image':
clone = fabric.util.object.clone(activeObject);
break;
}
if (clone) {
clone.set({ left: 10, top: 10 });
this.canvas.add(clone);
this.selectItemAfterAdded(clone);
}
}
};
FabricjsEditorComponent.prototype.getId = function () {
this.props.id = this.canvas.getActiveObject().toObject().id;
};
FabricjsEditorComponent.prototype.setId = function () {
var val = this.props.id;
var complete = this.canvas.getActiveObject().toObject();
console.log(complete);
this.canvas.getActiveObject().toObject = function () {
complete.id = val;
return complete;
};
};
FabricjsEditorComponent.prototype.getOpacity = function () {
this.props.opacity = this.getActiveStyle('opacity', null) * 100;
};
FabricjsEditorComponent.prototype.setOpacity = function () {
this.setActiveStyle('opacity', parseInt(this.props.opacity, 10) / 100, null);
};
FabricjsEditorComponent.prototype.getFill = function () {
this.props.fill = this.getActiveStyle('fill', null);
};
FabricjsEditorComponent.prototype.setFill = function () {
this.setActiveStyle('fill', this.props.fill, null);
};
FabricjsEditorComponent.prototype.getLineHeight = function () {
this.props.lineHeight = this.getActiveStyle('lineHeight', null);
};
FabricjsEditorComponent.prototype.setLineHeight = function () {
this.setActiveStyle('lineHeight', parseFloat(this.props.lineHeight), null);
};
FabricjsEditorComponent.prototype.getCharSpacing = function () {
this.props.charSpacing = this.getActiveStyle('charSpacing', null);
};
FabricjsEditorComponent.prototype.setCharSpacing = function () {
this.setActiveStyle('charSpacing', this.props.charSpacing, null);
};
FabricjsEditorComponent.prototype.getFontSize = function () {
this.props.fontSize = this.getActiveStyle('fontSize', null);
};
FabricjsEditorComponent.prototype.setFontSize = function () {
this.setActiveStyle('fontSize', parseInt(this.props.fontSize, 10), null);
};
FabricjsEditorComponent.prototype.getBold = function () {
this.props.fontWeight = this.getActiveStyle('fontWeight', null);
};
FabricjsEditorComponent.prototype.setBold = function () {
this.props.fontWeight = !this.props.fontWeight;
this.setActiveStyle('fontWeight', this.props.fontWeight ? 'bold' : '', null);
};
FabricjsEditorComponent.prototype.setFontStyle = function () {
this.props.fontStyle = !this.props.fontStyle;
if (this.props.fontStyle) {
this.setActiveStyle('fontStyle', 'italic', null);
}
else {
this.setActiveStyle('fontStyle', 'normal', null);
}
};
FabricjsEditorComponent.prototype.getTextDecoration = function () {
this.props.TextDecoration = this.getActiveStyle('textDecoration', null);
};
FabricjsEditorComponent.prototype.setTextDecoration = function (value) {
var iclass = this.props.TextDecoration;
if (iclass.includes(value)) {
iclass = iclass.replace(RegExp(value, 'g'), '');
}
else {
iclass += " " + value;
}
this.props.TextDecoration = iclass;
this.setActiveStyle('textDecoration', this.props.TextDecoration, null);
};
FabricjsEditorComponent.prototype.hasTextDecoration = function (value) {
return this.props.TextDecoration.includes(value);
};
FabricjsEditorComponent.prototype.getTextAlign = function () {
this.props.textAlign = this.getActiveProp('textAlign');
};
FabricjsEditorComponent.prototype.setTextAlign = function (value) {
this.props.textAlign = value;
this.setActiveProp('textAlign', this.props.textAlign);
};
FabricjsEditorComponent.prototype.getFontFamily = function () {
this.props.fontFamily = this.getActiveProp('fontFamily');
};
FabricjsEditorComponent.prototype.setFontFamily = function () {
this.setActiveProp('fontFamily', this.props.fontFamily);
};
/*System*/
FabricjsEditorComponent.prototype.removeSelected = function () {
var activeObject = this.canvas.getActiveObject();
var activeGroup = this.canvas.getActiveObjects();
if (activeObject) {
this.canvas.remove(activeObject);
// this.textString = '';
}
else if (activeGroup) {
this.canvas.discardActiveObject();
var self_1 = this;
activeGroup.forEach(function (object) {
self_1.canvas.remove(object);
});
}
};
FabricjsEditorComponent.prototype.bringToFront = function () {
var activeObject = this.canvas.getActiveObject();
var activeGroup = this.canvas.getActiveObjects();
if (activeObject) {
activeObject.bringToFront();
activeObject.opacity = 1;
}
else if (activeGroup) {
this.canvas.discardActiveObject();
activeGroup.forEach(function (object) {
object.bringToFront();
});
}
};
FabricjsEditorComponent.prototype.sendToBack = function () {
var activeObject = this.canvas.getActiveObject();
var activeGroup = this.canvas.getActiveObjects();
if (activeObject) {
this.canvas.sendToBack(activeObject);
activeObject.sendToBack();
activeObject.opacity = 1;
}
else if (activeGroup) {
this.canvas.discardActiveObject();
activeGroup.forEach(function (object) {
object.sendToBack();
});
}
};
FabricjsEditorComponent.prototype.confirmClear = function () {
if (confirm('Are you sure?')) {
this.canvas.clear();
}
};
FabricjsEditorComponent.prototype.rasterize = function () {
var image = new Image();
image.src = this.canvas.toDataURL({ format: 'png' });
var w = window.open('');
w.document.write(image.outerHTML);
this.downLoadImage();
};
FabricjsEditorComponent.prototype.downLoadImage = function () {
var c = this.canvas.toDataURL({ format: 'png' });
var downloadLink = document.createElement('a');
document.body.appendChild(downloadLink);
downloadLink.href = c;
downloadLink.target = '_self';
downloadLink.download = Date.now() + '.png';
downloadLink.click();
};
FabricjsEditorComponent.prototype.rasterizeSVG = function () {
var w = window.open('');
w.document.write(this.canvas.toSVG());
this.downLoadSVG();
return 'data:image/svg+xml;utf8,' + encodeURIComponent(this.canvas.toSVG());
};
FabricjsEditorComponent.prototype.downLoadSVG = function () {
var c = 'data:image/svg+xml;utf8,' + encodeURIComponent(this.canvas.toSVG());
var downloadLink = document.createElement('a');
document.body.appendChild(downloadLink);
downloadLink.href = c;
downloadLink.target = '_self';
downloadLink.download = Date.now() + '.svg';
downloadLink.click();
};
FabricjsEditorComponent.prototype.saveCanvasToJSON = function () {
var json = JSON.stringify(this.canvas);
localStorage.setItem('Kanvas', json);
console.log('json');
console.log(json);
};
FabricjsEditorComponent.prototype.loadCanvasFromJSON = function () {
var _this = this;
var CANVAS = localStorage.getItem('Kanvas');
console.log('CANVAS');
console.log(CANVAS);
// and load everything from the same json
this.canvas.loadFromJSON(CANVAS, function () {
console.log('CANVAS untar');
console.log(CANVAS);
// making sure to render canvas at the end
_this.canvas.renderAll();
// and checking if object's "name" is preserved
console.log('this.canvas.item(0).name');
console.log(_this.canvas);
});
};
FabricjsEditorComponent.prototype.rasterizeJSON = function () {
this.json = JSON.stringify(this.canvas, null, 2);
};
FabricjsEditorComponent.prototype.resetPanels = function () {
this.textEditor = false;
this.imageEditor = false;
this.figureEditor = false;
};
FabricjsEditorComponent.prototype.drawingMode = function () {
this.canvas.isDrawingMode = !this.canvas.isDrawingMode;
};
__decorate([
ViewChild('htmlCanvas')
], FabricjsEditorComponent.prototype, "htmlCanvas", void 0);
FabricjsEditorComponent = __decorate([
Component({
selector: 'angular-editor-fabric-js',
template: "<canvas id=\"canvas\" #htmlCanvas></canvas>\r\n",
styles: ["#canvas{border:2px dashed #ccc}"]
})
], FabricjsEditorComponent);
return FabricjsEditorComponent;
}());
var FabricjsEditorModule = /** @class */ (function () {
function FabricjsEditorModule() {
}
FabricjsEditorModule = __decorate([
NgModule({
declarations: [FabricjsEditorComponent],
imports: [
BrowserModule,
FormsModule,
ColorPickerModule
],
exports: [FabricjsEditorComponent]
})
], FabricjsEditorModule);
return FabricjsEditorModule;
}());
/*
* Public API Surface of angular-editor-fabric-js
*/
/**
* Generated bundle index. Do not edit.
*/
export { FabricjsEditorComponent, FabricjsEditorModule };
//# sourceMappingURL=angular-fabric-js.js.map