# Path adornments — scripting guide **For:** artists writing their own path decorations, and the AI agents helping them. Hand this file to your agent **together with** [Writing a Script Tone](https://mangaloft.org/learn/script-tones.html): a decoration is written the same way as a Script Tone, and that guide covers the header (§3), the pen `g` (§5), randomness (§6), shape helpers (§7) and errors (§9). This guide covers only what is different. The sample in §5 was run, and its output was visually checked. --- ## 1. What a decoration is A **path** can wear one decoration: sparkles, a glow, burst lines, dots — anything drawn **along its line**, on both sides, **not cut by the path**. The path stays the object: you select, edit, move and delete the path, and the decoration follows. It is never selected on its own. **In the app** 1. Select a path or a compound (Selection tool, or the Path tools). A compound's decoration follows its final outline, including the inner edge of a ring. Decorations on its member paths remain stored, but are not drawn while those paths are inside the compound. An oval or rectangle made with a Shape tool needs **Convert to Path** before you can decorate it. 2. In the inspector's **Adornment** section, the popup shows what the path wears: **None**, or a preset — *Sparkles*, *Glow*, *Burst*, *Stipple glow*, *Uneasy lines*, *Soap bubbles*, *Hearts*, *Lightning*, *Flower garland*, *Lace*, *Scallops* or *Hair shine*. Choose a preset to add or replace the decoration; **None** removes it. Each change is one undo step. **Custom…** opens the script editor with the path's current custom script, or with a starter that draws dots along the line. Edit it or paste in a script; **Done** applies it. The popup then shows the script's `// @name`. 3. Once set: **Seed / Roll**, **Invert** (flips black and white in everything the decoration draws — white sparkles over a dark tone; it survives choosing another preset), the script's **sliders**, **Edit Script…**, and **Generate** (draws it again and re-measures its reach, §4). Sliders apply **when you let go** — there is no Live mode for decorations, so per-tick speed doesn't matter; the time and size limits are Script Tone guide §10. 4. While you drag the path's anchors the decoration hides; it redraws once you let go. Moving the whole path doesn't run the script again. --- ## 2. The shape of a script Exactly as for a Script Tone (Script Tone guide §2–3, including the `@jscg` version rules): a `// @jscg 1` header, `// @param` sliders, and one `function generate(p, g)`. - Slider names you **can't** use: `width`, `height`, `seed`, `outline` (the header refuses them), and also `lines`, `path`, `anchors`, `closed` (a decoration refuses them — they are its inputs). - At most 12 sliders. --- ## 3. The inputs — `p` | Input | What it is | |---|---| | `p.width`, `p.height` | the bounding box of the **whole path**, in points (a straight horizontal path has height 0) | | `p.seed` | 0…4294967295 — the Roll. All randomness comes from it (Script Tone guide §6) | | `p.lines` | one entry per line of the path (a path can hold several — e.g. after a Join or a boolean), each `{ points, anchors, closed }` | | `p.lines[i].points` | the line, flattened: `[x0, y0, x1, y1, …]`. For a closed line, the last point does NOT repeat the first. A line can be a **single point** (a one-anchor subpath) with no segments | | `p.lines[i].anchors` | the path's anchors: `[x, y, kind, x, y, kind, …]` — kind **0** smooth, **1** sharp corner, **2** open end | | `p.lines[i].closed` | `true` for a closed line | | `p.path`, `p.anchors`, `p.closed` | shorthand for the **first** line | | `p.` | each slider from the header | **Coordinates:** points, origin at the **top-left of the whole path's box**, `y` growing **down**. All entries in `p.lines` use this same origin. Unlike a Script Tone, **draw outside the box freely** — a decoration straddles its line, so half of it is usually outside. Nothing is cut by the path. There is **no** `p.outline` and **no** `p.reach`: how far the ink goes is measured from what you draw (§4). --- ## 4. Reach — how far the ink goes Each time you add or change the decoration — Add, a slider, Roll, Edit Script, Generate — the app prepares the drawing (running your script, or reusing a drawing it already made for exactly these settings; Generate always runs it again), and **measures how far the ink reaches past the whole path's bounding box**. That distance is saved with the path. It decides what area the app redraws and exports, and **visible ink is limited to it**: the decoration is cut off at the path's box grown by the saved reach, so the screen and the export always agree. - **The limit is 512 points** past the box. Ink the measurement sees reaching that far is refused ("reaches too far"), and nothing changes. - **Ink drawn wholly far away** (more than about 514 points past the box, not touching the measured area) isn't seen, isn't counted and never shows. The *Glow* preset relies on this: it draws its line 5,000 points away and lets only the shadow land on the page. - **Keep random extremes bounded.** The largest of many random sizes grows with how many there are — a long path gets more chances at a big one. Give the farthest-reaching part a fixed limit (Lightning ends every fork at the same distance) so the reach doesn't creep up as the path grows. - **Sizes in points are less likely to outgrow the reach** than sizes that follow the path (e.g. `var r = p.width / 10`). Even then, reshaping or **rotating** a path changes its box, which can change the reach. If the decoration looks cut off after reshaping or rotating, press **Generate** — it measures again. - Blur counts: a shadow's softness is part of the measured ink. - If the drawing can't be made or measured at that moment (the script failed, or scripts are paused), a **new** decoration gets the largest reach and an existing one keeps its saved reach. Generate measures again once it works. --- **Use decorations for edge effects.** A script can draw inside the path too, but the path's own **Fill** is the better choice for filling its interior. Opaque shapes along the edge — petals, bubbles or lace — can cover the ink beneath them (and **Invert** turns their whites black). The Scallops preset draws only an outline. ## 5. Walking along the line Most decorations place things at regular steps along the line, facing outward. This sample does exactly that — copy `outward` and `walk` into your own scripts: ```javascript // @jscg 1 // @name Dots along the path // @param spacing 4..60 default 14 "Spacing" // @param size 1..12 default 4 "Dot size" // @param offset -30..30 default 0 "Offset from the line" function circle(g, x, y, r) { var k = 0.5523 * r; g.moveTo(x + r, y); g.curveTo(x + r, y + k, x + k, y + r, x, y + r); g.curveTo(x - k, y + r, x - r, y + k, x - r, y); g.curveTo(x - r, y - k, x - k, y - r, x, y - r); g.curveTo(x + k, y - r, x + r, y - k, x + r, y); g.closePath(); } // Which way is "out" for a closed line: +1 or -1 (screen y runs down). Open lines: +1. function outward(line) { if (!line.closed) { return 1; } var pts = line.points, n = pts.length / 2, area = 0; for (var i = 0; i < n; i++) { var j = (i + 1) % n; area += pts[2 * i] * pts[2 * j + 1] - pts[2 * j] * pts[2 * i + 1]; } return area > 0 ? 1 : -1; } // Calls visit(x, y, nx, ny) every `spacing` points along the line; (nx, ny) points outward. function walk(line, spacing, visit) { var pts = line.points, n = pts.length / 2; if (n < 2 || !(spacing > 0)) { return; } var side = outward(line), next = 0; var segments = line.closed ? n : n - 1; // A closed line: stretch the spacing a little so the last item doesn't crowd the first. if (line.closed) { var total = 0; for (var k = 0; k < n; k++) { var k2 = (k + 1) % n; total += Math.hypot(pts[2 * k2] - pts[2 * k], pts[2 * k2 + 1] - pts[2 * k + 1]); } spacing = total / Math.max(1, Math.round(total / spacing)); } for (var i = 0; i < segments; i++) { var j = (i + 1) % n; var x0 = pts[2 * i], y0 = pts[2 * i + 1]; var dx = pts[2 * j] - x0, dy = pts[2 * j + 1] - y0; var len = Math.sqrt(dx * dx + dy * dy); if (len === 0) { continue; } while (next < len - (line.closed && i === segments - 1 ? spacing / 2 : 0)) { var t = next / len; visit(x0 + dx * t, y0 + dy * t, side * dy / len, -side * dx / len); next += spacing; } next -= len; } } function generate(p, g) { g.setFill(0, 0, 0, 1); for (var l = 0; l < p.lines.length; l++) { walk(p.lines[l], p.spacing, function (x, y, nx, ny) { circle(g, x + nx * p.offset, y + ny * p.offset, p.size); }); } g.fill(); } ``` - **`walk(line, spacing, visit)`** calls `visit(x, y, nx, ny)` every `spacing` points: `(x, y)` is on the line, `(nx, ny)` is a unit vector pointing **outward** — so `x + nx * d, y + ny * d` is `d` points outside the line (a negative `d` is inside). - **Outward on a closed line** is away from its inside, whichever way the path was drawn — `outward` reads the line's turning direction. On an **open** line it is a fixed side of the drawing direction: heading right (left to right), outward is **up**; heading down, outward is to the right. - On a closed line the spacing is **adjusted** (slightly stretched or shortened) so the items come out even and the last doesn't crowd the first. - "Outward" assumes a simple closed line. A line that crosses itself has no single outside, and each line of a multi-line path (a hole, say) is handled on its own. - **Corners:** `line.anchors` lists each anchor with its kind; mark sharp corners (kind 1), or keep items away from them — the *Sparkles* preset does both. The built-in presets are complete examples; the first four are the clearest: Sparkles (corner handling), Glow (blur via a far-away shadow), Burst (tapered lines across the line), Stipple glow (dots thrown over the area and kept by distance to the line, the inside of a closed line left clean — the way to get even density around bumps and dents). --- ## 6. Sharing and storage - A script is identified by its **exact text**. Editing it — even a space — makes a new script; paths already using the old one keep it. - Every page keeps its **own copy** of each script it uses, so copying a path to another page or document, checking pages in and out, and exporting `.mangapage` all carry the script along. Share a script by copying its text (Edit Script… → copy), e.g. on Discord. - If a page has lost a script, the path draws as a plain line and the popup says **Script missing** — choose a preset to replace it, or **None** to remove it. Exporting such a page is refused with the reason. --- ## 7. When something is wrong Same as a Script Tone (Script Tone guide §9): the inspector shows ⚠️ and the reason, the path draws as a plain line, and **Generate** runs the script again. A script that ran too long is blocked until you press Generate. Keep the Script Tone guide's rules: all randomness from `p.seed` (§6 — `Math.random` is seeded and `Date` frozen, so neither tells the time), loops with a fixed count, and the limits in §10.