my eye

author Angelo Gladding

name SVG

published 2025-07-04T17:11:35.222287-07:00

type entry

updated 2025-07-05T13:56:46.002006-07:00

url /svg, /2025/07/05/zv

visibility public

Content

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
*****UNDER RESURRECTION*****

## Simple Manual Icon Editor

$ icons = {
$   "bookmark": (576, 512, """
$     M 144,32
$     C 136,32 128,40 128,48
$     L 128,480
$     L 288,384
$     L 448,480
$     L 448,48
$     C 448,40 440,32 432,32
$     Z""")
$ }

<header class=toolbar>
<div id=gallery>
$for icon_name, (icon_width, icon_height, icon_data) in icons.items():
    <svg title="$icon_name" xmlns="http://www.w3.org/2000/svg"
    viewBox="0 0 $icon_width $icon_height"><path fill="#666" d="$dedent(icon_data).strip()"
    /></svg>
</div>
<textarea id=source></textarea>
<div id=icon>
    <div id=preview-large></div>
    <div id=preview-medium></div>
    <div id=preview-small></div>
    <a id=save download=icon.svg href="">download</a>
</div>
<div id=toolbox>
    <div id=coords>&nbsp;</div>
    <button class=add data-shape=curve>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path fill="#666" d="M 120,352C 120,192 224,88 384,88L 384,136C 252,136 168,220 168,352ZM 96,352L 192,352L 192,448L 96,448ZM 384,64L 480,64L 480,160L 384,160Z" /></svg>
    </button>
    <button class=add data-shape=line>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path fill="#666" d="M 156,352L 384,124L 420,160L 192,388ZM 96,352L 192,352L 192,448L 96,448ZM 384,64L 480,64L 480,160L 384,160Z" /></svg>
    </button>
    <button id=showcase>Showcase</button>
</div>
</header>

<div id=editor>
<div id=canvas></div>
<div id=vrulerl class="ruler vruler"></div>
<div id=vrulerr class="ruler vruler"></div>
<div id=hruler class=ruler></div>
</div>

$# <script src=/static/utils.js></script>
$# <script src=/static/moment-2.21.0.js></script>
$# <script src=/static/web.js></script>

<style>
$# .article {
$#     display: grid;
$#     grid-column-gap: 1em;
$#     grid-template-columns: 12em 40em 8em;
$#     grid-template-rows: 2em auto; }

$# header.toolbar {
$#     display: grid;
$#     grid-column-gap: 1em;
$#     grid-template-columns: 10em 16em 10em;
$#     grid-template-rows: auto 2em; }

#gallery svg {
    cursor: pointer;
    height: 28px;
    width: 32px; }
#gallery .icon {
    margin-right: .5em; }

button.add svg {
    height: 14px;
    width: 16px; }

#preview-large {
    height: 128px;
    width: 144px; }
#preview-medium {
    height: 32px;
    width: 36px; }
#preview-small {
    height: 16px;
    width: 18px; }

$# #toolbox {
$#     grid-column: 3;
$#     grid-row: 2; }

#source {
    font-size: .8em;
    height: 20em;
    width: 20em; }

#editor {
    $# grid-column: 2;
    $# grid-row: 2;
    display: grid;
    grid-column-gap: .5em;
    grid-row-gap: .25em;
    grid-template-columns: 2em 41.25em 2em;
    grid-template-rows: 36.5em 2em; }

.showcase #canvas {
    background: none;
    border-color: #000; }
.showcase .ruler, .showcase circle, .showcase line {
    display: none; }

#canvas {
    background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABmJLR0QAAAAAAAD5Q7t/AAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4gMQEiU3zw0/kAAAAMdJREFUeNrt2tEJxCAMBmDvNnCFrOAKXcH1XcERvA0UTkqhfnktJeV7SH5CU5pURIzZ81LK2Hn/uq7xZP9a6/imwwsAAAAAjq7PalXMKueceu9/N4+I1FpLT/UvpSw/UA4wAwAAAADgzTlgtUrurN09vlsRIQeYAQAAAADgHuAeIAeYAQAAAABwZA5YraI3V85ZDjADAAAAAMA9wD1ADjADAAAAAODIHOD/ADnADAAAAAAA9wD3ADnADAAAAACA83JArXWcDPADG+SewdhVz+sAAAAASUVORK5CYII=);
    border: 1px solid #555;
    grid-column: 2;
    grid-row: 1;
    height: 511px;
    width: 575px; }

