root / tei / js / .raphael.graffle.js @ 2
Historique | Voir | Annoter | Télécharger (3,27 ko)
1 |
Raphael.fn.connection = function (obj1, obj2, line, bg) { |
---|---|
2 |
if (obj1.line && obj1.from && obj1.to) {
|
3 |
line = obj1; |
4 |
obj1 = line.from; |
5 |
obj2 = line.to; |
6 |
} |
7 |
var bb1 = obj1.getBBox(),
|
8 |
bb2 = obj2.getBBox(), |
9 |
p = [{x: bb1.x + bb1.width / 2, y: bb1.y - 1}, |
10 |
{x: bb1.x + bb1.width / 2, y: bb1.y + bb1.height + 1}, |
11 |
{x: bb1.x - 1, y: bb1.y + bb1.height / 2}, |
12 |
{x: bb1.x + bb1.width + 1, y: bb1.y + bb1.height / 2}, |
13 |
{x: bb2.x + bb2.width / 2, y: bb2.y - 1}, |
14 |
{x: bb2.x + bb2.width / 2, y: bb2.y + bb2.height + 1}, |
15 |
{x: bb2.x - 1, y: bb2.y + bb2.height / 2}, |
16 |
{x: bb2.x + bb2.width + 1, y: bb2.y + bb2.height / 2}], |
17 |
d = {}, dis = []; |
18 |
for (var i = 0; i < 4; i++) { |
19 |
for (var j = 4; j < 8; j++) { |
20 |
var dx = Math.abs(p[i].x - p[j].x),
|
21 |
dy = Math.abs(p[i].y - p[j].y); |
22 |
if ((i == j - 4) || (((i != 3 && j != 6) || p[i].x < p[j].x) && ((i != 2 && j != 7) || p[i].x > p[j].x) && ((i != 0 && j != 5) || p[i].y > p[j].y) && ((i != 1 && j != 4) || p[i].y < p[j].y))) { |
23 |
dis.push(dx + dy); |
24 |
d[dis[dis.length - 1]] = [i, j];
|
25 |
} |
26 |
} |
27 |
} |
28 |
if (dis.length == 0) { |
29 |
var res = [0, 4]; |
30 |
} else {
|
31 |
res = d[Math.min.apply(Math, dis)]; |
32 |
} |
33 |
var x1 = p[res[0]].x, |
34 |
y1 = p[res[0]].y,
|
35 |
x4 = p[res[1]].x,
|
36 |
y4 = p[res[1]].y;
|
37 |
dx = Math.max(Math.abs(x1 - x4) / 2, 10); |
38 |
dy = Math.max(Math.abs(y1 - y4) / 2, 10); |
39 |
var x2 = [x1, x1, x1 - dx, x1 + dx][res[0]].toFixed(3), |
40 |
y2 = [y1 - dy, y1 + dy, y1, y1][res[0]].toFixed(3), |
41 |
x3 = [0, 0, 0, 0, x4, x4, x4 - dx, x4 + dx][res[1]].toFixed(3), |
42 |
y3 = [0, 0, 0, 0, y1 + dy, y1 - dy, y4, y4][res[1]].toFixed(3); |
43 |
var path = ["M", x1.toFixed(3), y1.toFixed(3), "C", x2, y2, x3, y3, x4.toFixed(3), y4.toFixed(3)].join(","); |
44 |
if (line && line.line) {
|
45 |
line.bg && line.bg.attr({path: path});
|
46 |
line.line.attr({path: path});
|
47 |
} else {
|
48 |
var color = typeof line == "string" ? line : "#000"; |
49 |
return {
|
50 |
bg: bg && bg.split && this.path(path).attr({stroke: bg.split("|")[0], fill: "none", "stroke-width": bg.split("|")[1] || 3}), |
51 |
line: this.path(path).attr({stroke: color, fill: "none"}), |
52 |
from: obj1,
|
53 |
to: obj2
|
54 |
}; |
55 |
} |
56 |
}; |
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
var el;
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
init = function (shapes,r) { |
68 |
var dragger = function () { |
69 |
this.ox = this.type == "rect" ? this.attr("x") : this.attr("cx"); |
70 |
this.oy = this.type == "rect" ? this.attr("y") : this.attr("cy"); |
71 |
this.animate({"fill-opacity": .2}, 500); |
72 |
}, |
73 |
move = function (dx, dy) { |
74 |
var att = this.type == "rect" ? {x: this.ox + dx, y: this.oy + dy} : {cx: this.ox + dx, cy: this.oy + dy}; |
75 |
this.attr(att);
|
76 |
for (var i = connections.length; i--;) { |
77 |
r.connection(connections[i]); |
78 |
} |
79 |
r.safari(); |
80 |
}, |
81 |
up = function () { |
82 |
this.animate({"fill-opacity": 0}, 500); |
83 |
}; |
84 |
for (var i = 0, ii = shapes.length; i < ii; i++) { |
85 |
var color = Raphael.getColor();
|
86 |
shapes[i].attr({fill: color, stroke: color, "fill-opacity": 0, "stroke-width": 2, cursor: "move"}); |
87 |
shapes[i].drag(move, dragger, up); |
88 |
} |
89 |
|
90 |
}; |
91 |
|