bd358cc1da7f77b1009e2e5b81be60420bfddaed
[raphael] / plugins / raphael.path.methods.js
1 Raphael.el.isAbsolute = true;
2 Raphael.el.absolutely = function () {
3     if (this.type != "path") {
4         return this;
5     }
6     this.isAbsolute = true;
7     return this;
8 };
9 Raphael.el.relatively = function () {
10     if (this.type != "path") {
11         return this;
12     }
13     this.isAbsolute = false;
14     return this;
15 };
16 Raphael.el.moveTo = function (x, y) {
17     if (this.type != "path") {
18         return this;
19     }
20     var d = this.isAbsolute ? "M" : "m";
21     d += +parseFloat(x).toFixed(3) + " " + (+parseFloat(y).toFixed(3)) + " ";
22     this.attr({path: this.attrs.path + d});
23     return this;
24 };
25 Raphael.el.lineTo = function (x, y) {
26     if (this.type != "path") {
27         return this;
28     }
29     var d = this.isAbsolute ? "L" : "l";
30     d += +parseFloat(x).toFixed(3) + " " + (+parseFloat(y).toFixed(3)) + " ";
31     this.attr({path: this.attrs.path + d});
32     return this;
33 };
34 Raphael.el.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x, y) {
35     if (this.type != "path") {
36         return this;
37     }
38     var d = this.isAbsolute ? "A" : "a";
39     d += [+parseFloat(rx).toFixed(3), +parseFloat(ry).toFixed(3), 0, large_arc_flag, sweep_flag, +parseFloat(x).toFixed(3), +parseFloat(y).toFixed(3)].join(" ");
40     this.attr({path: this.attrs.path + d});
41     return this;
42 };
43 Raphael.el.curveTo = function () {
44     if (this.type != "path") {
45         return this;
46     }
47     var args = Array.prototype.splice.call(arguments, 0, arguments.length),
48         d = [0, 0, 0, 0, "s", 0, "c"][args.length] || "";
49     if (this.isAbsolute) {
50         d = d.toUpperCase();
51     }
52     this.attr({path: this.attrs.path + d + args});
53     return this;
54 };
55 Raphael.el.qcurveTo = function () {
56     if (this.type != "path") {
57         return this;
58     }
59     var d = [0, 1, "t", 3, "q"][arguments.length],
60         args = Array.prototype.splice.call(arguments, 0, arguments.length);
61     if (this.isAbsolute) {
62         d = d.toUpperCase();
63     }
64     this.attr({path: this.attrs.path + d + args});
65     return this;
66 };
67 Raphael.el.addRoundedCorner = function (r, dir) {
68     if (this.type != "path") {
69         return this;
70     }
71     var rollback = this.isAbsolute,
72         o = this;
73     if (rollback) {
74         this.relatively();
75         rollback = function () {
76             o.absolutely();
77         };
78     } else {
79         rollback = function () {};
80     }
81     this.arcTo(r, r, 0, {"lu": 1, "rd": 1, "ur": 1, "dl": 1}[dir] || 0, r * (!!(dir.indexOf("r") + 1) * 2 - 1), r * (!!(dir.indexOf("d") + 1) * 2 - 1));
82     rollback();
83     return this;
84 };
85 Raphael.el.andClose = function () {
86     if (this.type != "path") {
87         return this;
88     }
89     this.attr({path: this.attrs.path + "z"});
90     return this;
91 };