.selected {
    color: #3f9; }

.ruler, #coords {
    font-family: Ubuntu Mono; }
.icon {
    height: 16px;
    width: 18px; }

/* rulers */

.ruler span {
    color: #666;
    font-size: .7em; }
.ruler strong {
    color: #ccc; }
.vruler {
    margin-top: -.3em;
    width: 2em; }
.vruler span {
    display: block;
    height: 32px; }
#vrulerl {
    grid-column: 1;
    grid-row: 1;
    text-align: right; }
#vrulerr {
    grid-column: 3;
    grid-row: 1; }
#hruler {
    grid-column: 2;
    grid-row: 2;
    margin-left: -1.5em;
    width: 110%; }
#hruler span {
    display: inline-block;
    transform: rotate(90deg);
    height: 32px;
    width: 32px; }
</style>

<script>
var paths = [];
var id = 0;

function render() {
    var canvas = $$("#canvas");
    while (canvas.firstChild) {
        canvas.removeChild(canvas.firstChild) }
    var svglines = $$("#source").value;
    var output = '<svg viewBox="0 0 576 512"><path fill="#999" fill-opacity="0.75" d="' + svglines + '" />';
    var simplified_output = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path fill="#666" d="' + svglines + '" />';

    function parseCoord(coord) {
        coords = coord.split(",");
        return [parseInt(coords[0]), parseInt(coords[1])]
    }

    function renderShape(shape) {
        paths = [];
        id = 0;
        var lines = svglines.split("\n");
        var path = [];
        var prev;
        for (var i = 0; i < lines.length; i++) {
            var line = lines[i];
            if (line[0] == "Z") {
                path.push(["Z"]);
                paths.push(path);
                path = [];
                continue
            }
            var parts = line.split(" ");
            switch (line[0]) {
                case "C":
                    var from = parseCoord(parts[1]);
                    var fromx = from[0];
                    var fromy = from[1];
                    var to = parseCoord(parts[2]);
                    var tox = to[0];
                    var toy = to[1];
                    var node = parseCoord(parts[3]);
                    var nodex = node[0];
                    var nodey = node[1];
                    path.push(["C", id, id + 1, id + 2]);
                    if (shape == "line") {
                        output = output + "<line x1=" + fromx + " y1=" + fromy + " x2=" + prev[0] + " y2=" + prev[1] + " stroke=orange />";
                        output = output + "<line x1=" + tox + " y1=" + toy + " x2=" + nodex + " y2=" + nodey + " stroke=orange />";
                    } else {
                        output = output + '<circle id="n' + id++ + '" cx="' + fromx + '" cy="' + fromy + '" r="2" fill="red" />';
                        output = output + '<circle id="n' + id++ + '" cx="' + tox + '" cy="' + toy + '" r="2" fill="red" />';
                        output = output + '<circle id="n' + id++ + '" cx="' + nodex + '" cy="' + nodey + '" r="4" fill="blue" />';
                    }
                    break;
                case "L":
                    path.push(["L", id]);
                    var node = parseCoord(parts[1]);
                    var nodex = node[0];
                    var nodey = node[1];
                    if (shape == "circle")
                        output = output + '<circle id="n' + id++ + '" cx="' + nodex + '" cy="' + nodey + '" r="4" fill="blue" />';
                    break;
                case "M":
                    path.push(["M", id]);
                    var node = parseCoord(parts[1]);
                    var nodex = node[0];
                    var nodey = node[1];
                    if (shape == "circle")
                        output = output + '<circle id="n' + id++ + '" cx="' + nodex + '" cy="' + nodey + '" r="4" fill="blue" />';
                    break;
                default:
                    continue
            }
            prev = [nodex, nodey, id];
        }
    }
    // paint circles second to appear above the lines
    renderShape("line");
    renderShape("circle");


    output = output + '</svg>';
    simplified_output = simplified_output + '</svg>';

    $$("#preview-large").innerHTML = output;
    $$$$("#preview-large circle").each(function() {
        this.remove();
    });
    $$$$("#preview-large line").each(function() {
        this.remove();
    });
    $$("#preview-medium").innerHTML = $$("#preview-large").innerHTML;
    $$("#preview-small").innerHTML = $$("#preview-large").innerHTML;

    function escapeRegExp(str) {
        return str.replace(/([.*+?^=!:$${}()|\[\]\/\\])/g, "\\$$1");
    }
    function replaceAll(str, find, replace) {
        return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
    }
    var encoded = replaceAll(simplified_output, "#", "%23");
    $$("#save").setAttribute("href", "data:image/svg+xml," + encoded);

    canvas.innerHTML = output;
    $$$$("circle").each(function() {
        this.addEventListener("click", function(ev) {
            this.setAttribute("stroke", "yellow");
            this.setAttribute("stroke-width", "1");
            this.classList.add("selected");
        })
    });
}

