From b46c5b4ca35d1d9bfd01eee4a6958d4ebdde7900 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Sun, 2 Oct 2016 12:12:42 +0200 Subject: [PATCH] remove unused function in staticCanvas (#3313) * remove unused function in static * fixed svg export --- src/canvas.class.js | 60 ++++++++++++ src/mixins/canvas_dataurl_exporter.mixin.js | 4 +- src/mixins/object_interactivity.mixin.js | 3 +- src/static_canvas.class.js | 102 ++++---------------- test/unit/canvas_static.js | 12 --- 5 files changed, 83 insertions(+), 98 deletions(-) diff --git a/src/canvas.class.js b/src/canvas.class.js index fcf11b38..5ee5f600 100644 --- a/src/canvas.class.js +++ b/src/canvas.class.js @@ -1516,6 +1516,66 @@ } }, + /** + * @private + */ + _toObject: function(instance, methodName, propertiesToInclude) { + //If the object is part of the current selection group, it should + //be transformed appropriately + //i.e. it should be serialised as it would appear if the selection group + //were to be destroyed. + var originalProperties = this._realizeGroupTransformOnObject(instance), + object = this.callSuper('_toObject', instance, methodName, propertiesToInclude); + //Undo the damage we did by changing all of its properties + this._unwindGroupTransformOnObject(instance, originalProperties); + return object; + }, + + /** + * Realises an object's group transformation on it + * @private + * @param {fabric.Object} [instance] the object to transform (gets mutated) + * @returns the original values of instance which were changed + */ + _realizeGroupTransformOnObject: function(instance) { + var layoutProps = ['angle', 'flipX', 'flipY', 'height', 'left', 'scaleX', 'scaleY', 'top', 'width']; + if (instance.group && instance.group === this.getActiveGroup()) { + //Copy all the positionally relevant properties across now + var originalValues = {}; + layoutProps.forEach(function(prop) { + originalValues[prop] = instance[prop]; + }); + this.getActiveGroup().realizeTransform(instance); + return originalValues; + } + else { + return null; + } + }, + + /** + * Restores the changed properties of instance + * @private + * @param {fabric.Object} [instance] the object to un-transform (gets mutated) + * @param {Object} [originalValues] the original values of instance, as returned by _realizeGroupTransformOnObject + */ + _unwindGroupTransformOnObject: function(instance, originalValues) { + if (originalValues) { + instance.set(originalValues); + } + }, + + /** + * @private + */ + _setSVGObject: function(markup, instance, reviver) { + var originalProperties; + //If the object is in a selection group, simulate what would happen to that + //object when the group is deselected + originalProperties = this._realizeGroupTransformOnObject(instance); + this.callSuper('_setSVGObject', markup, instance, reviver); + this._unwindGroupTransformOnObject(instance, originalProperties); + }, }); // copying static properties manually to work around Opera's bug, diff --git a/src/mixins/canvas_dataurl_exporter.mixin.js b/src/mixins/canvas_dataurl_exporter.mixin.js index 40d43183..a2ee5068 100644 --- a/src/mixins/canvas_dataurl_exporter.mixin.js +++ b/src/mixins/canvas_dataurl_exporter.mixin.js @@ -117,8 +117,8 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, /** @lends fabric.Stati origHeight = this.getHeight(), scaledWidth = origWidth * multiplier, scaledHeight = origHeight * multiplier, - activeObject = this.getActiveObject(), - activeGroup = this.getActiveGroup(), + activeObject = this._activeObject, + activeGroup = this._activeGroup, zoom = this.getZoom(), newZoom = zoom * multiplier / fabric.devicePixelRatio; diff --git a/src/mixins/object_interactivity.mixin.js b/src/mixins/object_interactivity.mixin.js index 27988c63..fe097110 100644 --- a/src/mixins/object_interactivity.mixin.js +++ b/src/mixins/object_interactivity.mixin.js @@ -193,8 +193,7 @@ * @chainable */ drawSelectionBackground: function(ctx) { - if (!this.selectionBackgroundColor || this.group - || this !== this.canvas.getActiveObject()) { + if (!this.selectionBackgroundColor || this.group || !this.active) { return this; } ctx.save(); diff --git a/src/static_canvas.class.js b/src/static_canvas.class.js index bea56669..67fe510d 100644 --- a/src/static_canvas.class.js +++ b/src/static_canvas.class.js @@ -668,10 +668,11 @@ * @chainable true */ setViewportTransform: function (vpt) { - var activeGroup = this.getActiveGroup(); + var activeGroup = this._activeGroup, object; this.viewportTransform = vpt; for (var i = 0, len = this._objects.length; i < len; i++) { - this._objects[i].setCoords(); + object = this._objects[i]; + object.group || object.setCoords(); } if (activeGroup) { activeGroup.setCoords(); @@ -744,22 +745,6 @@ return this.lowerCanvasEl; }, - /** - * Returns currently selected object, if any - * @return {fabric.Object} - */ - getActiveObject: function() { - return null; - }, - - /** - * Returns currently selected group of object, if any - * @return {fabric.Group} - */ - getActiveGroup: function() { - return null; - }, - /** * @private * @param {fabric.Object} obj Object that was added @@ -849,27 +834,19 @@ ctx.transform.apply(ctx, this.viewportTransform); this._renderObjects(ctx, objects); ctx.restore(); - if (!this.controlsAboveOverlay && this.interactive) { + if (!this.controlsAboveOverlay && this.drawControls) { this.drawControls(ctx); } if (this.clipTo) { ctx.restore(); } this._renderOverlay(ctx); - if (this.controlsAboveOverlay && this.interactive) { + if (this.controlsAboveOverlay && this.drawControls) { this.drawControls(ctx); } this.fire('after:render'); }, - /** - * dummy function for organization purpouse. - * @private - */ - drawControls: function() { - // NOOP - }, - /** * @private * @param {CanvasRenderingContext2D} ctx Context to render on @@ -1100,56 +1077,13 @@ instance.includeDefaultValues = false; } - //If the object is part of the current selection group, it should - //be transformed appropriately - //i.e. it should be serialised as it would appear if the selection group - //were to be destroyed. - var originalProperties = this._realizeGroupTransformOnObject(instance), - object = instance[methodName](propertiesToInclude); + var object = instance[methodName](propertiesToInclude); if (!this.includeDefaultValues) { instance.includeDefaultValues = originalValue; } - - //Undo the damage we did by changing all of its properties - this._unwindGroupTransformOnObject(instance, originalProperties); - return object; }, - /** - * Realises an object's group transformation on it - * @private - * @param {fabric.Object} [instance] the object to transform (gets mutated) - * @returns the original values of instance which were changed - */ - _realizeGroupTransformOnObject: function(instance) { - var layoutProps = ['angle', 'flipX', 'flipY', 'height', 'left', 'scaleX', 'scaleY', 'top', 'width']; - if (instance.group && instance.group === this.getActiveGroup()) { - //Copy all the positionally relevant properties across now - var originalValues = {}; - layoutProps.forEach(function(prop) { - originalValues[prop] = instance[prop]; - }); - this.getActiveGroup().realizeTransform(instance); - return originalValues; - } - else { - return null; - } - }, - - /** - * Restores the changed properties of instance - * @private - * @param {fabric.Object} [instance] the object to un-transform (gets mutated) - * @param {Object} [originalValues] the original values of instance, as returned by _realizeGroupTransformOnObject - */ - _unwindGroupTransformOnObject: function(instance, originalValues) { - if (originalValues) { - instance.set(originalValues); - } - }, - /** * @private */ @@ -1307,20 +1241,24 @@ * @private */ _setSVGObjects: function(markup, reviver) { - var instance, originalProperties; + var instance; for (var i = 0, objects = this.getObjects(), len = objects.length; i < len; i++) { instance = objects[i]; if (instance.excludeFromExport) { continue; } - //If the object is in a selection group, simulate what would happen to that - //object when the group is deselected - originalProperties = this._realizeGroupTransformOnObject(instance); - markup.push(instance.toSVG(reviver)); - this._unwindGroupTransformOnObject(instance, originalProperties); + this._setSVGObject(markup, instance, reviver); } }, + /** + * push single object svg representation in the markup + * @private + */ + _setSVGObject: function(markup, instance, reviver) { + markup.push(instance.toSVG(reviver)); + }, + /** * @private */ @@ -1372,7 +1310,7 @@ if (!object) { return this; } - var activeGroup = this.getActiveGroup ? this.getActiveGroup() : null, + var activeGroup = this._activeGroup, i, obj, objs; if (object === activeGroup) { objs = activeGroup._objects; @@ -1400,7 +1338,7 @@ if (!object) { return this; } - var activeGroup = this.getActiveGroup ? this.getActiveGroup() : null, + var activeGroup = this._activeGroup, i, obj, objs; if (object === activeGroup) { objs = activeGroup._objects; @@ -1428,7 +1366,7 @@ if (!object) { return this; } - var activeGroup = this.getActiveGroup ? this.getActiveGroup() : null, + var activeGroup = this._activeGroup, i, obj, idx, newIdx, objs; if (object === activeGroup) { @@ -1496,7 +1434,7 @@ if (!object) { return this; } - var activeGroup = this.getActiveGroup ? this.getActiveGroup() : null, + var activeGroup = this._activeGroup, i, obj, idx, newIdx, objs; if (object === activeGroup) { diff --git a/test/unit/canvas_static.js b/test/unit/canvas_static.js index a7cf82ac..3df6a938 100644 --- a/test/unit/canvas_static.js +++ b/test/unit/canvas_static.js @@ -1382,18 +1382,6 @@ canvas.viewportTransform = fabric.StaticCanvas.prototype.viewportTransform; }); - test('getActiveObject', function() { - ok(typeof canvas.getActiveObject == 'function'); - var activeObject = canvas.getActiveObject(); - equal(activeObject, null, 'should return null'); - }); - - test('getActiveGroup', function() { - ok(typeof canvas.getActiveGroup == 'function'); - var activeGroup = canvas.getActiveGroup(); - equal(activeGroup, null, 'should return null'); - }); - test('getContext', function() { ok(typeof canvas.getContext == 'function'); var context = canvas.getContext();