diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-12-14 22:35:45 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-14 22:35:45 +0100 |
commit | f2eb24610bb607b92d786dc77857871302cea49a (patch) | |
tree | 143cbf007c0039e4cae4a8c89fa7ae86a65f4dff | |
parent | 33e82b796e7870a9523b6e9653877a2613f8f7a2 (diff) | |
parent | 79554009233065d40117690158d99161e31cb5dc (diff) | |
download | svg.js-f2eb24610bb607b92d786dc77857871302cea49a.tar.gz svg.js-f2eb24610bb607b92d786dc77857871302cea49a.zip |
Merge pull request #945 from edemaine/pointarray-transform
Add PointArray.transform by analogy to Point.transform
-rw-r--r-- | spec/spec/pointarray.js | 28 | ||||
-rw-r--r-- | src/types/PointArray.js | 17 |
2 files changed, 45 insertions, 0 deletions
diff --git a/spec/spec/pointarray.js b/spec/spec/pointarray.js new file mode 100644 index 0000000..f50f981 --- /dev/null +++ b/spec/spec/pointarray.js @@ -0,0 +1,28 @@ +describe('PointArray', function() { + const squareString = '0,0 1,0 1,1 0,1'; + const square = new SVG.PointArray(squareString) + + describe('toString()', function() { + it('round trips with string', () => { + expect(square.toString()).toEqual(squareString) + }) + }) + + describe('transform()', function() { + it('translates correctly', () => { + const translation = new SVG.Matrix().translate(2,1) + const newSquare = square.transform(translation) + expect(newSquare.toString()).toEqual('2,1 3,1 3,2 2,2') + }) + + it('transforms like Point', () => { + const matrix = new SVG.Matrix(1, 2, 3, 4, 5, 6) + const newSquare = square.transform(matrix) + for (let i = 0; i < square.length; i++) { + const squarePoint = new SVG.Point(square[i]) + const newSquarePoint = new SVG.Point(newSquare[i]) + expect(squarePoint.transform(matrix)).toEqual(newSquarePoint) + } + }) + }) +}) diff --git a/src/types/PointArray.js b/src/types/PointArray.js index 9e7406d..2246bbd 100644 --- a/src/types/PointArray.js +++ b/src/types/PointArray.js @@ -71,6 +71,23 @@ extend(PointArray, { return points }, + // transform points with matrix (similar to Point.transform) + transform (m) { + const points = [] + + for (let i = 0; i < this.length; i++) { + const point = this[i] + // Perform the matrix multiplication + points.push([ + m.a * point[0] + m.c * point[1] + m.e, + m.b * point[0] + m.d * point[1] + m.f + ]) + } + + // Return the required point + return new PointArray(points) + }, + // Move point string move (x, y) { var box = this.bbox() |