forked from zwartopwit/raphael-shapes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
raphael.shapes.js
94 lines (86 loc) · 2.42 KB
/
raphael.shapes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* raphael.shapes 0.0.2
*
* Copyright (c) 2009 Wout Fierens
* Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
*/
// extending raphael with a polygon function (to be used with raw SVG polygon data)
Raphael.fn.polygon = function(cx, cy, points) {
return this.path()
.sett({ type: "polygon", points: points })
.translate(cx, cy);
};
// adding a n-gon element
Raphael.fn.ngon = function(cx, cy, r, points) {
return this.path()
.sett({ type: "ngon", r: r, points: points })
.translate(cx, cy);
};
// adding a star element
Raphael.fn.star = function(cx, cy, r1, r2, points) {
return this.path()
.sett({ type: "star", r1: r1, r2: r2, points: points })
.translate(cx, cy);
};
// adding a star element
Raphael.el.sett = function() {
var setts = {};
if (typeof arguments[0] == "string") {
setts[arguments[0]] = arguments[1];
} else if (arguments[0]) {
setts = arguments[0];
}
this.setts = {};
var self = this;
_(setts).chain().keys().each(function(key) {
self.setts[key] = setts[key];
});
return this.attr("path", this[this.setts.type]());
};
// n-gon path function
Raphael.el.ngon = function() {
var points = [],
n = this.setts.points,
r = this.setts.r,
part = 360 / n;
_(n).times(function(i) {
var a = i * part - 90,
x = r * Math.cos(a * Math.PI / 180),
y = r * Math.sin(a * Math.PI / 180);
points.push((i == 0 ? "M" : "L") + x + "," + y);
});
points.push("Z");
return points.join(" ");
};
// star path function
Raphael.el.star = function() {
var points = [],
n = this.setts.points,
r1 = this.setts.r1,
r2 = this.setts.r2,
part = 360 / n;
_(n).times(function(i) {
var a = i * part + 90,
x = r1 * Math.cos(a * Math.PI / 180),
y = r1 * Math.sin(a * Math.PI / 180);
points.push((i == 0 ? "M" : "L") + x + "," + y);
a += part / 2;
x = r2 * Math.cos(a * Math.PI / 180),
y = r2 * Math.sin(a * Math.PI / 180),
points.push("L" + x + "," + y);
});
points.push("Z");
return points.join(" ");
};
// polygon function
Raphael.el.polygon = function() {
var poly_array = ["M"];
_(this.setts.points.split(' ')).each(function(point, i) {
_(point.split(",")).each(function(c) {
poly_array.push(parseFloat(c));
});
if (i == 0) poly_array.push("L");
});
poly_array.push("Z");
return _.compact(poly_array);
};