Skip to content

Commit

Permalink
MAJOR feat(fabric.Point): divide, scalarDivide, scalarDivideEquals (#…
Browse files Browse the repository at this point in the history
…7716)

* **BREAKING**: divide, scalarDivide, scalarDivideEquals
  • Loading branch information
ShaMan123 authored Feb 20, 2022
1 parent 2d922e1 commit 30c0c19
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
15 changes: 11 additions & 4 deletions src/point.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,31 @@
return this;
},

/**
* Divides this point by another and returns a new one
* @param {fabric.Point} that
* @return {fabric.Point}
*/
divide: function (that) {
return new Point(this.x / that.x, this.y / that.y);
},

/**
* Divides this point by a value and returns a new one
* TODO: rename in scalarDivide in 2.0
* @param {Number} scalar
* @return {fabric.Point}
*/
divide: function (scalar) {
scalarDivide: function (scalar) {
return new Point(this.x / scalar, this.y / scalar);
},

/**
* Divides this point by a value
* TODO: rename in scalarDivideEquals in 2.0
* @param {Number} scalar
* @return {fabric.Point} thisArg
* @chainable
*/
divideEquals: function (scalar) {
scalarDivideEquals: function (scalar) {
this.x /= scalar;
this.y /= scalar;
return this;
Expand Down
22 changes: 16 additions & 6 deletions test/unit/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,28 +187,38 @@
assert.equal(point.y, y1 * scalar, 'y coords should be added');
});

QUnit.test('divide', function(assert) {
QUnit.test('divide', function (assert) {
var a = new fabric.Point(2, 3), b = new fabric.Point(4, 5);

assert.ok(typeof a.divide === 'function');
var returned = a.divide(b);
assert.ok(returned instanceof fabric.Point, 'returns a point class');
assert.equal(returned.x, a.x / b.x, 'should be the quotient of the x coords');
assert.equal(returned.y, a.y / b.y, 'should be the quotient of the y coords');
});

QUnit.test('scalarDivide', function(assert) {
var x1 = 2, y1 = 3, scalar = 3,
point = new fabric.Point(x1, y1);

assert.ok(typeof point.divide === 'function');
assert.ok(typeof point.scalarDivide === 'function');
assert.equal(point.x, x1, 'constructor pass x value');
assert.equal(point.y, y1, 'constructor pass y value');
var returned = point.divide(scalar);
var returned = point.scalarDivide(scalar);
assert.ok(returned instanceof fabric.Point, 'returns a point class');
assert.notEqual(returned, point, 'is not chainable');
assert.equal(returned.x, x1 / scalar, 'x coords should be added');
assert.equal(returned.y, y1 / scalar, 'y coords should be added');
});

QUnit.test('divideEquals', function(assert) {
QUnit.test('scalarDivideEquals', function(assert) {
var x1 = 2, y1 = 3, scalar = 3,
point = new fabric.Point(x1, y1);

assert.ok(typeof point.divideEquals === 'function');
assert.ok(typeof point.scalarDivideEquals === 'function');
assert.equal(point.x, x1, 'constructor pass x value');
assert.equal(point.y, y1, 'constructor pass y value');
var returned = point.divideEquals(scalar);
var returned = point.scalarDivideEquals(scalar);
assert.ok(returned instanceof fabric.Point, 'returns a point class');
assert.equal(returned, point, 'is chainable');
assert.equal(point.x, x1 / scalar, 'x coords should be added');
Expand Down

0 comments on commit 30c0c19

Please sign in to comment.