From 758c9b6146783edc8b7819d4b6c258ecf4c2894e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sat, 7 Dec 2013 12:47:16 +0100 Subject: [PATCH] Improve caching of decomposed transformation values. --- src/item/Item.js | 57 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/src/item/Item.js b/src/item/Item.js index df1ce45b..b666232f 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -212,6 +212,7 @@ var Item = Base.extend(Callback, /** @lends Item# */{ // Clear cached bounds and position whenever geometry changes delete this._bounds; delete this._position; + delete this._decomposed; } if (parent && (flags & (/*#=*/ ChangeFlag.GEOMETRY | /*#=*/ ChangeFlag.STROKE))) { @@ -962,22 +963,8 @@ var Item = Base.extend(Callback, /** @lends Item# */{ * @ignore */ }), /** @lends Item# */{ - /** - * The current scaling of the item, as described by its {@link #matrix}. - * - * @type Point - * @bean - */ - getScaling: function() { - return this._matrix.getScaling(); - }, - - setScaling: function(/* scale */) { - var scaling = this.getScaling(); - if (scaling != null) { - var scale = Point.read(arguments); - this.scale(scale.x / scaling.x, scale.y / scaling.y); - } + _decompose: function() { + return this._decomposed = this._matrix.decompose(); }, /** @@ -987,12 +974,44 @@ var Item = Base.extend(Callback, /** @lends Item# */{ * @bean */ getRotation: function() { - return this._matrix.getRotation(); + var decomposed = this._decomposed || this._decompose(); + return decomposed && decomposed.rotation; }, setRotation: function(rotation) { - if (rotation != null) - this.rotate(rotation - this.getRotation()); + var current = this.getRotation(); + if (current != null && rotation != null) { + // Preseve the cached _decomposed values over rotation, and only + // update the rotation property on it. + var decomposed = this._decomposed; + this.rotate(rotation - current); + decomposed.rotation = rotation; + this._decomposed = decomposed; + } + }, + + /** + * The current scaling of the item, as described by its {@link #matrix}. + * + * @type Point + * @bean + */ + getScaling: function() { + var decomposed = this._decomposed || this._decompose(); + return decomposed && decomposed.scaling; + }, + + setScaling: function(/* scaling */) { + var current = this.getScaling(); + if (current != null) { + // Clone existing points since we're caching internally. + var scaling = Point.read(arguments, 0, 0, { clone: true }), + // See #setRotation() for preservation of _decomposed. + decomposed = this._decomposed; + this.scale(scaling.x / current.x, scaling.y / current.y); + decomposed.scaling = scaling; + this._decomposed = decomposed; + } }, /**