Debian Codegraph @

Nasal/canvas/draw/draw.nas

raw
1 #
2 # canvas.draw library
3 # created 12/2018 by jsb
4 # based on plot2D.nas from the oscilloscope add-on by R. Leibner
5 #
6 # Contains functions to draw path elements on an existing canvas group.
7 # - basic shapes
8 # - grids
9 # - scale marks
10 #
11 # Basic shapes:
12 # These are macros calling existing API command for path elements.
13 # They return a path element, no styling done. You can easily do by passing
14 # the returned element to other existing functions like setColor, e.g.
15 # var myCircle = canvas.circle(myCanvasGroup, 10, 30, 30).setColor(myColor)
16 #
17 # Grids:
18 # draw horizontal and vertical lines
19 # from the Oscilloscope add-on by rleibner with a few modifications
20 #
21 # Scale marks:
22 # Draw equidistant lines ("marker", "ticks") perpendicular to a baseline.
23 # Baseline can be a horizontal or vertical line or a part of a circle.
24 # This is a building block for compass rose and tapes.
25 #
26
27 var draw = {
28 # draw line; from and to must be vectors [x,y]
29 line: func(cgroup, from, to) {
30 var path = cgroup.createChild("path", "line");
31 return path.moveTo(from[0], from[1]).lineTo(to[0], to[1]);
32 },
33
34 # draw horizontal line;
35 # from: optional, vector, defaults to [0,0]
36 # or scalar
37 hline: func(cgroup, length, from = nil) {
38 if (from == nil) {
39 to = [length, 0];
40 from = [0,0];
41 }
42 elsif (typeof(from) == "scalar") {
43 to = [num(from) + length, 0];
44 from = [num(from) ,0];
45 }
46 else to = [from[0] + length, from[1]];
47 me.line(cgroup, from, to);
48 },
49
50 # draw vertical line;
51 # from: optional, vector, defaults to [0,0]
52 # or scalar
53 vline: func(cgroup, length, from = nil) {
54 if (from == nil) {
55 to = [0, length];
56 from = [0,0];
57 }
58 elsif (typeof(from) == "scalar") {
59 to = [0, num(from) + length];
60 from = [0, num(from)];
61 }
62 else to = [from[0], from[1] + length];
63 me.line(cgroup, from, to);
64 },
65 # if center_x is given as vector, its first two elements define the center
66 # and center_y is ignored
67 circle: func(cgroup, radius, center_x = nil, center_y = nil) {
68 var path = cgroup.createChild("path", "circle");
69 return path.circle(radius, center_x, center_y);
70 },
71
72 # if center_x is given as vector, its first two elements define the center
73 # and center_y is ignored
74 ellipse: func(cgroup, radius_x, radius_y, center_x = nil, center_y = nil) {
75 var path = cgroup.createChild("path", "ellipse");
76 return path.ellipse(radius_x, radius_y, center_x, center_y);
77 },
78
79 # draw part of a circle
80 # radius as integer (for circle) or [rx,ry] (for ellipse) in pixels.
81 # center vector [x,y]
82 # from_deg begin of arc in degree (0 = north, increasing clockwise)
83 # to_deg end of arc
84 arc: func(cgroup, radius, center, from_deg = nil, to_deg = nil) {
85 if (from_deg == nil)
86 return me.circle(radius, center);
87
88 var path = cgroup.createChild("path", "arc");
89 from_deg *= D2R;
90 to_deg *= D2R;
91 var (rx, ry) = (typeof(radius) == "vector") ? [radius[0], radius[1]] : [radius, radius];
92 var (fs, fc) = [math.sin(from_deg), math.cos(from_deg)];
93 var dx = (math.sin(to_deg) - fs) * rx;
94 var dy = (math.cos(to_deg) - fc) * ry;
95
96 path.moveTo(center[0] + rx*fs, center[1] - ry*fc);
97 if(abs(to_deg - from_deg) > 180*D2R) {
98 path.arcLargeCW(rx, ry, 0, dx, -dy);
99 }
100 else {
101 path.arcSmallCW(rx, ry, 0, dx, -dy);
102 }
103 return path;
104 },
105
106 # x, y is top, left corner
107 rectangle: func(cgroup, width, height, x = 0, y = 0, rounded = nil) {
108 var path = cgroup.createChild("path", "rectangle");
109 return path.rect(x, y, width, height, {"border-radius": rounded});
110 },
111
112 # x, y is top, left corner
113 square: func(cgroup, length, center_x = 0, center_y = 0, cfg = nil) {
114 var path = cgroup.createChild("path", "square");
115 return path.square(center_x, center_y, length, cfg = nil);
116 },
117
118 # deltoid draws a kite (dy1 > 0 and dy2 > 0) or a arrow head (dy2 < 0)
119 # dx = width
120 # dy1 = height of "upper" triangle
121 # dy2 = height of "lower" triangle, < 0 draws an arrow head
122 # x, y = position of tip
123 deltoid: func (cgroup, dx, dy1, dy2, x = 0, y = 0) {
124 var path = cgroup.createChild("path", "deltoid");
125 path.moveTo(x, y)
126 .line(-dx/2, dy1)
127 .line(dx/2, dy2)
128 .line(dx/2, -dy2)
129 .close();
130 return path;
131 },
132
133 # draw a "diamond"
134 # dx: width
135 # dy: height
136 rhombus: func(cgroup, dx, dy, center_x = 0, center_y = 0) {
137 return draw.deltoid(cgroup, dx, dy/2, dy/2, center_x, center_y - dy/2);
138 },
139 };
140
141 #aliases
142 draw.diamond = draw.rhombus;
143
144 #base class for styles
145 draw.style = {
146 new: func() {
147 var obj = {
148 parents: [draw.style],
149 _color: [255, 255, 255, 1],
150 _color_fill: nil,
151 _stroke_width: 1,
152 };
153 return obj;
154 },
155
156 #set value of existing(!) key
157 set: func(key, value) {
158 if (contains(me, key)) {
159 me[key] = value;
160 return me;
161 }
162 return nil;
163 },
164
165 get: func(key) {
166 return me[key];
167 },
168
169 setColor: func() {
170 me._color = arg;
171 return me;
172 },
173
174 getColor: func() {
175 return me._color;
176 },
177
178 setColorFill: func() {
179 me._color_fill = arg;
180 return me;
181 },
182
183 setStrokeLineWidth: func() {
184 me._stroke_width = arg;
185 return me;
186 },
187 };
188
189 #
190 # marksStyle - parameter set for draw.marks*
191 # Interpretation depends on the draw function used. In general, marks are
192 # lines drawn perpendicular to a baseline in certain intervals. 'Big' and
193 # 'small' marks are supported by means of 'subdivisions'.
194 # Some values are expressed as percentage to allow easy scaling.
195 # Again: Interpretation depends on the draw function using this style.
196 #
197 draw.marksStyle = {
198 # constants to align marks relative to baseline
199 MARK_LEFT: -1,
200 MARK_UP: -1,
201 MARK_CENTER: 0,
202 MARK_RIGHT: 1,
203 MARK_DOWN: 1,
204
205 MARK_IN: -1, # from radius to center of circle
206 MARK_OUT: 1, # from radius to outside
207
208 new: func() {
209 var obj = {
210 parents: [draw.marksStyle, draw.style.new()],
211 baseline_width: 0, # stroke of baseline
212 mark_length: 0.8, # length of a division marker in %interval or %radius
213 mark_offset: 0, # in %mark_length, see setMarkLength below
214 mark_width: 1, # in pixel
215 subdivisions: 0, # number of smaller marks between to marks
216 subdiv_length: 0.5, # in %mark_length
217 };
218 return obj;
219 },
220
221 setBaselineWidth: func(value) {
222 me.baseline_width = num(value) or 0;
223 return me;
224 },
225
226 setMarkLength: func(value) {
227 me.mark_length = num(value) or 1;
228 return me;
229 },
230
231 # position of mark relative to baseline, call this with MARK_* defined above
232 # -1 = left, 0 = center, 1 = right
233 setMarkOffset: func(value) {
234 if (num(value) == nil) return nil;
235 me.mark_offset = value;
236 return me;
237 },
238
239 setMarkWidth: func(value) {
240 me.mark_width = num(value) or 1;
241 return me;
242 },
243
244 setSubdivisions: func(value) {
245 me.subdivisions = int(value) or 0;
246 return me;
247 },
248
249 setSubdivisionLength: func(value) {
250 me.subdiv_length = num(value) or 0.5;
251 return me;
252 },
253 };
254
255 # draw.marksLinear: draw marks for a linear scale on a canvas group, e.g. speed tape
256 # mark lines are draws perpendicular to baseline
257 # orientation of baseline; "up", "down", "left", "right"
258 # num_marks number of marks to draw
259 # interval distance between marks (pixel)
260 # style marksStyle hash with more parameters
261
262 draw.marksLinear = func(cgroup, orientation, num_marks, interval, style)
263 {
264 if (!isa(style, draw.marksStyle)) {
265 logprint(DEV_WARN, "draw.marks: invalid style argument.");
266 return nil;
267 }
268 orientation = chr(string.tolower(orientation[0]));
269 if (orientation == "v") orientation = "d";
270 if (orientation == "h") orientation = "r";
271 var marks = cgroup.createChild("path", "marks");
272
273 if (style.baseline_width > 0) {
274 var length = interval * (num_marks - 1);
275 if (orientation == "d") {
276 marks.vert(length);
277 }
278 elsif(orientation == "u") {
279 marks.vert(-length);
280 }
281 elsif(orientation == "r") {
282 marks.horiz(length);
283 }
284 elsif(orientation == "l") {
285 marks.horiz(-length);
286 }
287 }
288
289 var mark_length = interval * style.mark_length;
290 if (style.subdivisions > 0) {
291 interval /= (style.subdivisions + 1);
292 var subdiv_length = mark_length * style.subdiv_length;
293 };
294 marks.setColor(style.getColor());
295
296 var offset0 = 0.5 * style.mark_offset - 0.5;
297 for (var i = 0; i < num_marks; i += 1) {
298 for (var j = 0; j <= style.subdivisions; j += 1) {
299 var length = (j == 0) ? mark_length : subdiv_length;
300 var translation = interval * (i*(style.subdivisions + 1) + j);
301 var offset = offset0 * length;
302 if (orientation == "d") {
303 marks.moveTo(offset, translation)
304 .horiz(length);
305 }
306 elsif (orientation == "u") {
307 marks.moveTo(offset, -translation)
308 .horiz(length);
309 }
310 elsif (orientation == "r") {
311 marks.moveTo(translation, offset)
312 .vert(length);
313 }
314 elsif (orientation == "l") {
315 marks.moveTo(-translation, offset)
316 .vert(length);
317 }
318 }
319 }
320 while (j) {
321 marks.pop_back();
322 j -= 1;
323 }
324 return marks;
325 }
326
327 # radius of baseline (circle)
328 # interval distance of marks in degree
329 # phi_start position of first mark in degree (default 0 = north)
330 # phi_stop position of last mark in degree (default 360)
331 draw.marksCircular = func(cgroup, radius, interval, phi_start = 0, phi_stop = 360, style = nil) {
332 if (style == nil) {
333 style = draw.marksStyle.new();
334 }
335 if (!isa(style, draw.marksStyle)) {
336 logprint(DEV_WARN, "draw.marksCircular: invalid style argument");
337 return nil;
338 }
339 # normalize
340 while (phi_start >= 360) { phi_start -= 360; }
341 while (phi_stop > 360) { phi_stop -= 360; }
342 if (phi_start > phi_stop) {
343 phi_stop += 360;
344 }
345
346 if (style.baseline_width > 0) {
347 var marks = draw.arc(cgroup, radius, [0,0], phi_start, phi_stop)
348 .set("id", "marksCircular")
349 .setColor(style.getColor());
350 }
351 else {
352 var marks = cgroup.createChild("path", "marksCircular").setColor(style.getColor());
353 }
354
355 var mark_length = style.mark_length * radius;
356 var subd_length = style.subdiv_length * mark_length;
357 interval *= D2R;
358 phi_start *= D2R;
359 phi_stop *= D2R;
360 var phi_s = interval / (style.subdivisions + 1);
361 var x = y = l = 0;
362 var offset0 = 0.5 * style.mark_offset - 0.5;
363 for (var phi = phi_start; phi <= phi_stop; phi += interval) {
364 for (var j = 0; j <= style.subdivisions; j += 1) {
365 var p = phi + j * phi_s;
366 #print(p*R2D);
367 x = math.sin(p);
368 y = -math.cos(p);
369 l = (j == 0) ? mark_length : subd_length;
370 r = radius + offset0 * l;
371 marks.moveTo(x * r, y * r)
372 .line(x * l, y * l);
373 }
374 }
375 while (j) {
376 marks.pop_back();
377 j -= 1;
378 }
379 return marks;
380 }
381
382 # draw.grid
383 # 1) (cgroup, [sizeX, sizeY], dx, dy, border = 1)
384 # 2) (cgroup, nx, ny, dx, dy, border = 1)
385 # size [width, height] in pixels.
386 # nx, ny number of lines in x/y direction
387 # dx tiles width in pixels.
388 # dy tiles height in pixels.
389 # border optional as boolean, True by default.
390 draw.grid = func(cgroup) {
391 var i = 0;
392 var (s, n) = ([0, 0], []);
393 if (typeof(arg[i]) == "vector") {
394 s = arg[i];
395 i += 1;
396 }
397 else {
398 var n = [arg[i], arg[i + 1]];
399 i += 2;
400 }
401 var dx = arg[i];
402 i += 1;
403 var dy = dx;
404 var border = 1;
405 if (size(arg) - 1 >= i) { dy = arg[i]; i += 1; }
406 if (size(arg) - 1 >= i) { border = arg[i]; }
407 if (size(n) == 2) {
408 s[0] = n[0] * dx - 1;
409 s[1] = n[1] * dy - 1;
410 }
411 #print("size: ", s[0], " ", s[1], " d:", dx, ", ", dy, " b:", border);
412 var grid = cgroup.createChild("path", "grid").setColor([255, 255, 255, 1]);
413 var (x0, y0) = border ? [0, 0] : [dx, dy];
414 for (var x = x0; x <= s[0]; x += dx) {
415 grid.moveTo(x, 0).vertTo(s[1]);
416 }
417 for (var y = y0; y <= s[1]; y += dy) {
418 grid.moveTo(0, y).horizTo(s[0]);
419 }
420 if (border) {
421 grid.moveTo(s[0], 0).vertTo(s[1]).horizTo(0);
422 }
423 return grid;
424 }
425
426 draw.arrow = func(cgroup, length, origin_center=0) {
427 #var path = cgroup.createChild("path", "arrow");
428 var tip_size = length * 0.1;
429 var offset = (origin_center) ? -length/2 : 0;
430 var arrow = draw.deltoid(cgroup, tip_size, tip_size, 0, 0, offset);
431 #if (offset) { arrow.moveTo(0, offset); }
432 arrow.line(0,length);
433 return arrow;
434 }