From 00cbc7f64ee7dc8ce6b5351724ef1302ea3d6778 Mon Sep 17 00:00:00 2001 From: Dennis Hotson Date: Sat, 11 Jan 2014 05:17:41 +1100 Subject: [PATCH] Improved calculation of text angle and spacing --- springyui.js | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/springyui.js b/springyui.js index d1937bb..57803a2 100755 --- a/springyui.js +++ b/springyui.js @@ -168,7 +168,7 @@ jQuery.fn.springy = function(params) { } //change default to 10.0 to allow text fit between edges - var spacing = 10.0; //was 6.0 before + var spacing = 12.0; // Figure out how far off center the line should be drawn var offset = normal.multiply(-((total - 1) * spacing)/2.0 + (n * spacing)); @@ -235,11 +235,10 @@ jQuery.fn.springy = function(params) { ctx.textAlign = "center"; ctx.textBaseline = "top"; ctx.font = (edge.data.font !== undefined) ? edge.data.font : edgeFont; - ctx.fillStyle = "#5BA6EC"; - var xText = (s1.x+s2.x)/2; - var yText = (s1.y+s2.y)/2; - ctx.translate(xText, yText); - ctx.rotate(getTextAngle(s1.x,s1.y,s2.x,s2.y)); + ctx.fillStyle = stroke; + var textPos = s1.add(s2).divide(2).add(normal.multiply(-8)); + ctx.translate(textPos.x, textPos.y); + ctx.rotate(Math.atan2(s2.y - s1.y, s2.x - s1.x)); ctx.fillText(text, 0,-2); ctx.restore(); } @@ -280,12 +279,6 @@ jQuery.fn.springy = function(params) { renderer.start(); - // return angle of text in radian - function getTextAngle(x1, y1, x2, y2){ - var tan = (y2-y1)/(x2-x1); - return Math.atan(tan); - } - // helpers for figuring out where to draw arrows function intersect_line_line(p1, p2, p3, p4) { var denom = ((p4.y - p3.y)*(p2.x - p1.x) - (p4.x - p3.x)*(p2.y - p1.y));