UNPKG

angular-fabric-js

Version:

This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 9.0.7.

590 lines (584 loc) 20.2 kB
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'; let FabricjsEditorComponent = class FabricjsEditorComponent { constructor() { 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; } ngAfterViewInit() { // 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': (e) => { }, 'object:modified': (e) => { }, 'object:selected': (e) => { const 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': (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', (e) => { const canvasElement = document.getElementById('canvas'); }); } /*------------------------Block elements------------------------*/ // Block "Size" changeSize() { this.canvas.setWidth(this.size.width); this.canvas.setHeight(this.size.height); } // Block "Add text" addText() { if (this.textString) { const 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" getImgPolaroid(event) { const el = event.target; fabric.loadSVGFromURL(el.src, (objects, options) => { const 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" addImageOnCanvas(url) { if (url) { fabric.Image.fromURL(url, (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); }); } } readUrl(event) { if (event.target.files && event.target.files[0]) { const reader = new FileReader(); reader.onload = (readerEvent) => { this.url = readerEvent.target.result; }; reader.readAsDataURL(event.target.files[0]); } } removeWhite(url) { this.url = ''; } // Block "Add figure" addFigure(figure) { let 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*/ cleanSelect() { this.canvas.discardActiveObject().renderAll(); } selectItemAfterAdded(obj) { this.canvas.discardActiveObject().renderAll(); this.canvas.setActiveObject(obj); } setCanvasFill() { if (!this.props.canvasImage) { this.canvas.backgroundColor = this.props.canvasFill; this.canvas.renderAll(); } } extend(obj, id) { obj.toObject = ((toObject) => { return function () { return fabric.util.object.extend(toObject.call(this), { id }); }; })(obj.toObject); } setCanvasImage() { const 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' }); } } randomId() { return Math.floor(Math.random() * 999999) + 1; } /*------------------------Global actions for element------------------------*/ getActiveStyle(styleName, object) { object = object || this.canvas.getActiveObject(); if (!object) { return ''; } if (object.getSelectionStyles && object.isEditing) { return (object.getSelectionStyles()[styleName] || ''); } else { return (object[styleName] || ''); } } setActiveStyle(styleName, value, object) { object = object || this.canvas.getActiveObject(); if (!object) { return; } if (object.setSelectionStyles && object.isEditing) { const 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(); } getActiveProp(name) { const object = this.canvas.getActiveObject(); if (!object) { return ''; } return object[name] || ''; } setActiveProp(name, value) { const object = this.canvas.getActiveObject(); if (!object) { return; } object.set(name, value).setCoords(); this.canvas.renderAll(); } clone() { const activeObject = this.canvas.getActiveObject(); const activeGroup = this.canvas.getActiveObjects(); if (activeObject) { let clone; 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); } } } getId() { this.props.id = this.canvas.getActiveObject().toObject().id; } setId() { const val = this.props.id; const complete = this.canvas.getActiveObject().toObject(); console.log(complete); this.canvas.getActiveObject().toObject = () => { complete.id = val; return complete; }; } getOpacity() { this.props.opacity = this.getActiveStyle('opacity', null) * 100; } setOpacity() { this.setActiveStyle('opacity', parseInt(this.props.opacity, 10) / 100, null); } getFill() { this.props.fill = this.getActiveStyle('fill', null); } setFill() { this.setActiveStyle('fill', this.props.fill, null); } getLineHeight() { this.props.lineHeight = this.getActiveStyle('lineHeight', null); } setLineHeight() { this.setActiveStyle('lineHeight', parseFloat(this.props.lineHeight), null); } getCharSpacing() { this.props.charSpacing = this.getActiveStyle('charSpacing', null); } setCharSpacing() { this.setActiveStyle('charSpacing', this.props.charSpacing, null); } getFontSize() { this.props.fontSize = this.getActiveStyle('fontSize', null); } setFontSize() { this.setActiveStyle('fontSize', parseInt(this.props.fontSize, 10), null); } getBold() { this.props.fontWeight = this.getActiveStyle('fontWeight', null); } setBold() { this.props.fontWeight = !this.props.fontWeight; this.setActiveStyle('fontWeight', this.props.fontWeight ? 'bold' : '', null); } setFontStyle() { this.props.fontStyle = !this.props.fontStyle; if (this.props.fontStyle) { this.setActiveStyle('fontStyle', 'italic', null); } else { this.setActiveStyle('fontStyle', 'normal', null); } } getTextDecoration() { this.props.TextDecoration = this.getActiveStyle('textDecoration', null); } setTextDecoration(value) { let 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); } hasTextDecoration(value) { return this.props.TextDecoration.includes(value); } getTextAlign() { this.props.textAlign = this.getActiveProp('textAlign'); } setTextAlign(value) { this.props.textAlign = value; this.setActiveProp('textAlign', this.props.textAlign); } getFontFamily() { this.props.fontFamily = this.getActiveProp('fontFamily'); } setFontFamily() { this.setActiveProp('fontFamily', this.props.fontFamily); } /*System*/ removeSelected() { const activeObject = this.canvas.getActiveObject(); const activeGroup = this.canvas.getActiveObjects(); if (activeObject) { this.canvas.remove(activeObject); // this.textString = ''; } else if (activeGroup) { this.canvas.discardActiveObject(); const self = this; activeGroup.forEach((object) => { self.canvas.remove(object); }); } } bringToFront() { const activeObject = this.canvas.getActiveObject(); const activeGroup = this.canvas.getActiveObjects(); if (activeObject) { activeObject.bringToFront(); activeObject.opacity = 1; } else if (activeGroup) { this.canvas.discardActiveObject(); activeGroup.forEach((object) => { object.bringToFront(); }); } } sendToBack() { const activeObject = this.canvas.getActiveObject(); const activeGroup = this.canvas.getActiveObjects(); if (activeObject) { this.canvas.sendToBack(activeObject); activeObject.sendToBack(); activeObject.opacity = 1; } else if (activeGroup) { this.canvas.discardActiveObject(); activeGroup.forEach((object) => { object.sendToBack(); }); } } confirmClear() { if (confirm('Are you sure?')) { this.canvas.clear(); } } rasterize() { const image = new Image(); image.src = this.canvas.toDataURL({ format: 'png' }); const w = window.open(''); w.document.write(image.outerHTML); this.downLoadImage(); } downLoadImage() { const c = this.canvas.toDataURL({ format: 'png' }); const downloadLink = document.createElement('a'); document.body.appendChild(downloadLink); downloadLink.href = c; downloadLink.target = '_self'; downloadLink.download = Date.now() + '.png'; downloadLink.click(); } rasterizeSVG() { const w = window.open(''); w.document.write(this.canvas.toSVG()); this.downLoadSVG(); return 'data:image/svg+xml;utf8,' + encodeURIComponent(this.canvas.toSVG()); } downLoadSVG() { const c = 'data:image/svg+xml;utf8,' + encodeURIComponent(this.canvas.toSVG()); const downloadLink = document.createElement('a'); document.body.appendChild(downloadLink); downloadLink.href = c; downloadLink.target = '_self'; downloadLink.download = Date.now() + '.svg'; downloadLink.click(); } saveCanvasToJSON() { const json = JSON.stringify(this.canvas); localStorage.setItem('Kanvas', json); console.log('json'); console.log(json); } loadCanvasFromJSON() { const CANVAS = localStorage.getItem('Kanvas'); console.log('CANVAS'); console.log(CANVAS); // and load everything from the same json this.canvas.loadFromJSON(CANVAS, () => { 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); }); } rasterizeJSON() { this.json = JSON.stringify(this.canvas, null, 2); } resetPanels() { this.textEditor = false; this.imageEditor = false; this.figureEditor = false; } drawingMode() { 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); let FabricjsEditorModule = class FabricjsEditorModule { }; FabricjsEditorModule = __decorate([ NgModule({ declarations: [FabricjsEditorComponent], imports: [ BrowserModule, FormsModule, ColorPickerModule ], exports: [FabricjsEditorComponent] }) ], FabricjsEditorModule); /* * Public API Surface of angular-editor-fabric-js */ /** * Generated bundle index. Do not edit. */ export { FabricjsEditorComponent, FabricjsEditorModule }; //# sourceMappingURL=angular-fabric-js.js.map