-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathshaky.dart
497 lines (415 loc) · 13.2 KB
/
shaky.dart
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// Copyright 2012 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'dart:async' as async;
import 'dart:html' as html;
import 'dart:math' as math;
import 'package:js/js.dart' as js;
final FONT = "20pt 'Gloria Hallelujah'";
// ShakyCanvas provides a way of drawing shaky lines on a normal
// HTML5 canvas element.
class ShakyCanvas {
ShakyCanvas(html.CanvasElement canvas)
: ctx = canvas.getContext("2d") {
ctx.lineWidth = 3;
ctx.font = FONT;
ctx.textBaseline = "middle";
}
final random = new math.Random();
final html.CanvasRenderingContext2D ctx;
var x0, y0;
moveTo(x0, y0) {
this.x0 = x0;
this.y0 = y0;
}
lineTo(x1, y1) {
shakyLine(x0, y0, x1, y1);
this.x0 = x1;
this.y0 = y1;
}
// Draw a shaky line between (x0, y0) and (x1, y1).
shakyLine(x0, y0, x1, y1) {
// Let $v = (d_x, d_y)$ be a vector between points $P_0 = (x_0, y_0)$ and $P_1 = (x_1, y_1)$.
var dx = x1 - x0;
var dy = y1 - y0;
// Let $l$ be the length of $v$.
var l = math.sqrt(dx * dx + dy * dy);
// Now we need to pick two random points that are placed
// on different sides of the line that passes through
// $P_1$ and $P_2$ and not very far from it if length of
// $P_1 P_2$ is small.
var K = math.sqrt(l) / 1.5;
var k1 = random.nextDouble();
var k2 = random.nextDouble();
var l3 = random.nextDouble() * K;
var l4 = random.nextDouble() * K;
// Point $P_3$: pick a random point on the line between $P_0$ and $P_1$,
// then shift it by vector $\frac{l_1}{l} (d_y, -d_x)$ which is a line's normal.
var x3 = x0 + dx * k1 + dy/l * l3;
var y3 = y0 + dy * k1 - dx/l * l3;
// Point $P_3$: pick a random point on the line between $P_0$ and $P_1$,
// then shift it by vector $\frac{l_2}{l} (-d_y, d_x)$ which also is a line's normal
// but points into opposite direction from the one we used for $P_3$.
var x4 = x0 + dx * k2 - dy/l * l4;
var y4 = y0 + dy * k2 + dx/l * l4;
// Draw a bezier curve through points $P_0$, $P_3$, $P_4$, $P_1$.
// Selection of $P_3$ and $P_4$ makes line "jerk" a little
// between them but otherwise it will be mostly straight thus
// creating illusion of being hand drawn.
ctx.moveTo(x0, y0);
ctx.bezierCurveTo(x3, y3, x4, y4, x1, y1);
}
// Draw a shaky bulb (used for line endings).
bulb(x0, y0) {
fuzziness() =>
random.nextDouble() * 2 - 1;
for (var i = 0; i < 3; i++) {
ctx.beginPath();
ctx.arc(x0 + fuzziness(), y0 + fuzziness(), 5, 0, math.PI * 2, true);
ctx.closePath();
ctx.fill();
}
}
// Draw a shaky arrowhead at the (x1, y1) as an ending
// for the line from (x0, y0) to (x1, y1).
arrowhead (x0, y0, x1, y1) {
var dx = x0 - x1;
var dy = y0 - y1;
var alpha = math.atan(dy / dx);
if (dy == 0) {
alpha = dx < 0 ? -math.PI : 0;
}
var alpha3 = alpha + 0.5;
var alpha4 = alpha - 0.50;
var l3 = 20;
var x3 = x1 + l3 * math.cos(alpha3);
var y3 = y1 + l3 * math.sin(alpha3);
ctx.beginPath();
moveTo(x3, y3);
lineTo(x1, y1);
ctx.stroke();
var l4 = 20;
var x4 = x1 + l4 * math.cos(alpha4);
var y4 = y1 + l4 * math.sin(alpha4);
ctx.beginPath();
moveTo(x4, y4);
lineTo(x1, y1);
ctx.stroke();
}
// Forward some methods to rendering context.
// Ideally we would just use
//
// noSuchMethod(mirror) => mirror.invokeOn(mirror);
//
// But that does not work on VM and does not entirely
// work on dart2js.
// So for now we will just use manual forwarding.
beginPath() => ctx.beginPath();
stroke() => ctx.stroke();
set strokeStyle(val) { ctx.strokeStyle = val; }
set fillStyle(val) { ctx.fillStyle = val; }
fillText(text, x0, y0) => ctx.fillText(text, x0, y0);
}
//
// Code below converts ASCII art into Line and Text elements.
//
// Size in pixels for a sigle character cell of ASCII art.
final CELL_SIZE = 15;
X(x) => x * CELL_SIZE + (CELL_SIZE / 2);
Y(y) => y * CELL_SIZE + (CELL_SIZE / 2);
// Auxiliary Point class used during parsing.
// Unfortunately Dart does not support structural classes or
// local classes so I had to polute library namespace with it.
class Point {
final x;
final y;
const Point(this.x, this.y);
}
// Line from (x0, y0) to (x1, y1) with the given color and decolartions
// at the start and end.
class Line {
Line(this.x0, this.y0, this.start, this.x1, this.y1, this.end, this.color);
var x0, y0, start, x1, y1, end, color;
draw(ctx) {
ctx.strokeStyle = color;
ctx.fillStyle = color;
ctx.beginPath();
ctx.moveTo(X(x0), Y(y0));
ctx.lineTo(X(x1), Y(y1));
ctx.stroke();
_ending(ctx, start, X(x1), Y(y1), X(x0), Y(y0));
_ending(ctx, end, X(x0), Y(y0), X(x1), Y(y1));
}
// Draw given type of ending on the (x1, y1).
_ending(canvas, type, x0, y0, x1, y1) {
switch (type) {
case "circle":
canvas.bulb(x1, y1);
break;
case "arrow":
canvas.arrowhead(x0, y0, x1, y1);
break;
}
}
}
// Text annotation at (x0, y0) with the given color.
class Text {
Text(this.x0, this.y0, this.text, this.color);
var x0, y0, text, color;
draw(ctx) {
ctx.fillStyle = color;
ctx.fillText(text, X(x0), Y(y0));
}
}
// Parses given ASCII art string into a list of figures.
parseASCIIArt(string) {
var lines = string.split('\n');
var height = lines.length;
var width = lines.fold(0, (w, line) => math.max(w, line.length));
var data = new List(height); // Matrix containing ASCII art.
// Get a character from the array or null if we are out of bounds.
// Useful in places where we inspect character's neighbors and peek
// out of bounds for boundary characters.
at(y, x) =>
(0 <= y && y < height && 0 <= x && x < width) ? data[y][x] : null;
// Convert strings into a mutable matrix of characters.
for (var y = 0; y < height; y++) {
var line = lines[y];
data[y] = new List(width);
for (var x = 0; x < line.length; x++) {
data[y][x] = line[x];
}
for (var x = line.length; x < width; x++) {
data[y][x] = " ";
}
}
// Returns true iff the character can be part of the line.
isPartOfLine(x, y) {
var c = at(y, x);
return c == "|" || c == "-" || c == "+" || c == "~" || c == "!";
}
// If character represents a color modifier returns CSS color.
toColor(x, y) {
switch (at(y, x)) {
case "~": case "!": return "#666";
}
}
// Returns true iff characters is line ending decoration.
isLineEnding(x, y) {
var c = at(y, x);
return c == "*" || c == "<" || c == ">" || c == "^" || c == "v";
}
// Finds a character that belongs to unextracted line.
findLineChar() {
for (var y = 0; y < height; y++) {
for (var x = 0; x < width; x++) {
if (data[y][x] == '|' || data[y][x] == '-') {
return new Point(x, y);
}
}
}
}
// Converts line's character to the direction of line's growth.
var dir = { "-": const Point(1, 0), "|": const Point(0, 1)};
// Erases character that belongs to the extracted line.
eraseChar(x, y, dx, dy) {
switch (at(y, x)) {
case "|":
case "-":
case "*":
case ">":
case "<":
case "^":
case "v":
case "~":
case "!":
data[y][x] = " ";
return;
case "+":
dx = 1 - dx;
dy = 1 - dy;
data[y][x] = " ";
switch (at(y - dy, x - dx)) {
case "|":
case "!":
case "+":
data[y][x] = "|";
return;
case "-":
case "~":
case "+":
data[y][x] = "-";
return;
}
switch (at(y + dy, x + dx)) {
case "|":
case "!":
case "+":
data[y][x] = "|";
return;
case "-":
case "~":
case "+":
data[y][x] = "-";
return;
}
return;
}
}
// Erase the given extracted line.
erase(line) {
var dx = line.x0 != line.x1 ? 1 : 0;
var dy = line.y0 != line.y1 ? 1 : 0;
if (dx != 0 || dy != 0) {
var x = line.x0 + dx, y = line.y0 + dy;
var x_ = line.x1 - dx, y_ = line.y1 - dy;
while (x <= x_ && y <= y_) {
eraseChar(x, y, dx, dy);
x += dx;
y += dy;
}
eraseChar(line.x0, line.y0, dx, dy);
eraseChar(line.x1, line.y1, dx, dy);
} else {
eraseChar(line.x0, line.y0, dx, dy);
}
}
var figures = []; // List of extracted figures.
// Extract a single line and erase it from the ascii art matrix.
extractLine() {
var ch = findLineChar();
if (ch == null) return false;
var d = dir[data[ch.y][ch.x]];
// Find line's start by advancing in the oposite direction.
var x0 = ch.x;
var y0 = ch.y;
var color;
while (isPartOfLine(x0 - d.x, y0 - d.y)) {
x0 -= d.x;
y0 -= d.y;
if (color == null) color = toColor(x0, y0);
}
var start = null;
if (isLineEnding(x0 - d.x, y0 - d.y)) {
// Line has a decorated start. Extract is as well.
x0 -= d.x;
y0 -= d.y;
start = (data[y0][x0] == "*") ? "circle" : "arrow";
}
// Find line's end by advancing forward in the given direction.
var x1 = ch.x;
var y1 = ch.y;
while (isPartOfLine(x1 + d.x, y1 + d.y)) {
x1 += d.x;
y1 += d.y;
if (color == null) {
color = toColor(x1, y1);
}
}
var end = null;
if (isLineEnding(x1 + d.x, y1 + d.y)) {
// Line has a decorated end. Extract it.
x1 += d.x;
y1 += d.y;
end = (data[y1][x1] == "*") ? "circle" : "arrow";
}
// Create line object and erase line from the ascii art matrix.
var line = new Line(x0, y0, start, x1, y1, end, color == null ? "black" : color);
figures.add(line);
erase(line);
// Adjust line start and end to accomodate for arrow endings.
// Those should not intersect with their targets but should touch them
// instead. Should be done after erasure to ensure that erase deletes
// arrowheads.
if (start == "arrow") {
line.x0 -= d.x;
line.y0 -= d.y;
}
if (end == "arrow") {
line.x1 += d.x;
line.y1 += d.y;
}
return true;
}
// Extract all non space characters that were left after line extraction
// as text objects.
extractText() {
for (var y = 0; y < height; y++) {
for (var x = 0; x < width; x++) {
if (data[y][x] != ' ') {
// Find the end of the text annotation by searching for a space.
var start = x, end = x;
while ((end < width) && (data[y][end] != " ")) end++;
var text = data[y].getRange(start, end).join('');
// Check if it can be concatenated with a previously found text annotation.
var prev = figures[figures.length - 1];
if ((prev is Text) && (prev.x0 + prev.text.length + 1) == start) {
// If they touch concatentate them.
prev.text = "${prev.text} $text";
} else {
// Look for a grey color modifiers.
var color = "black";
if (text[0] == "\\" && text[text.length - 1] == "\\") {
text = text.substring(1, text.length - 1);
color = "#666";
}
figures.add(new Text(x, y, text, color));
}
x = end;
}
}
}
}
while (extractLine()); // Extract all lines.
extractText(); // Extract all text.
return figures;
}
// Draw a diagram from the ascii art contained in the #textarea.
drawDiagram() {
var figures = parseASCIIArt(html.querySelector("#textarea").value);
// Compute required canvas size.
var width = 0;
var height = 0;
for (var figure in figures) {
if (figure is Line) {
width = math.max(width, X(figure.x1 + 1));
height = math.max(height, Y(figure.y1 + 1));
}
}
var canvas = html.querySelector("#canvas");
canvas.width = width.toInt();
canvas.height = height.toInt();
var ctx = new ShakyCanvas(canvas);
for (var figure in figures) figure.draw(ctx);
}
void main() {
html.querySelector("#textarea").onChange.listen((e) => drawDiagram());
html.querySelector("#textarea").onKeyUp.listen((e) => drawDiagram());
html.querySelector("#save").onClick.listen((e) {
var a = new html.AnchorElement()
..href = html.querySelector("#canvas").toDataUrl("image/png")
..attributes['download'] = html.querySelector("#name").value;
html.document.body.nodes.add(a);
new async.Timer(const Duration(seconds: 1), () => a.remove());
try {
a.click();
} catch (e) {
a.$dom_dispatchEvent(new html.Event("click"));
}
});
try {
if (js.context.window.FONTS_ACTIVE) return;
} catch (e) { }
js.context.drawDiagram = new js.FunctionProxy(drawDiagram);
drawDiagram();
}