Plugins
[raphael] / plugins / raphael.primitives.js
1 /*!
2  * Raphael Primitives Plugin 0.2
3  *
4  * Copyright (c) 2009 Dmitry Baranovskiy (http://raphaeljs.com)
5  * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
6  */
7
8 Raphael.fn.g.star = function (cx, cy, r, r2, rays) {
9     r2 = r2 || r * .382;
10     rays = rays || 5;
11     var points = ["M", cx, cy + r2, "L"],
12         R;
13     for (var i = 1; i < rays * 2; i++) {
14         R = i % 2 ? r : r2;
15         points = points.concat([(cx + R * Math.sin(i * Math.PI / rays)), (cy + R * Math.cos(i * Math.PI / rays))]);
16     }
17     points.push("z");
18     return this.path(points.join());
19 };
20 Raphael.fn.flower = function (cx, cy, rout, rin, n) {
21     rin = rin || rout * .5;
22     n = +n < 3 || !n ? 5 : n;
23     var points = ["M", cx, cy + rin, "Q"],
24         R;
25     for (var i = 1; i < n * 2 + 1; i++) {
26         R = i % 2 ? rout : rin;
27         points = points.concat([+(cx + R * Math.sin(i * Math.PI / n)).toFixed(3), +(cy + R * Math.cos(i * Math.PI / n)).toFixed(3)]);
28     }
29     points.push("z");
30     return this.path(points);
31 };
32 Raphael.fn.spike = function (cx, cy, rout, rin, n) {
33     rin = rin || rout * .5;
34     n = +n < 3 || !n ? 5 : n;
35     var points = ["M", cx, cy - rout, "Q"],
36         R;
37     for (var i = 1; i < n * 2 + 1; i++) {
38         R = i % 2 ? rin : rout;
39         points = points.concat([cx + R * Math.sin(i * Math.PI / n - Math.PI), cy + R * Math.cos(i * Math.PI / n - Math.PI)]);
40     }
41     points.push("z");
42     return this.path(points);
43 };
44 Raphael.fn.polygon = function (cx, cy, r, n) {
45     n = +n < 3 || !n ? 5 : n;
46     var points = ["M", cx, cy - r, "L"],
47         R;
48     for (var i = 1; i < n; i++) {
49         points = points.concat([cx + r * Math.sin(i * Math.PI * 2 / n - Math.PI), cy + r * Math.cos(i * Math.PI * 2 / n - Math.PI)]);
50     }
51     points.push("z");
52     return this.path(points);
53 };
54 Raphael.fn.line = function (x1, y1, x2, y2) {
55     return this.path(["M", x1, y1, "L", x2, y2]);
56 };
57 Raphael.fn.drawGrid = function (x, y, w, h, wv, hv, color) {
58     color = color || "#000";
59     var path = ["M", x, y, "L", x + w, y, x + w, y + h, x, y + h, x, y],
60         rowHeight = h / hv,
61         columnWidth = w / wv;
62     for (var i = 1; i < hv; i++) {
63         path = path.concat(["M", x, y + i * rowHeight, "L", x + w, y + i * rowHeight]);
64     }
65     for (var i = 1; i < wv; i++) {
66         path = path.concat(["M", x + i * columnWidth, y, "L", x + i * columnWidth, y + h]);
67     }
68     return this.path(path.join(",")).attr({stroke: color});
69 };
70 Raphael.fn.square = function (cx, cy, r) {
71     r = r * .7;
72     return this.rect(cx - r, cy - r, 2 * r, 2 * r);
73 };
74 Raphael.fn.triangle = function (cx, cy, r) {
75     r *= 1.75;
76     return this.path("M".concat(cx, ",", cy, "m0-", r * .58, "l", r * .5, ",", r * .87, "-", r, ",0z"));
77 };
78 Raphael.fn.diamond = function (cx, cy, r) {
79     return this.path(["M", cx, cy - r, "l", r, r, -r, r, -r, -r, r, -r, "z"]);
80 };
81 Raphael.fn.cross = function (cx, cy, r) {
82     r = r / 2.5;
83     return this.path("M".concat(cx - r, ",", cy, "l", [-r, -r, r, -r, r, r, r, -r, r, r, -r, r, r, r, -r, r, -r, -r, -r, r, -r, -r, "z"]));
84 };
85 Raphael.fn.plus = function (cx, cy, r) {
86     r = r / 2;
87     return this.path("M".concat(cx - r / 2, ",", cy - r / 2, "l", [0, -r, r, 0, 0, r, r, 0, 0, r, -r, 0, 0, r, -r, 0, 0, -r, -r, 0, 0, -r, "z"]));
88 };
89 Raphael.fn.g.arrow = function (cx, cy, r) {
90     return this.path("M".concat(cx - r * .7, ",", cy - r * .4, "l", [r * .6, 0, 0, -r * .4, r, r * .8, -r, r * .8, 0, -r * .4, -r * .6, 0], "z"));
91 };