diff options
Diffstat (limited to 'src/Polygon.js')
-rw-r--r-- | src/Polygon.js | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/Polygon.js b/src/Polygon.js new file mode 100644 index 0000000..9625776 --- /dev/null +++ b/src/Polygon.js @@ -0,0 +1,67 @@ +/* global proportionalSize */ + +SVG.Polyline = SVG.invent({ + // Initialize node + create: 'polyline', + + // Inherit from + inherit: SVG.Shape, + + // Add parent method + construct: { + // Create a wrapped polyline element + polyline: function (p) { + // make sure plot is called as a setter + return this.put(new SVG.Polyline()).plot(p || new SVG.PointArray()) + } + } +}) + +SVG.Polygon = SVG.invent({ + // Initialize node + create: 'polygon', + + // Inherit from + inherit: SVG.Shape, + + // Add parent method + construct: { + // Create a wrapped polygon element + polygon: function (p) { + // make sure plot is called as a setter + return this.put(new SVG.Polygon()).plot(p || new SVG.PointArray()) + } + } +}) + +// Add polygon-specific functions +SVG.extend([SVG.Polyline, SVG.Polygon], { + // Get array + array: function () { + return this._array || (this._array = new SVG.PointArray(this.attr('points'))) + }, + + // Plot new path + plot: function (p) { + return (p == null) ? this.array() + : this.clear().attr('points', typeof p === 'string' ? p + : (this._array = new SVG.PointArray(p))) + }, + + // Clear array cache + clear: function () { + delete this._array + return this + }, + + // Move by left top corner + move: function (x, y) { + return this.attr('points', this.array().move(x, y)) + }, + + // Set element size to given width and height + size: function (width, height) { + var p = proportionalSize(this, width, height) + return this.attr('points', this.array().size(p.width, p.height)) + } +}) |