function highlightCoord(textarea) {
    for (var start = textarea.selectionStart; textarea.value[start] != " "; start--) { }
    for (var end = start + 2; textarea.value[end].match(/[\d,]/); end++) { }
    textarea.selectionStart = start + 1;
    textarea.selectionEnd = end;
    textarea.focus();
}

$$.load(function() {
    document.onkeyup = function(ev) {
        if (ev.key == "q")
            $$("#editor").classList.toggle("showcase");
    }

    $$("#source").addEventListener("click", function() {
        if ($$(this).value.length == 0)
            return
        highlightCoord(this);
    });

    /* capture selected, determine offset, shift, scratch,
       render & restore selected */
    $$("#editor").addEventListener("wheel", function(ev) {
        var selected = [];
        $$$$("circle.selected").each(function() {
            selected.push($$(this).getAttribute("id"));
        });
        var offset = -4;
        if (ev.deltaY > 0)
            offset = 4;
        var x = 0;
        var y = 0;
        if (ev.ctrlKey)
            x = offset;
        else
            y = offset;
        shiftNodes(selected, x, y);
        scratch();
        render();
        for (let node of selected) {
            var el = $$("circle#" + node);
            el.setAttribute("stroke", "yellow");
            el.setAttribute("stroke-width", "1");
            el.classList.add("selected");
        }
        ev.preventDefault();
    });

    function scratch() {
        var output = "";
        for (var i = 0; i < paths.length; i++) {
            var path = paths[i];
            for (var j = 0; j < path.length; j++) {
                var command = path[j];
                output = output + command[0] + " ";
                for (var k = 1; k < command.length; k++) {
                    var coord = $$("#n" + command[k]);
                    output = output + coord.getAttribute("cx") + ","
                             + coord.getAttribute("cy") + " ";
                }
                output = output.trim() + "\n";
            }
            output = output + "\n";
        }
        $$("#source").value = output.trim();
    }

    function shiftNodes(nodes, xoffset, yoffset) {
        for (var i = 0; i < paths.length; i++) {
            var path = paths[i];
            for (var j = 0; j < path.length; j++) {
                var command = path[j];
                for (var k = 1; k < command.length; k++) {
                    var id = command[k];
                    for (let node of nodes) {
                        if ("n" + id == node) {
                            var n = $$("#" + node);
                            n.setAttribute("cx", parseInt(n.getAttribute("cx")) + xoffset);
                            n.setAttribute("cy", parseInt(n.getAttribute("cy")) + yoffset);
                        }
                    }
                }
            }
        }
    }

    function addNode(shape, x, y) {
        /* require two nodes, insert between */
        x = parseInt(x);
        y = parseInt(y);
        for (var i = 0; i < paths.length; i++) {
            var path = paths[i];
            for (var j = 0; j < path.length; j++) {
                var command = path[j];
                for (var k = 1; k < command.length; k++) {
                    var id = command[k];
                    var n = $$("#n" + id);
                    if (n.getAttribute("cx") == x && n.getAttribute("cy") == y) {
                        var canvas = $$("#canvas svg");
                        if (shape == "curve") {
                            canvas.append("<circle id=n9997 cx=" + (x + 12) + " cy=" + (y + 12) + ">");
                            canvas.append("<circle id=n9998 cx=" + (x + 16) + " cy=" + (y + 16) + ">");
                            canvas.append("<circle id=n9999 cx=" + (x - 8) + " cy=" + (y + 8) + ">");
                            path.splice(j + 1, 0, ["C", 9997, 9998, 9999]);
                        } else {
                            canvas.append("<circle id=n9999 cx=" + (x - 12) + " cy=" + (y - 12) + ">");
                            path.splice(j + 1, 0, ["L", 9999]);
                        }
                        return
                    }
                }
            }
        }
    }

    var dragStart = 0;
    var canvasOffsets = $$("#canvas").getBoundingClientRect();
    var canvasOffset = {top: canvasOffsets.top + window.scrollY,
                        left: canvasOffsets.left + window.scrollX};
    function getCoords(ev) {
        var x = ev.clientX - canvasOffset.left;
        var y = ev.clientY - canvasOffset.top;
        return [x, y]
    }
    function getRoundedCoords(ev, s) {
        var x = Math.round((ev.clientX - canvasOffset.left) / s) * s;
        var y = Math.round((ev.clientY - canvasOffset.top) / s) * s;
        return [x, y]
    }
    $$("#canvas").addEventListener("mousedown", function(ev) {
        if (!ev.ctrlKey) {
            $$$$("circle.selected").each(function() {
                this.setAttribute("stroke-width", "0");
                this.classList.remove("selected");
            });
        }
        dragStart = getCoords(ev);
        return false
    })
    $$("#canvas").addEventListener("mousemove", function(ev) {
        coords = getRoundedCoords(ev, 16);
        $$("#coords").innerHTML = coords[0] + ":" + coords[1];
    })
    $$("#canvas").addEventListener("mouseup", function(ev) {
        selectCoordsByRange(dragStart, getCoords(ev));
    })

    function selectCoordsByRange(a, b) {
        var minx = a[0];
        if (b[0] < a[0])
            minx = b[0];
        var maxx = a[0];
        if (b[0] > a[0])
            maxx = b[0];
        var miny = a[1];
        if (b[1] < a[1])
            miny = b[1];
        var maxy = a[1];
        if (b[1] > a[1])
            maxy = b[1];
        $$$$("circle").each(function() {
            var x = parseInt(this.getAttribute("cx"));
            var y = parseInt(this.getAttribute("cy"));
            if (minx < x && x < maxx && miny < y && y < maxy) {
                this.setAttribute("stroke", "yellow");
                this.setAttribute("stroke-width", "1");
                this.classList.add("selected");
            }
        });
    }

    $$("#source").addEventListener("keyup", render);
    render();

    $$(".add").addEventListener("click", function() {
        var selected = $$("circle.selected");
        switch (selected.length) {
            case 0:
                alert("please select at least one node");
                break;
            case 1:
                var shape = $$(this).getAttribute("data-shape");
                addNode(shape, selected.getAttribute("cx"), selected.getAttribute("cy"));
                scratch();
                render();
                break;
            default:
                alert("can't add a new node with multiple nodes selected");
        }
    });
    $$("#showcase").addEventListener("click", function() {
        $$("#editor").classList.toggle("showcase");
    });

    var ruler_html = "";
    for (var i = 0; i < 17; i++) {
        var num = i * 32;
        if (i == 8)
            num = "<strong>" + num + "</strong>";
        ruler_html = ruler_html + "<span>" + num + "</span>";
    }
    $$("#vrulerl").innerHTML = ruler_html;
    $$("#vrulerr").innerHTML = ruler_html;

    var ruler_html = "";
    for (var i = 0; i < 19; i++) {
        var num = i * 32;
        if (i == 9)
            num = "<strong>" + num + "</strong>";
        ruler_html = ruler_html + "<span>" + num + "</span>";
    }
    $$("#hruler").innerHTML = ruler_html;

    $$$$("#gallery svg").each(function() {
        this.addEventListener("click", function(ev) {
            $$("#source").value = this.querySelector("path").getAttribute("d");
            render();
        });
    });
});
</script>