📚 The CoCalc Library - books, templates and other resources
cocalc-examples / martinthoma-latex-examples / presentations / Diskrete-Mathematik / isomorph-graph-example / graph1-js / graffle.js
132948 viewsLicense: OTHER
/**1* Originally grabbed from the official RaphaelJS Documentation2* http://raphaeljs.com/graffle.html3* Changes and comments by Philipp Strathausen http://blog.ameisenbar.de4* Licenced under the MIT licence.5*/67/**8* Usage:9* connect two shapes10* parameters:11* source shape [or connection for redrawing],12* target shape,13* style with { fg : linecolor, bg : background color, directed: boolean }14* returns:15* connection { draw = function() }16*/17Raphael.fn.connection = function (obj1, obj2, style) {18var selfRef = this;19/* create and return new connection */20var color = style.fg || "#000";21var edge = {/*22from : obj1,23to : obj2,24style : style,*/25draw : function() {26/* get bounding boxes of target and source */27var bb1 = obj1.getBBox();28var bb2 = obj2.getBBox();29bb1.height = bb1.height + 15; /* respect the label TODO do it in a cleaner way */30bb2.height = bb2.height + 15;31var off1 = 1;32var off2 = 2;33/* coordinates for potential connection coordinates from/to the objects */34var p = [35{x: bb1.x + bb1.width / 2, y: bb1.y - off1}, /* NORTH 1 */36{x: bb1.x + bb1.width / 2, y: bb1.y + bb1.height + off1}, /* SOUTH 1 */37{x: bb1.x - off1, y: bb1.y + bb1.height / 2}, /* WEST 1 */38{x: bb1.x + bb1.width + off1, y: bb1.y + bb1.height / 2}, /* EAST 1 */39{x: bb2.x + bb2.width / 2, y: bb2.y - off2}, /* NORTH 2 */40{x: bb2.x + bb2.width / 2, y: bb2.y + bb2.height + off2}, /* SOUTH 2 */41{x: bb2.x - off2, y: bb2.y + bb2.height / 2}, /* WEST 2 */42{x: bb2.x + bb2.width + off2, y: bb2.y + bb2.height / 2} /* EAST 2 */43];4445/* distances between objects and according coordinates connection */46var d = {}, dis = [];4748/*49* find out the best connection coordinates by trying all possible ways50*/51/* loop the first object's connection coordinates */52for (var i = 0; i < 4; i++) {53/* loop the seond object's connection coordinates */54for (var j = 4; j < 8; j++) {55var dx = Math.abs(p[i].x - p[j].x),56dy = Math.abs(p[i].y - p[j].y);57if ((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))) {58dis.push(dx + dy);59d[dis[dis.length - 1]] = [i, j];60}61}62}63var res = dis.length == 0 ? res = [0, 4] : d[Math.min.apply(Math, dis)];64/* bezier path */65var x1 = p[res[0]].x,66y1 = p[res[0]].y,67x4 = p[res[1]].x,68y4 = p[res[1]].y,69dx = Math.max(Math.abs(x1 - x4) / 2, 10),70dy = Math.max(Math.abs(y1 - y4) / 2, 10),71x2 = [x1, x1, x1 - dx, x1 + dx][res[0]].toFixed(3),72y2 = [y1 - dy, y1 + dy, y1, y1][res[0]].toFixed(3),73x3 = [0, 0, 0, 0, x4, x4, x4 - dx, x4 + dx][res[1]].toFixed(3),74y3 = [0, 0, 0, 0, y1 + dy, y1 - dy, y4, y4][res[1]].toFixed(3);75/* assemble path and arrow */76var path = ["M", x1.toFixed(3), y1.toFixed(3), "C", x2, y2, x3, y3, x4.toFixed(3), y4.toFixed(3)].join(",");77/* arrow */78if(style && style.directed) {79/* magnitude, length of the last path vector */80var mag = Math.sqrt((y4 - y3) * (y4 - y3) + (x4 - x3) * (x4 - x3));81/* vector normalisation to specified length */82var norm = function(x,l){return (-x*(l||5)/mag);};83/* calculate array coordinates (two lines orthogonal to the path vector) */84var arr = [85{x:(norm(x4-x3)+norm(y4-y3)+x4).toFixed(3), y:(norm(y4-y3)+norm(x4-x3)+y4).toFixed(3)},86{x:(norm(x4-x3)-norm(y4-y3)+x4).toFixed(3), y:(norm(y4-y3)-norm(x4-x3)+y4).toFixed(3)}87];88path = path + ",M"+arr[0].x+","+arr[0].y+",L"+x4+","+y4+",L"+arr[1].x+","+arr[1].y;89}9091// applying path92edge.fg && edge.fg.attr({path:path})93|| (edge.fg = selfRef.path(path).attr({stroke: color, fill: "none"}).toBack());94edge.bg && edge.bg.attr({path:path})95|| style && style.bg && (edge.bg = style.bg.split && selfRef.path(path).attr({stroke: style.bg.split("|")[0], fill: "none", "stroke-width": style.bg.split("|")[1] || 3}).toBack());96}97}98edge.draw();99return edge;100};101102103