-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgofigure.js
206 lines (186 loc) · 8.95 KB
/
gofigure.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*! gofigure v0.0.3 */
/* global $:false, Raphael:false */
var gofigure = function() {
var box = function(x, y, w, h, options) {
var radius = $.extend({radius: 12.875}, options).radius;
var cx = [7.109 / 12.875 * radius, 12.875 / 12.875 * radius];
var cy = [5.765 / 12.875 * radius, 12.876 / 12.875 * radius];
w -= 12.875 / 12.875 * radius * 2;
h -= 12.876 / 12.875 * radius * 2;
x += 12.875 / 12.875 * radius;
return {
pathString: "M" + (x - options.strokeWidth/2.0 ) + "," + y + "h" + options.strokeWidth/2.0 +
"h" + w + "c" + cx[0] + ",0," + cx[1] + "," + cy[0] + "," + cx[1] + "," + cy[1] + " " +
"v" + h + "c0," + cx[0] + ",-" + cy[0] + "," + cx[1] + ",-" + cx[1] + "," + cy[1] + " " +
"h-" + w + "c-" + cx[0] + ",0,-" + cx[1] + ",-" + cy[0] + ",-" + cx[1] + ",-" + cy[1] + " " +
"v-" + h + "c0,-" + cx[0] + "," + cy[0] + ",-" + cx[1] + "," + cx[1] + ",-" + cy[1]
};
};
var circle = function(x, y, radius, options) {
return box(x-radius, y-radius, radius*2.001, radius*2.001, $.extend(options, { radius: radius }));
};
var round = function(num, places) {
return Math.round(num * Math.pow(10, places)) / Math.pow(10, places);
};
var line = function(x1, y1, x2, y2, options) {
var heads = (options || {}).arrowheads || "none";
var len = (options || {}).len || 10;
var ang = Math.atan2((y2-y1), (x2-x1));
var begin = (heads != "begin" && heads != "both") ? "" : ("M" + x1 + "," + y1 +
"l" + round(Math.cos(ang - Math.PI/4)*len, 3) + "," + round(Math.sin(ang - Math.PI/4)*len, 3) +
"M" + x1 + "," + y1 +
"l" + round(Math.cos(ang + Math.PI/4)*len, 3) + "," + round(Math.sin(ang + Math.PI/4)*len, 3));
var end = (heads != "end" && heads != "both") ? "" : ("M" + x2 + "," + y2 +
"l" + round(Math.cos(ang - Math.PI*3/4)*len, 3) + "," + round(Math.sin(ang - Math.PI*3/4)*len, 3) +
"M" + x2 + "," + y2 +
"l" + round(Math.cos(ang + Math.PI*3/4)*len, 3) + "," + round(Math.sin(ang + Math.PI*3/4)*len, 3));
return {
pathString: begin + "M" + x1 + "," + y1 + "l" + (x2 - x1) + "," + (y2 - y1) + end
};
};
var path = function(points, options) {
console.log(points.length, points);
if (points.length <= 1) return { pathString: "" };
if (points.length === 2) return line(points[0][0], points[0][1], points[1][0], points[1][1], options);
var headBegin = /(begin|both)/.test(options.arrowheads) ? "begin" : "none";
var headEnd = /(end|both)/.test(options.arrowheads) ? "end" : "none";
var l = line(points[0][0], points[0][1], points[1][0], points[1][1], $.extend(options, {"arrowheads" : headBegin}));
for (var i = 1; i < (points.length - 2); i++) {
l.pathString += line(points[i][0], points[i][1], points[i+1][0], points[i+1][1], $.extend(options, {"arrowheads" : "none"})).pathString;
}
l.pathString += line(points[points.length - 2][0], points[points.length - 2][1], points[points.length - 1][0], points[points.length - 1][1], $.extend(options, {"arrowheads" : headEnd})).pathString;
return l;
};
var drawcenteredtext = function(canvas, x, y, text, options) { //Yes, this is quite ugly code, but it seems to work
var result = drawtext(canvas, -1000, -1000, text, options).pathString;
var tmp = result.replace(/[A-Z](,?[A-Z])*/g, ",").replace(/^,+/, "").replace(/,+$/g, "");
var parts = tmp.split(",");
var maxRight = -2000;
var minLeft = 200000;
for (var i = 0; i < parts.length; i = i + 2) {
var n = parseFloat(parts[i]);
maxRight = maxRight < n ? n : maxRight;
minLeft = minLeft < n ? minLeft : n;
}
return drawtext(canvas, x - (maxRight - minLeft)/2.0, y, text, options);
};
var drawtext = function(canvas, x, y, text, options) {
options = $.extend({fontsize : 20}, options);
var pathString = [];
var line = canvas.print(x, y, text, canvas.getFont("Vegur"), options.fontsize);
pathString.push(line[0].getAttribute("d"));
line[0].remove();
return { pathString: pathString.join(",") };
};
var gofigureid = 0;
var gofigure_lineid = 0;
function setupLine(canvas, group, part) {
var line = canvas.path(part.path.pathString).attr({
"stroke": "#000",
"stroke-width": 0
});
var identifier = group + "-animate-" + gofigure_lineid++;
line.node.setAttribute("class", identifier);
return { line : line, identifier : identifier, options : part.options };
}
function animate(canvas, lineObj, callback) {
var line = lineObj.line;
var length = line.getTotalLength();
var current = null;
$("path." + lineObj.identifier + '[fill*="none"]').animate({
"to": 1
}, {
duration: lineObj.options.duration || 1000,
step: function(pos, fx) {
var offset = length * fx.pos;
var subpath = line.getSubpath(0, offset);
var subline = canvas.path(subpath).attr({
"stroke-width": lineObj.options.strokeWidth || 3,
stroke: "#000",
fill: lineObj.options.fill || "none"
});
if (current) {
current.remove();
}
current = subline;
},
done: function() {
line.remove();
if (callback) callback();
}
});
}
function prepare(step, path, options) {
step.parts.push({ path: path, options: options});
return step;
}
function doAnimate(canvas, group, parts) {
var lines = [];
for(var i in parts) {
lines.push(setupLine(canvas, group, parts[i]));
}
function run(lines, i) {
if (i >= lines.length) return;
animate(canvas, lines[i], function() { run(lines, i + 1); });
}
run(lines, 0);
}
function createStep(canvas, group) {
var step = { parts: [] };
var defaults = function(options){ return $.extend({ strokeWidth : 3 }, options); };
step.box = function(x, y, width, height, options) {
options = defaults(options);
return prepare(step, box(x, y, width, height, options), options);
};
step.centeredText = function(x, y, text, options) {
options = defaults($.extend({ strokeWidth: -1, fill: "#000" }, options));
return prepare(step, drawcenteredtext(canvas, x, y, text, options), options);
};
step.circle = function(x, y, radius, options) {
options = defaults(options);
return prepare(step, circle(x, y, radius, options), options);
};
step.path = function(points, options) {
options = defaults(options);
return prepare(step, path(points, options), options);
};
step.line = function(xfrom, yfrom, xto, yto, options) {
options = defaults(options);
return prepare(step, line(xfrom, yfrom, xto, yto, options), options);
};
step.arrow = function(xfrom, yfrom, xto, yto, options) {
options = defaults($.extend({ arrowheads : "both"}, options));
return prepare(step, line(xfrom, yfrom, xto, yto, options), options);
};
step.animate = function() {
doAnimate(canvas, group, step.parts);
};
return step;
}
function create(containerId, width, height) {
var canvas = Raphael(containerId, width, height);
$("#" + containerId + " svg").wrap("<div>");
var group = "group" + gofigureid++;
return {
bindClick : function(steps) {
var i = 0;
$("#" + containerId + " svg").click(function() {
if (steps[i]) {
steps[i].animate();
}
i++;
});
},
animate: function() {},
box: function(x, y, width, height, options) { return createStep(canvas, group).box(x, y, width, height, options); },
centeredText: function(x, y, text, options) { return createStep(canvas, group).centeredText(x, y, text, options); },
circle: function(x, y, radius, options) { return createStep(canvas, group).circle(x, y, radius, options); },
path: function(points, options) { return createStep(canvas, group).path(points, options); },
line: function(xfrom, yfrom, xto, yto, options) { return createStep(canvas, group).line(xfrom, yfrom, xto, yto, options); },
arrow: function(xfrom, yfrom, xto, yto, options) { return createStep(canvas, group).arrow(xfrom, yfrom, xto, yto, options); },
};
}
return {
create: create
};
}();