diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2016-01-11 23:39:49 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2016-01-11 23:39:49 +0100 |
commit | bda9f32dcd38ff1e71b5783bfa26f3012ae3cc7c (patch) | |
tree | 73ff60bf0bb2f8dca6716cfbc7b9503f5b8c74aa /src | |
parent | 5ea599b880395380ec80167ebbfb54046d517cc5 (diff) | |
download | svg.js-bda9f32dcd38ff1e71b5783bfa26f3012ae3cc7c.tar.gz svg.js-bda9f32dcd38ff1e71b5783bfa26f3012ae3cc7c.zip |
added SVG.Point class as wrapper for SVGPoint, added `el.point()` method (#403 / #437)
Diffstat (limited to 'src')
-rw-r--r-- | src/matrix.js | 2 | ||||
-rw-r--r-- | src/point.js | 72 |
2 files changed, 73 insertions, 1 deletions
diff --git a/src/matrix.js b/src/matrix.js index 4358e55..9b3c28e 100644 --- a/src/matrix.js +++ b/src/matrix.js @@ -183,4 +183,4 @@ SVG.Matrix = SVG.invent({ } -})
\ No newline at end of file +}) diff --git a/src/point.js b/src/point.js new file mode 100644 index 0000000..ba7bd8f --- /dev/null +++ b/src/point.js @@ -0,0 +1,72 @@ +SVG.Point = SVG.invent({ + // Initialize + create: function(x,y) { + var i, source, base = {x:0, y:0} + + // ensure source as object + source = Array.isArray(x) ? + {x:x[0], y:x[1]} : + typeof x === 'object' ? + {x:x.x, y:x.y} : + y != null ? + {x:x, y:y} : base + + // merge source + this.x = source.x + this.y = source.y + } + + // Add methods +, extend: { + // Clone point + clone: function() { + return new SVG.Point(this) + } + // Morph one point into another + , morph: function(point) { + // store new destination + this.destination = new SVG.Point(point) + + return this + } + // Get morphed point at a given position + , at: function(pos) { + // make sure a destination is defined + if (!this.destination) return this + + // calculate morphed matrix at a given position + var point = new SVG.Point({ + x: this.x + (this.destination.x - this.x) * pos + , y: this.y + (this.destination.y - this.y) * pos + }) + + return point + } + // Convert to native SVGPoint + , native: function() { + // create new point + var point = SVG.parser.draw.node.createSVGPoint() + + // update with current values + point.x = this.x + point.y = this.y + + return point + } + // transform point with matrix + , transform: function(matrix) { + return new SVG.Point(this.native().matrixTransform(matrix.native())) + } + + } + +}) + +SVG.extend(SVG.Element, { + + // Get point + point: function(x, y) { + return new SVG.Point(x,y).transform(this.screenCTM().inverse()); + } + +}) |