From bda9f32dcd38ff1e71b5783bfa26f3012ae3cc7c Mon Sep 17 00:00:00 2001 From: Ulrich-Matthias Schäfer Date: Mon, 11 Jan 2016 23:39:49 +0100 Subject: added SVG.Point class as wrapper for SVGPoint, added `el.point()` method (#403 / #437) --- spec/spec/element.js | 8 ++++ spec/spec/point.js | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 spec/spec/point.js (limited to 'spec') diff --git a/spec/spec/element.js b/spec/spec/element.js index 10b8b75..fd273a6 100644 --- a/spec/spec/element.js +++ b/spec/spec/element.js @@ -616,4 +616,12 @@ describe('Element', function() { }) }) }) + + describe('point()', function() { + it('creates a point from screen coordinates transformed in the elements space', function(){ + var rect = draw.rect(100,100) + expect(rect.point(2,5).x).toBeCloseTo(-6) + expect(rect.point(2,5).y).toBeCloseTo(-3) + }) + }) }) diff --git a/spec/spec/point.js b/spec/spec/point.js new file mode 100644 index 0000000..ce28781 --- /dev/null +++ b/spec/spec/point.js @@ -0,0 +1,120 @@ +describe('Point', function() { + var point + + describe('initialization', function() { + + describe('without a source', function() { + + point(function() { + matrix = new SVG.Point + }) + + it('creates a new point with default values', function() { + expect(point.x).toBe(0) + expect(point.y).toBe(0) + }) + + }) + + describe('with x and y given', function() { + it('creates a point with given values', function() { + var point = new SVG.Point(2,4) + + expect(point.x).toBe(2) + expect(point.y).toBe(4) + }) + }) + + describe('with array given', function() { + it('creates a point from array', function() { + var point = new SVG.Point([2,4]) + + expect(point.x).toBe(2) + expect(point.y).toBe(4) + }) + }) + + describe('with object given', function() { + it('creates a point from object', function() { + var point = new SVG.Point({x:2,y:4}) + + expect(point.x).toBe(2) + expect(point.y).toBe(4) + }) + }) + + describe('with SVG.Point given', function() { + it('creates a point from SVG.Point', function() { + var point = new SVG.Point(new SVG.Point(2,4)) + + expect(point.x).toBe(2) + expect(point.y).toBe(4) + }) + }) + + describe('with native SVGPoint given', function() { + it('creates a point from native SVGPoint', function() { + var point = new SVG.Point(new SVG.Point(2,4).native()) + + expect(point.x).toBe(2) + expect(point.y).toBe(4) + }) + }) + + }) + + describe('clone()', function() { + it('returns cloned point', function() { + var point1 = new SVG.Point(1,1) + , point2 = new SVG.Point(point1) + + expect(point1).toEqual(point2) + expect(point1).not.toBe(point2) + }) + }) + + describe('morph()', function() { + it('stores a given point for morphing', function() { + var point1 = new SVG.Point(1,1) + , point2 = new SVG.Matrix(2,2) + + point1.morph(point2) + + expect(point1.destination).toEqual(point2) + }) + it('stores a clone, not the given matrix itself', function() { + var point1 = new SVG.Point(1,1) + , point2 = new SVG.Matrix(2,2) + + point1.morph(point2) + + expect(point1.destination).not.toBe(point2) + }) + }) + + describe('at()', function() { + it('returns a morphed point at a given position', function() { + var point1 = new SVG.Point(1,1) + , point2 = new SVG.Point(2,2) + , matrix3 = matrix1.morph(matrix2).at(0.5) + + expect(matrix3).toEqual(new SVG.Point(1.5, 1.5)) + }) + }) + + describe('transform()', function() { + it('returns a point transformed with given matrix', function() { + var point = new SVG.Point(1,5) + , matrix = new SVG.Matrix(0,0,1,0,0,1) + + expect(point.transform(matrox)).toEqual(new SVG.Point(5,1)) + }) + } + + describe('native()', function() { + it('returns native SVGPoint', function() { + expect(new SVG.Point().native() instanceof SVGPoint).toBeTruthy() + }) + }) + +}) \ No newline at end of file -- cgit v1.2.3