mirror of
https://github.com/Hopiu/fabric.js.git
synced 2026-05-10 06:44:44 +00:00
Add convenience methods to objects, utilizing new object.canvas property — reference to a canvas to which an object was added last. Add unit tests for new methods.
This commit is contained in:
parent
9a7d40d7d7
commit
db37e1e855
2 changed files with 190 additions and 0 deletions
|
|
@ -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;
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
});
|
||||
|
||||
})();
|
||||
|
|
|
|||
Loading…
Reference in a new issue