From db37e1e8556e0d8577b1f8a062a8311d648a68d3 Mon Sep 17 00:00:00 2001 From: kangax Date: Mon, 20 Aug 2012 18:23:15 +0200 Subject: [PATCH] =?UTF-8?q?Add=20convenience=20methods=20to=20objects,=20u?= =?UTF-8?q?tilizing=20new=20`object.canvas`=20property=20=E2=80=94=20refer?= =?UTF-8?q?ence=20to=20a=20canvas=20to=20which=20an=20object=20was=20added?= =?UTF-8?q?=20last.=20Add=20unit=20tests=20for=20new=20methods.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/object.class.js | 85 +++++++++++++++++++++++++++++++++++ test/unit/object.js | 105 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 190 insertions(+) diff --git a/src/object.class.js b/src/object.class.js index 31b9b741..d05ccac5 100644 --- a/src/object.class.js +++ b/src/object.class.js @@ -1417,6 +1417,91 @@ options.onComplete && options.onComplete(); } }); + }, + + /** + * Centers object horizontally on canvas to which it was added last + * @method centerH + * @return {fabric.Object} thisArg + */ + centerH: function () { + this.canvas.centerObjectH(this); + return this; + }, + + /** + * Centers object vertically on canvas to which it was added last + * @method centerV + * @return {fabric.Object} thisArg + * @chainable + */ + centerV: function () { + this.canvas.centerObjectV(this); + return this; + }, + + /** + * Centers object vertically and horizontally on canvas to which is was added last + * @method center + * @return {fabric.Object} thisArg + * @chainable + */ + center: function () { + return this.centerH().centerV(); + }, + + /** + * Removes object from canvas to which it was added last + * @method remove + * @return {fabric.Object} thisArg + * @chainable + */ + remove: function() { + return this.canvas.remove(this); + }, + + /** + * Moves an object to the bottom of the stack of drawn objects + * @method sendToBack + * @return {fabric.Object} thisArg + * @chainable + */ + sendToBack: function() { + this.canvas.sendToBack(this); + return this; + }, + + /** + * Moves an object to the top of the stack of drawn objects + * @method bringToFront + * @return {fabric.Object} thisArg + * @chainable + */ + bringToFront: function() { + this.canvas.bringToFront(this); + return this; + }, + + /** + * Moves an object one level down in stack of drawn objects + * @method sendBackwards + * @return {fabric.Object} thisArg + * @chainable + */ + sendBackwards: function() { + this.canvas.sendBackwards(this); + return this; + }, + + /** + * Moves an object one level up in stack of drawn objects + * @method bringForward + * @return {fabric.Object} thisArg + * @chainable + */ + bringForward: function() { + this.canvas.bringForward(this); + return this; } }); diff --git a/test/unit/object.js b/test/unit/object.js index e3a575ba..91c9908a 100644 --- a/test/unit/object.js +++ b/test/unit/object.js @@ -667,5 +667,110 @@ ok(addedEventFired); }); + test('canvas reference', function() { + var object = new fabric.Object(); + var object2 = new fabric.Object(); + + canvas.add(object); + canvas.insertAt(object2, 0); + + equal(object.canvas, canvas); + equal(object2.canvas, canvas); + }); + + test('remove', function() { + var object = new fabric.Object(); + + ok(typeof object.remove == 'function'); + + canvas.add(object); + object.remove(); + + equal(canvas.getObjects().length, 0); + }); + + test('center', function() { + var object = new fabric.Object(); + + ok(typeof object.center == 'function'); + + canvas.add(object); + object.center(); + + equal(object.getLeft(), canvas.getWidth() / 2); + equal(object.getTop(), canvas.getHeight() / 2); + }); + + test('centerH', function() { + var object = new fabric.Object(); + + ok(typeof object.centerH == 'function'); + + canvas.add(object); + object.centerH(); + + equal(object.getLeft(), canvas.getWidth() / 2); + }); + + test('centerV', function() { + var object = new fabric.Object(); + + ok(typeof object.centerV == 'function'); + + canvas.add(object); + object.centerV(); + + equal(object.getTop(), canvas.getHeight() / 2); + }); + + test('sendToBack', function() { + var object = new fabric.Object(); + + ok(typeof object.sendToBack == 'function'); + }); + + test('bringToFront', function() { + var object = new fabric.Object(); + + ok(typeof object.bringToFront == 'function'); + }); + + test('sendBackwards', function() { + var object = new fabric.Object(); + + ok(typeof object.bringToFront == 'function'); + }); + + test('bringForward', function() { + var object = new fabric.Object(); + + ok(typeof object.bringToFront == 'function'); + }); + + test('gradient serialization', function() { + var object = new fabric.Object(); + + object.fill = new fabric.Gradient({ + x1: 0, + y1: 0, + x2: 100, + y2: 100, + colorStops: { + '0': 'red', + '1': 'green' + } + }); + + ok(typeof object.toObject().fill == 'object'); + + equal(object.toObject().fill.x1, 0); + equal(object.toObject().fill.y1, 0); + + equal(object.toObject().fill.x2, 100); + equal(object.toObject().fill.y2, 100); + + equal(object.toObject().fill.colorStops['0'], 'red'); + equal(object.toObject().fill.colorStops['1'], 'green'); + }); })();