summaryrefslogtreecommitdiffstats
path: root/src/line.js
diff options
context:
space:
mode:
authorwout <wout@impinc.co.uk>2013-02-02 17:34:25 +0100
committerwout <wout@impinc.co.uk>2013-02-02 17:34:25 +0100
commitec56136a1a3cd83f856723d1076536e550296e31 (patch)
tree6031877fba4ada024a917947532245a0e979615c /src/line.js
parent1c9b6af19cd1367a74b91cb88e98ec1bef2d8234 (diff)
downloadsvg.js-ec56136a1a3cd83f856723d1076536e550296e31.tar.gz
svg.js-ec56136a1a3cd83f856723d1076536e550296e31.zip
Added SVG.Line as a native element0.5
Diffstat (limited to 'src/line.js')
-rw-r--r--src/line.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/line.js b/src/line.js
new file mode 100644
index 0000000..b5dd2ef
--- /dev/null
+++ b/src/line.js
@@ -0,0 +1,46 @@
+SVG.Line = function Line() {
+ this.constructor.call(this, SVG.create('line'));
+};
+
+// Inherit from SVG.Shape
+SVG.Line.prototype = new SVG.Shape();
+
+// Add required methods
+SVG.extend(SVG.Line, {
+ // Move line
+ move: function(x, y) {
+ var bbox = this.bbox();
+
+ return this.attr({
+ x1: this.attr('x1') - bbox.x + x,
+ y1: this.attr('y1') - bbox.y + y,
+ x2: this.attr('x2') - bbox.x + x,
+ y2: this.attr('y2') - bbox.y + y
+ });
+ },
+ // Move element by its center
+ center: function(x, y) {
+ var bbox = this.bbox();
+
+ return this.move(x - bbox.width / 2, y - bbox.height / 2);
+ },
+ // Set line size by width and height
+ size: function(width, height) {
+ var bbox = this.bbox();
+
+ this.attr(this.attr('x1') < this.attr('x2') ? 'x2' : 'x1', bbox.x + width);
+ return this.attr(this.attr('y1') < this.attr('y2') ? 'y2' : 'y1', bbox.y + height);
+ }
+});
+
+// Extend all container modules
+SVG.extend(SVG.Container, {
+ line: function(x1, y1, x2, y2) {
+ return this.put(new SVG.Line().attr({
+ x1: x1,
+ y1: y1,
+ x2: x2,
+ y2: y2
+ }));
+ }
+}); \ No newline at end of file