Statistiques
| Révision :

root / tei / js / .graffle.js @ 2

Historique | Voir | Annoter | Télécharger (3,74 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
var el;
59
window.onload = function () {
60
    var dragger = function () {
61
        this.ox = this.type == "rect" ? this.attr("x") : this.attr("cx");
62
        this.oy = this.type == "rect" ? this.attr("y") : this.attr("cy");
63
        this.animate({"fill-opacity": .2}, 500);
64
    },
65
        move = function (dx, dy) {
66
            var att = this.type == "rect" ? {x: this.ox + dx, y: this.oy + dy} : {cx: this.ox + dx, cy: this.oy + dy};
67
            this.attr(att);
68
            for (var i = connections.length; i--;) {
69
                r.connection(connections[i]);
70
            }
71
            r.safari();
72
        },
73
        up = function () {
74
            this.animate({"fill-opacity": 0}, 500);
75
        },
76
        r = Raphael("holder", 640, 480),
77
        connections = [],
78
        shapes = [  r.ellipse(190, 100, 30, 20),
79
                    r.rect(290, 80, 60, 40, 10),
80
                    r.rect(290, 180, 60, 40, 2),
81
                    r.ellipse(450, 100, 20, 20)
82
                ];
83
    for (var i = 0, ii = shapes.length; i < ii; i++) {
84
        var color = Raphael.getColor();
85
        shapes[i].attr({fill: color, stroke: color, "fill-opacity": 0, "stroke-width": 2, cursor: "move"});
86
        shapes[i].drag(move, dragger, up);
87
    }
88
    connections.push(r.connection(shapes[0], shapes[1], "#fff"));
89
    connections.push(r.connection(shapes[1], shapes[2], "#fff", "#fff|5"));
90
    connections.push(r.connection(shapes[1], shapes[3], "#000", "#fff"));
91
};