-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrowhead.js-e
230 lines (210 loc) · 5.69 KB
/
arrowhead.js-e
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
## Copyright (c) 2016, Empirical Modelling Group
## All rights reserved.
##
## See LICENSE.txt
${{
Arrowhead = function(start, end, length, width, filled) {
this.start = start;
this.end = end;
this.length = length;
this.width = width;
this.filled = filled;
};
Arrowhead.prototype.toString = function () {
return "Arrowhead(" + Eden.edenCodeForValues(this.start, this.end, this.length, this.width,
this.filled) + ")";
};
Arrowhead.prototype.getEdenCode = Arrowhead.prototype.toString;
Arrowhead.prototype.draw = function (context, scale, x1, y1, gradient1, reverse1, x2, y2, gradient2, reverse2) {
var invGradient, len1, len2, len3, p0x, p0y, p1x, p1y, p2x, p2y, p3x, p3y;
var lineWidth = context.lineWidth;
var adjustment = lineWidth / 4;
var filled = this.filled;
var width = this.width;
var length = this.length;
if (length === undefined) {
if (lineWidth <= 4) {
length = 10 / scale;
} else {
length = lineWidth * 2.5 / scale;
}
} else {
length = length / scale;
}
if (width === undefined) {
width = length / 2;
} else {
width = (width / 2) / scale;
}
context.fillStyle = context.strokeStyle;
if (this.start) {
invGradient = - 1 / gradient1;
if ((reverse1 && invGradient == Infinity) || (!reverse1 && invGradient == -Infinity)) {
//Horizontal left
p1x = x1 + length;
p1y = y1 - width;
p2x = p1x;
p2y = y1 + width;
p3x = x1 + adjustment;
p3y = y1;
} else if (gradient1 == 0) {
//Horizontal right
p1x = x1 - length;
p1y = y1 - width;
p2x = p1x;
p2y = y1 + width;
p3x = x1 - adjustment;
p3y = y1;
} else if ((reverse1 && gradient1 == -Infinity) || (!reverse1 && gradient1 == Infinity)) {
//Vertical upward
p1x = x1 - width;
p1y = y1 + length;
p2x = x1 + width;
p2y = p1y;
p3x = x1;
p3y = y1 + adjustment;
} else if (gradient1 == Infinity || gradient1 == -Infinity) {
//Vertical downward
p1x = x1 - width;
p1y = y1 - length;
p2x = x1 + width;
p2y = p1y;
p3x = x1;
p3y = y1 - adjustment;
} else {
len1 = Math.sqrt(length * length / (1 + gradient1 * gradient1));
if (reverse1) {
len1 = -len1;
}
p0x = x1 + len1;
p0y = y1 + len1 * gradient1;
len2 = Math.sqrt(width * width / (1 + invGradient * invGradient));
p1x = p0x - len2;
p1y = p0y - len2 * invGradient;
p2x = p0x + len2;
p2y = p0y + len2 * invGradient;
len3 = Math.sqrt(adjustment * adjustment / (1 + gradient1 * gradient1));
p3x = x1 - len3;
p3y = y1 - len3 * gradient1
}
context.beginPath();
context.moveTo(p1x, p1y);
context.lineTo(p3x, p3y);
context.lineTo(p2x, p2y);
if (filled) {
context.closePath();
context.fill();
}
context.stroke();
}
if (this.end) {
invGradient = - 1 / gradient2;
if ((reverse2 && invGradient == -Infinity) || (!reverse2 && invGradient == Infinity)) {
//Horizontal left
p1x = x2 + length;
p1y = y2 - width;
p2x = p1x;
p2y = y2 + width;
p3x = x2 + adjustment;
p3y = y2;
} else if (gradient2 == 0) {
//Horizontal right
p1x = x2 - length;
p1y = y2 - width;
p2x = p1x;
p2y = y2 + width;
p3x = x2 - adjustment;
p3y = y2;
} else if ((reverse2 && gradient2 == Infinity) || (!reverse2 && gradient2 == -Infinity)) {
//Vertical upward
p1x = x2 - width;
p1y = y2 + length;
p2x = x2 + width;
p2y = p1y;
p3x = x2;
p3y = y2 + adjustment;
} else if (gradient2 == Infinity || gradient2 == -Infinity) {
//Vertical downward
p1x = x2 - width;
p1y = y2 - length;
p2x = x2 + width;
p2y = p1y;
p3x = x2;
p3y = y2 - adjustment;
} else {
len1 = Math.sqrt(length * length / (1 + gradient2 * gradient2));
if (reverse2) {
len1 = -len1;
}
p0x = x2 - len1;
p0y = y2 - len1 * gradient2;
len2 = Math.sqrt(width * width / (1 + invGradient * invGradient));
p1x = p0x - len2;
p1y = p0y - len2 * invGradient;
p2x = p0x + len2;
p2y = p0y + len2 * invGradient;
len3 = Math.sqrt(adjustment * adjustment / (1 + gradient2 * gradient2));
p3x = x2 - len3;
p3y = y2 - len3 * gradient2
}
context.beginPath();
context.moveTo(p1x, p1y);
context.lineTo(p3x, p3y);
context.lineTo(p2x, p2y);
if (filled) {
context.closePath();
context.fill();
}
context.stroke();
}
};
}}$;
#! Create an Arrowhead line.
# @param start Whether to draw the arrowhead at the start
# @param end Whether to draw the arrowhead at the end
# @param length Length of the arrowhead
# @param width Width of the arrowhead. IF undefined, a proportional value is automatically chosen.
# @param [filled] Whether to fill the arrowhead or just draw an outline. Defaults to true.
#
# #canvas #arrow #arrowhead #shape #arrow #library #draw
func Arrowhead {
${{
var argsProcessed;
var numArgs = arguments.length;
var arg = arguments[0];
var argType = typeof(arg);
var start, end, length, width, filled;
if ((argType == "boolean" && numArgs >= 2 &&
(typeof(arguments[1]) == "boolean" || arguments[1] === undefined)) ||
numArgs == 5
) {
start = arg;
end = arguments[1];
argsProcessed = 2;
arg = arguments[2];
argType = typeof(arg);
} else {
start = false;
end = true;
argsProcessed = 0;
}
if (argType == "number" || arg === undefined) {
length = arg;
argsProcessed++;
arg = arguments[argsProcessed];
argType = typeof(arg);
}
if (argType == "number" || arg === undefined) {
width = length;
length = arg;
argsProcessed++;
}
if (argsProcessed < numArgs) {
filled = arguments[argsProcessed];
}
if (filled === undefined) {
filled = true;
}
return new Arrowhead(start, end, length, width, filled);
}}$;
}