From 302ec3534c1c924c57af07f532d2542bac7bc05c Mon Sep 17 00:00:00 2001 From: Lilit Date: Thu, 19 Dec 2019 17:06:57 +0400 Subject: [PATCH] add setBackgroundImage and saveImage features --- README.md | 29 ++++-- package.json | 2 +- src/Editor.vue | 223 +++++++++++++++++++++++++---------------- src/assets/js/arrow.js | 38 +++---- src/assets/js/shape.js | 118 +++++++++++----------- src/assets/js/text.js | 2 +- 6 files changed, 235 insertions(+), 177 deletions(-) diff --git a/README.md b/README.md index 7f14d0f..ac9ed44 100644 --- a/README.md +++ b/README.md @@ -27,19 +27,13 @@ Then you'll be able to use Editor component. You must give your editor component ```ref```,which will help you to call the functions to set editor mode,clean objects or undo/redo your changes. ```vue - + mounted() { $this.$refs.editor.set(this.editor.mode,this.editor.options); -}, -data(){ - return:{ - url:"example.png" - } } ``` -`setBackgroundImage` prop will set editor background image `canvasWidth` prop will set editor width @@ -159,16 +153,29 @@ or you can customize your editor's free drawing mode styles by overwriting defau this.$refs.editor.set('freeDrawing',customizeFreeDrawing) ``` -## Function changeColor(`colorHex`) -##### `changeColor(`colorHex`)` to set color for all objects -```javascript - this.$refs.editor.changeColor('#228B22') +## Function setBackgroundImage(imageUrl) +##### `setBackgroundImage(imageUrl)` to set editor background image +```vue +data(){ + return{ + imageUrl:"example.png" + } +}, +methods:{ + this.$refs.editor.setBackgroundImage(this.imageUrl); +} ``` + ## Function uploadImage(e) ##### `uploadImage(e)` to set background of canvas ```javascript this.$refs.editor.uploadImage(e) ``` +## Function saveImage() +##### `saveImage()` to save your image,which returns image in base64 format. +```javascript + this.$refs.editor.saveImage() +``` ## Function clear() ##### `clear()` function delete editor's all objects diff --git a/package.json b/package.json index 9dfb652..65754cc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-image-markup", - "version": "1.0.1", + "version": "1.0.2", "description": "vue-image-markup will provide you to edit uploaded image easily and save it.", "main": "src/Editor.vue", "repository": { diff --git a/src/Editor.vue b/src/Editor.vue index 9d17b59..611f451 100644 --- a/src/Editor.vue +++ b/src/Editor.vue @@ -1,4 +1,3 @@ - + diff --git a/src/assets/js/arrow.js b/src/assets/js/arrow.js index 1916c20..39c39b9 100644 --- a/src/assets/js/arrow.js +++ b/src/assets/js/arrow.js @@ -62,10 +62,10 @@ export default (function () { if(color && color !== params.stroke){ color = params.stroke; - // drag = true; new Arrow(canvas,draggable,params) return Arrow; } + properties = params; if(properties){ fillArrow = params.fill; @@ -91,26 +91,26 @@ export default (function () { inst.canvas.renderAll() }; inst.selectable = true; - - inst.canvas.off('mouse:down'); - inst.canvas.on('mouse:down', function (o) { - inst.onMouseDown(o); - }); - inst.canvas.on('mouse:move', function (o) { - inst.onMouseMove(o); - }); - inst.canvas.on('mouse:up', function (o) { - inst.onMouseUp(o); - - }); - inst.canvas.on('object:moving', function () { - inst.disable(); - }); - + + inst.canvas.off('mouse:down'); + inst.canvas.on('mouse:down', function (o) { + inst.onMouseDown(o); + }); + inst.canvas.on('mouse:move', function (o) { + inst.onMouseMove(o); + }); + inst.canvas.on('mouse:up', function (o) { + inst.onMouseUp(o); + + }); + inst.canvas.on('object:moving', function () { + inst.disable(); + }); + }; Arrow.prototype.onMouseUp = function () { - + let inst = this; if (!inst.isEnable()) { return; @@ -127,7 +127,7 @@ export default (function () { inst.canvas.renderAll(); } inst.disable(); - + }; Arrow.prototype.onMouseMove = function (o) { diff --git a/src/assets/js/shape.js b/src/assets/js/shape.js index 3e99686..8675ba1 100644 --- a/src/assets/js/shape.js +++ b/src/assets/js/shape.js @@ -54,20 +54,20 @@ export default (function () { inst.canvas.renderAll() }; inst.selectable = true; - inst.canvas.off('mouse:down'); - - inst.canvas.on('mouse:down', function (o) { - inst.onMouseDown(o); - }); - inst.canvas.on('mouse:move', function (o) { - inst.onMouseMove(o); - }); - inst.canvas.on('mouse:up', function (o) { - inst.onMouseUp(o); - }); - inst.canvas.on('object:moving', function () { - inst.disable(); - }); + inst.canvas.off('mouse:down'); + + inst.canvas.on('mouse:down', function (o) { + inst.onMouseDown(o); + }); + inst.canvas.on('mouse:move', function (o) { + inst.onMouseMove(o); + }); + inst.canvas.on('mouse:up', function (o) { + inst.onMouseUp(o); + }); + inst.canvas.on('object:moving', function () { + inst.disable(); + }); @@ -149,10 +149,10 @@ export default (function () { }; Shape.prototype.onMouseDown = function (o) { - + let inst = this; if(!drag){ - + if( inst.canvas.getActiveObject()){ inst.canvas.getActiveObject().hasControls = true; inst.canvas.getActiveObject().hasBorders = true; @@ -164,50 +164,50 @@ export default (function () { inst.disable(); return; } - inst.enable(); - - if(inst.canvas.getActiveObject()){ - inst.canvas.getActiveObject().hasControls = false; - inst.canvas.getActiveObject().hasBorders = false; - inst.canvas.getActiveObject().lockMovementX = true; - inst.canvas.getActiveObject().lockMovementY = true; - inst.canvas.getActiveObject().lockUniScaling = true; - inst.canvas.renderAll(); - } - let pointer = inst.canvas.getPointer(o.e); - this.origX = pointer.x; - this.origY = pointer.y; - if(shape === "rect"){ - let rect = new fabric.Rect({ - left: this.origX, - top: this.origY, - originX: 'left', - originY: 'top', - width: pointer.x - this.origX, - height: pointer.y - this.origY, - angle: angle, - fill: fillCircle, - transparentCorners: false, - stroke: color, - strokeWidth: lineWidth - }); - inst.canvas.add(rect).setActiveObject(rect); - } - if(shape === "circle"){ - let circle = new fabric.Ellipse({ - top: this.origY, - left: this.origX, - rx: 0, - ry: 0, - transparentCorners: false, - hasBorders: true, - hasControls: true, - }); - inst.canvas.add(circle).setActiveObject(circle); - } - + inst.enable(); + + if(inst.canvas.getActiveObject()){ + inst.canvas.getActiveObject().hasControls = false; + inst.canvas.getActiveObject().hasBorders = false; + inst.canvas.getActiveObject().lockMovementX = true; + inst.canvas.getActiveObject().lockMovementY = true; + inst.canvas.getActiveObject().lockUniScaling = true; + inst.canvas.renderAll(); + } + let pointer = inst.canvas.getPointer(o.e); + this.origX = pointer.x; + this.origY = pointer.y; + if(shape === "rect"){ + let rect = new fabric.Rect({ + left: this.origX, + top: this.origY, + originX: 'left', + originY: 'top', + width: pointer.x - this.origX, + height: pointer.y - this.origY, + angle: angle, + fill: fillCircle, + transparentCorners: false, + stroke: color, + strokeWidth: lineWidth + }); + inst.canvas.add(rect).setActiveObject(rect); + } + if(shape === "circle"){ + let circle = new fabric.Ellipse({ + top: this.origY, + left: this.origX, + rx: 0, + ry: 0, + transparentCorners: false, + hasBorders: true, + hasControls: true, + }); + inst.canvas.add(circle).setActiveObject(circle); + } + + - }; Shape.prototype.isEnable = function () { diff --git a/src/assets/js/text.js b/src/assets/js/text.js index d980b1d..b2e12dd 100644 --- a/src/assets/js/text.js +++ b/src/assets/js/text.js @@ -25,7 +25,7 @@ export default (function () { Text.prototype.bindEvents = function () { let inst = this; inst.selectable = true; - inst.canvas.off("mouse:down") + // inst.canvas.off("mouse:down") inst.canvas.on("mouse:down", function (o) { inst.onMouseDown(o); });