Writing a Script Tone

A tone whose ink is drawn by a short JavaScript program.

For your AI agentThis whole guide as one plain-text file: script-tones.txt. Hand it over as it is.

For: artists writing their own Script Tone effects, and the AI agents helping them. Hand this whole file to your agent. Scripts use the versioned // @jscg 1 contract.

Every sample in this guide was run, and its output was visually checked.

Decorations along a path (sparkles, glows, bursts that follow a line) are written the same way — see the companion Writing a line decoration for what differs.

1. What a Script Tone is

A tone whose ink is drawn by a short JavaScript program. You draw the region; the script fills it — snow, sparkles, petals, rain, anything made of shapes. The app cuts the drawing at the region's edge, like any tone.

In the app

  1. Long-press the Tone cell in the tool strip → Script Tone ({ }). Draw a region — Rect, Lasso or Bezier. A new tone uses your last Script Tone settings with a fresh seed. The first one uses Sparkles.
  2. The inspector:
    • Presets — swap in a ready-made script. Custom… at the bottom opens the editor straight away, on the tone's own script or a starter that scatters dots; the menu then shows the script's // @name. The tool's panel has the same entry, so a script can be written or pasted before drawing.
    • Seed / Roll — the number all the randomness comes from. Roll draws the same effect a new way.
    • Invert — flips black and white in everything the script drew (grays mirrored, softness kept). Rain drawn in black becomes white rain; Petals' white body and ink outline swap. It survives picking another preset.
    • Live — on: sliders redraw while you drag. Off: move sliders, then press Generate.
    • Sliders — whatever the script declares in its header (§3).
    • Edit Script… — the code. Done checks it and applies it.
  3. Move, scale, undo, save and reopen like any tone. The script, seed and slider values are saved with it.

When something is wrong (§9) the inspector shows ⚠️ and the reason, and draws nothing for that tone.

2. The shape of a script

// @jscg 1
// @name My effect
// @param count int 10..500 default 100 "Count"

function generate(p, g) {
	// read numbers from p, draw with g
}

3. The header — sliders

// @jscg 1
// @name Snow
// @param count int 20..800 default 260 "Flakes"
// @param size 0.5..3 default 1 "Flake size"

4. The inputs — p

InputWhat it is
p.width, p.heightthe region's bounding box, in points
p.seeda whole number 0…4294967295 — the Roll. All randomness must come from it (§6).
p.outlinethe region's edge: a flat list [x0, y0, x1, y1, …], closed (the last point joins the first)
p.<name>each slider from the header

Coordinates are points, origin at the top-left of the region's box, y growing down. Cover the box — the app cuts away everything outside the region, so a script never has to clip.

5. The pen — g

Build a path, then paint it. Painting consumes the current path: after fill() or stroke(), start the next shape with moveTo.

CallDoes
g.moveTo(x, y)start a new shape
g.lineTo(x, y)straight line
g.curveTo(ax, ay, bx, by, x, y)cubic Bézier: controls a and b, ending at x, y
g.closePath()line back to the shape's start
g.fill()fill every shape built since the last paint
g.stroke()outline every shape built since the last paint
g.setFill(r, g, b, a)fill colour, each 0…1
g.setStroke(r, g, b, a)stroke colour, each 0…1
g.setLineWidth(w)stroke width, in points
g.save() / g.restore()remember / return to colours, width and transform (up to 64 deep)
g.translate(x, y) / g.rotate(radians) / g.scale(sx, sy)move, turn or stretch everything drawn after
g.setShadow(dx, dy, blur, r, g, b, a)a shadow under everything painted after it — offset, blur and colour (§7.1)
g.clearShadow()no shadow from here on
g.addGradientStop(t, r, g, b, a)one colour stop (t from 0 to 1) for the next gradient fill (§7.2)
g.fillLinearGradient(x0, y0, x1, y1)fill the current path with a gradient running from (x0, y0) to (x1, y1)
g.fillRadialGradient(cx, cy, r0, r1)fill the current path with a gradient from radius r0 out to r1 around (cx, cy)

6. Randomness — the seed

Use the seed to keep a tone's layout stable when you reopen, export, or move it to another device. MangaLoft seeds Math.random, but an explicit generator makes the sequence clear and portable. Use this one for every random number:

function mulberry32(seed) {
	var a = seed >>> 0;
	return function () {
		a = (a + 0x6D2B79F5) >>> 0;
		var t = a;
		t = Math.imul(t ^ (t >>> 15), t | 1);
		t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}
// in generate:  var rand = mulberry32(p.seed);   rand() gives 0 ≤ value < 1

MangaLoft freezes Date, but do not use it as a source of variation. Avoid changing a variable outside generate: the script may run many times in a row.

Useful shapes of randomness:

You wantWrite
a value between a and ba + rand() * (b - a)
mostly small, a few bigMath.pow(rand(), 3) — raise the power for more contrast
yes 30% of the timerand() < 0.3
one of a listlist[Math.floor(rand() * list.length)]
a random anglerand() * Math.PI * 2

The same layout on every device. Plain arithmetic (+ - * /), Math.sqrt and mulberry32 give the same random positions on a Mac and an iPad. Rasterized pixels can differ slightly: a Flames page exported from both at 350 dpi differed in 4 of 17 million pixels, by one level of edge smoothing (checked 2026-10-04). Math.sin, Math.pow, Math.exp and the like could, in principle, differ in the last digit after an operating-system update. That alone is invisible — unless a decision depends on it and changes how many random numbers are drawn after it: then every later shape lands somewhere else. To keep such a slip local, draw each shape's random numbers first, always the same count, and decide afterwards:

for (var i = 0; i < count; i++) {
	var x = rand() * p.width, y = rand() * p.height, size = rand(), keep = rand();   // always four
	if (keep > strength(x, y)) continue;   // a decision that uses Math.sin can no longer shift the next shape
	// … draw it
}

7. Shape helpers

Copy what you need into your script.

// A circle (four Bézier curves).
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();
}

// A rectangle.
function rect(g, x, y, w, h) {
	g.moveTo(x, y); g.lineTo(x + w, y); g.lineTo(x + w, y + h); g.lineTo(x, y + h); g.closePath();
}

// A four-point star centred on (x, y); pinch 0.1 is sharp, 0.3 is soft.
function star(g, x, y, r, pinch) {
	var k = r * pinch;
	g.moveTo(x, y - r);
	g.curveTo(x + k, y - k, x + k, y - k, x + r, y);
	g.curveTo(x + k, y + k, x + k, y + k, x, y + r);
	g.curveTo(x - k, y + k, x - k, y + k, x - r, y);
	g.curveTo(x - k, y - k, x - k, y - k, x, y - r);
	g.closePath();
}

Draw shapes around (0, 0), then place them with save → translate → rotate → draw → restore. The shape code stays simple, and turning a shape is one line.

7.1 Shadows, glows and blur

g.setShadow(dx, dy, blur, r, g, b, a) puts a shadow under everything filled or stroked after it, until g.clearShadow(). Offsets and blur are in page points — the same on screen and in print — and point the same way on the page whatever you rotate afterwards. dy is down, like every coordinate.

EffectCall
soft white glowg.setShadow(0, 0, 8, 1, 1, 1, 1) — no offset
ink drop shadowg.setShadow(3, 4, 3, 0, 0, 0, 0.6)
white drop shadow (over a tone)g.setShadow(3, 4, 2, 1, 1, 1, 1)
hard offset copyg.setShadow(4, 4, 0, 0, 0, 0, 1) — zero blur

Blur goes up to 200 points and offsets up to 10,000. A white shadow, like any white, only shows over a tone.

A blurred shape, not just a blurred edge — the far-away trick. Draw the shape far off to the side and offset its shadow back to where the shape belongs. The sharp shape lands outside the region and is cut away; only its blurred shadow is left:

var FAR = 5000;     // farther than the region is wide — but offsets stop at 10000
g.setShadow(-FAR, 0, 6, 1, 1, 1, 1);   // shadow comes back FAR to the left, blurred by 6
circle(g, x + FAR, y, r);              // the shape itself, far to the right
g.fill();

7.2 Gradients

Build a shape as for fill(), add colour stops, then fill it with a gradient instead of a flat colour:

g.addGradientStop(0, 1, 1, 1, 1);      // at the start: white, solid
g.addGradientStop(1, 1, 1, 1, 0);      // at the end: white, clear
rect(g, 0, 0, p.width, p.height);
g.fillLinearGradient(0, 0, 0, p.height);   // top to bottom

8. Samples

Each sample is a complete script. Most are also in the inspector's Presets menu; the headings identify the exceptions. Pick a preset, or paste a sample into Edit Script… and press Done. Every sample was checked in a rounded region.

8.1 Stipple (in Presets) — your first script

The smallest useful script: random dots, one fill() for all of them. The count follows the region's area.

// @jscg 1
// @name Stipple
// @param density 0.002..0.05 default 0.012 "Density"
// @param size 0.3..4 default 1 "Dot size"
// Random dots. The count follows the region's area, so a big region isn't sparse.

function mulberry32(seed) {
	var a = seed >>> 0;
	return function () {
		a = (a + 0x6D2B79F5) >>> 0;
		var t = a;
		t = Math.imul(t ^ (t >>> 15), t | 1);
		t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}

function dot(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();
}

function generate(p, g) {
	var rand = mulberry32(p.seed);
	var count = Math.min(20000, Math.round(p.width * p.height * p.density));
	g.setFill(0, 0, 0, 1);
	for (var i = 0; i < count; i++) {
		dot(g, rand() * p.width, rand() * p.height, p.size * (0.5 + rand()));
	}
	g.fill();   // one fill for every dot: much faster than a fill per dot
}

8.2 Snow (not in Presets — a technique: white rims over ink)

Mixed flakes with a white rim so they read over dark tone, plus a few six-armed crystals.

// @jscg 1
// @name Snow
// @param count int 20..800 default 260 "Flakes"
// @param size 0.5..3 default 1 "Flake size"
// @param crystals int 0..60 default 12 "Crystals"
// Soft snow: round flakes of mixed size, a few six-armed crystals.

// Seeded random numbers (mulberry32). Never use Math.random: the same seed
// must always draw the same snow.
function mulberry32(seed) {
	var a = seed >>> 0;
	return function () {
		a = (a + 0x6D2B79F5) >>> 0;
		var t = a;
		t = Math.imul(t ^ (t >>> 15), t | 1);
		t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}

// A circle from four Bézier curves.
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();
}

function crystal(g, x, y, r, angle) {
	g.save();
	g.translate(x, y);
	g.rotate(angle);
	for (var i = 0; i < 6; i++) {
		g.moveTo(0, 0); g.lineTo(r, 0);
		g.moveTo(r * 0.55, 0); g.lineTo(r * 0.8, r * 0.2);
		g.moveTo(r * 0.55, 0); g.lineTo(r * 0.8, -r * 0.2);
		g.rotate(Math.PI / 3);
	}
	g.stroke();
	g.restore();
}

function generate(p, g) {
	var rand = mulberry32(p.seed);
	var count = p.count;

	// Black flakes with a white rim, so snow reads over both paper and dark tone.
	for (var i = 0; i < count; i++) {
		var x = rand() * p.width;
		var y = rand() * p.height;
		var r = (0.8 + Math.pow(rand(), 3) * 5) * p.size;   // mostly small, a few big
		g.setFill(1, 1, 1, 1);
		circle(g, x, y, r + 0.8); g.fill();
		g.setFill(0, 0, 0, 0.25 + 0.6 * rand());
		circle(g, x, y, r); g.fill();
	}

	g.setStroke(0, 0, 0, 0.8);
	g.setLineWidth(0.8);
	for (var j = 0; j < p.crystals; j++) {
		crystal(g, rand() * p.width, rand() * p.height, (6 + rand() * 8) * p.size, rand() * Math.PI);
	}
}

8.3 Sparkles (in Presets)

Four-point stars, mostly small; big ones get a small cross-star; tiny glints between.

// @jscg 1
// @name Sparkles
// @param count int 5..300 default 60 "Sparkles"
// @param size 2..30 default 9 "Size"
// @param glints int 0..400 default 120 "Glints"
// Four-point stars of mixed size with tiny glints between them.

function mulberry32(seed) {
	var a = seed >>> 0;
	return function () {
		a = (a + 0x6D2B79F5) >>> 0;
		var t = a;
		t = Math.imul(t ^ (t >>> 15), t | 1);
		t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}

// A four-point star centred on (x, y): long points, pinched sides.
function star(g, x, y, r, pinch) {
	var k = r * pinch;
	g.moveTo(x, y - r);
	g.curveTo(x + k, y - k, x + k, y - k, x + r, y);
	g.curveTo(x + k, y + k, x + k, y + k, x, y + r);
	g.curveTo(x - k, y + k, x - k, y + k, x - r, y);
	g.curveTo(x - k, y - k, x - k, y - k, x, y - r);
	g.closePath();
}

function generate(p, g) {
	var rand = mulberry32(p.seed);
	g.setFill(0, 0, 0, 1);
	for (var i = 0; i < p.count; i++) {
		var r = p.size * (0.3 + Math.pow(rand(), 2) * 1.4);   // mostly small, a few big
		var x = rand() * p.width, y = rand() * p.height;
		g.save();
		g.translate(x, y);
		g.rotate((rand() - 0.5) * 0.4);                          // a slight tilt reads hand-drawn
		star(g, 0, 0, r, 0.12);
		g.fill();
		if (r > p.size) {                                        // big ones get a small cross-star
			g.rotate(Math.PI / 4);
			star(g, 0, 0, r * 0.45, 0.15);
			g.fill();
		}
		g.restore();
	}
	for (var j = 0; j < p.glints; j++) {
		star(g, rand() * p.width, rand() * p.height, 0.8 + rand() * 1.6, 0.2);
	}
	g.fill();
}

8.4 Falling petals (in Presets)

A notched cherry petal, turned at random and narrowed as if seen edge-on. White body, ink outline.

// @jscg 1
// @name Falling petals
// @param count int 5..200 default 45 "Petals"
// @param size 4..40 default 16 "Size"
// @param ink 0..1.5 default 0.8 "Outline"
// Cherry petals drifting down: a notched petal, turned and tilted at random.

function mulberry32(seed) {
	var a = seed >>> 0;
	return function () {
		a = (a + 0x6D2B79F5) >>> 0;
		var t = a;
		t = Math.imul(t ^ (t >>> 15), t | 1);
		t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}

// One petal pointing up, base at (0, 0): narrow at the base, wide near the tip, with a notch.
function petal(g, s) {
	g.moveTo(0, 0);
	g.curveTo(-0.2 * s, -0.15 * s, -0.45 * s, -0.6 * s, -0.3 * s, -0.95 * s);
	g.lineTo(0, -0.78 * s);                       // the notch
	g.lineTo(0.3 * s, -0.95 * s);
	g.curveTo(0.45 * s, -0.6 * s, 0.2 * s, -0.15 * s, 0, 0);
	g.closePath();
}

function generate(p, g) {
	var rand = mulberry32(p.seed);
	for (var i = 0; i < p.count; i++) {
		var s = p.size * (0.6 + rand() * 0.8);
		g.save();
		g.translate(rand() * p.width, rand() * p.height);
		g.rotate(rand() * Math.PI * 2);
		g.scale(0.45 + rand() * 0.55, 1);              // narrower = turned away from us
		petal(g, s);
		g.setFill(1, 1, 1, 1);                        // white body…
		g.fill();
		petal(g, s);
		g.setStroke(0, 0, 0, 1);                      // …with an ink outline
		g.setLineWidth(p.ink);
		g.stroke();
		g.restore();
	}
}

8.5 Bubbles (in Presets)

Soap bubbles: clear in the middle and shading in toward the rim — an inward radial gradient (§7.2) — then an outline and a highlight arc. Turn on Invert for white bubbles over a dark tone.

// @jscg 1
// @name Bubbles
// @param count int 3..150 default 30 "Bubbles"
// @param size 3..40 default 14 "Size"
// @param line 0.3..3 default 1 "Line width"
// @param shade 0..1 default 0.35 "Rim shade"
// Soap bubbles: clear in the middle, shading in toward the rim (an inward radial
// gradient), an outline and a small highlight arc.

function mulberry32(seed) {
	var a = seed >>> 0;
	return function () {
		a = (a + 0x6D2B79F5) >>> 0;
		var t = a;
		t = Math.imul(t ^ (t >>> 15), t | 1);
		t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}

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();
}

function generate(p, g) {
	var rand = mulberry32(p.seed);
	g.setStroke(0, 0, 0, 1);
	for (var i = 0; i < p.count; i++) {
		var r = p.size * (0.3 + Math.pow(rand(), 1.5) * 1.4);
		var x = rand() * p.width, y = rand() * p.height;
		// Inward gradient: clear at the centre, darkening toward the rim.
		if (p.shade > 0) {
			g.addGradientStop(0, 0, 0, 0, 0);
			g.addGradientStop(0.65, 0, 0, 0, p.shade * 0.15);
			g.addGradientStop(1, 0, 0, 0, p.shade);
			circle(g, x, y, r);
			g.fillRadialGradient(x, y, 0, r);
		}
		g.setLineWidth(p.line);
		circle(g, x, y, r);
		g.stroke();
		// Highlight: a short arc inside the upper left, drawn as one curve.
		g.setLineWidth(p.line * 1.4);
		var a = r * 0.62;
		g.moveTo(x - a, y - a * 0.2);
		g.curveTo(x - a, y - a * 0.75, x - a * 0.6, y - a, x - a * 0.15, y - a);
		g.stroke();
	}
}

8.6 Rain (in Presets)

Streaks at one angle. They start outside the box, so they cross the edges like real rain.

// @jscg 1
// @name Rain
// @param count int 20..1500 default 400 "Drops"
// @param length 4..80 default 22 "Length"
// @param angle -60..60 default 15 "Angle"
// @param line 0.2..2 default 0.6 "Line width"
// Straight streaks, all at one angle, of mixed length.

function mulberry32(seed) {
	var a = seed >>> 0;
	return function () {
		a = (a + 0x6D2B79F5) >>> 0;
		var t = a;
		t = Math.imul(t ^ (t >>> 15), t | 1);
		t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}

function generate(p, g) {
	var rand = mulberry32(p.seed);
	var a = p.angle * Math.PI / 180;
	var dx = Math.sin(a), dy = Math.cos(a);
	g.setStroke(0, 0, 0, 1);
	g.setLineWidth(p.line);
	// Start a little outside the box so streaks cross the edges too (the app cuts them).
	var pad = p.length;
	for (var i = 0; i < p.count; i++) {
		var x = -pad + rand() * (p.width + 2 * pad);
		var y = -pad + rand() * (p.height + 2 * pad);
		var len = p.length * (0.5 + rand());
		g.moveTo(x, y);
		g.lineTo(x + dx * len, y + dy * len);
	}
	g.stroke();   // all streaks in one stroke
}

8.7 Hearts (in Presets)

Some filled, some outlined, slightly tilted.

// @jscg 1
// @name Hearts
// @param count int 3..120 default 25 "Hearts"
// @param size 4..40 default 14 "Size"
// @param filled 0..1 default 0.5 "Filled share"
// Hearts, some filled and some outlined.

function mulberry32(seed) {
	var a = seed >>> 0;
	return function () {
		a = (a + 0x6D2B79F5) >>> 0;
		var t = a;
		t = Math.imul(t ^ (t >>> 15), t | 1);
		t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}

// A heart centred near (0, 0), about s wide.
function heart(g, s) {
	g.moveTo(0, 0.35 * s);
	g.curveTo(-0.55 * s, -0.05 * s, -0.5 * s, -0.55 * s, -0.25 * s, -0.55 * s);
	g.curveTo(-0.1 * s, -0.55 * s, 0, -0.42 * s, 0, -0.3 * s);
	g.curveTo(0, -0.42 * s, 0.1 * s, -0.55 * s, 0.25 * s, -0.55 * s);
	g.curveTo(0.5 * s, -0.55 * s, 0.55 * s, -0.05 * s, 0, 0.35 * s);
	g.closePath();
}

function generate(p, g) {
	var rand = mulberry32(p.seed);
	g.setFill(0, 0, 0, 1);
	g.setStroke(0, 0, 0, 1);
	g.setLineWidth(1);
	for (var i = 0; i < p.count; i++) {
		g.save();
		g.translate(rand() * p.width, rand() * p.height);
		g.rotate((rand() - 0.5) * 0.8);
		heart(g, p.size * (0.5 + rand()));
		if (rand() < p.filled) { g.fill(); } else { g.stroke(); }
		g.restore();
	}
}

8.8 Fading dots (in Presets)

Density falls from the top of the region to the bottom — a gradient made of dots.

// @jscg 1
// @name Fading dots
// @param count int 100..8000 default 2500 "Dots"
// @param size 0.3..4 default 1.2 "Dot size"
// @param power 0.5..4 default 1.8 "Fade"
// Dots that thin out from the top of the region to the bottom.

function mulberry32(seed) {
	var a = seed >>> 0;
	return function () {
		a = (a + 0x6D2B79F5) >>> 0;
		var t = a;
		t = Math.imul(t ^ (t >>> 15), t | 1);
		t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}

function dot(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();
}

function generate(p, g) {
	var rand = mulberry32(p.seed);
	g.setFill(0, 0, 0, 1);
	for (var i = 0; i < p.count; i++) {
		var x = rand() * p.width, y = rand() * p.height;
		var keep = Math.pow(1 - y / p.height, p.power);   // 1 at the top, 0 at the bottom
		if (rand() < keep) { dot(g, x, y, p.size * (0.6 + rand() * 0.8)); }
	}
	g.fill();
}

8.9 Whole flowers (in Presets) — using p.outline

Every flower is kept whole inside the region: a spot is used only if the flower fits. Includes two reusable helpers, inside (is a point in the region?) and edgeDistance (how far to the edge?).

// @jscg 1
// @name Whole flowers
// @param count int 5..300 default 70 "Flowers"
// @param size 4..30 default 10 "Size"
// @param petals int 4..8 default 5 "Petals"
// Flowers kept whole inside the region: none is cut by the edge. Uses p.outline.

function mulberry32(seed) {
	var a = seed >>> 0;
	return function () {
		a = (a + 0x6D2B79F5) >>> 0;
		var t = a;
		t = Math.imul(t ^ (t >>> 15), t | 1);
		t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}

// Is (x, y) inside the polygon pts = [x0, y0, x1, y1, …]? (even-odd ray test)
function inside(pts, x, y) {
	var n = pts.length / 2, c = false;
	for (var i = 0, j = n - 1; i < n; j = i++) {
		var xi = pts[2 * i], yi = pts[2 * i + 1], xj = pts[2 * j], yj = pts[2 * j + 1];
		if ((yi > y) !== (yj > y) && x < (xj - xi) * (y - yi) / (yj - yi) + xi) { c = !c; }
	}
	return c;
}

// Distance from (x, y) to the nearest edge of the polygon.
function edgeDistance(pts, x, y) {
	var n = pts.length / 2, best = Infinity;
	for (var i = 0, j = n - 1; i < n; j = i++) {
		var ax = pts[2 * j], ay = pts[2 * j + 1], bx = pts[2 * i], by = pts[2 * i + 1];
		var vx = bx - ax, vy = by - ay, len2 = vx * vx + vy * vy;
		var t = len2 === 0 ? 0 : Math.max(0, Math.min(1, ((x - ax) * vx + (y - ay) * vy) / len2));
		var dx = x - (ax + t * vx), dy = y - (ay + t * vy);
		best = Math.min(best, Math.sqrt(dx * dx + dy * dy));
	}
	return best;
}

function flower(g, x, y, r, n, turn) {
	g.save();
	g.translate(x, y);
	g.rotate(turn);
	for (var i = 0; i < n; i++) {
		g.moveTo(0, 0);
		g.curveTo(-0.45 * r, -0.4 * r, -0.3 * r, -r, 0, -r);
		g.curveTo(0.3 * r, -r, 0.45 * r, -0.4 * r, 0, 0);
		g.closePath();
		g.rotate(2 * Math.PI / n);
	}
	g.setFill(1, 1, 1, 1); g.fill();
	for (var k = 0; k < n; k++) {
		g.moveTo(0, 0);
		g.curveTo(-0.45 * r, -0.4 * r, -0.3 * r, -r, 0, -r);
		g.curveTo(0.3 * r, -r, 0.45 * r, -0.4 * r, 0, 0);
		g.closePath();
		g.rotate(2 * Math.PI / n);
	}
	g.setStroke(0, 0, 0, 1); g.setLineWidth(0.8); g.stroke();
	g.restore();
}

function generate(p, g) {
	var rand = mulberry32(p.seed);
	var pts = p.outline || [0, 0, p.width, 0, p.width, p.height, 0, p.height];
	var placed = 0;
	// Try more spots than needed; keep only those where the whole flower fits.
	for (var tries = 0; tries < p.count * 20 && placed < p.count; tries++) {
		var r = p.size * (0.6 + rand() * 0.8);
		var x = rand() * p.width, y = rand() * p.height;
		if (!inside(pts, x, y) || edgeDistance(pts, x, y) < r) { continue; }
		flower(g, x, y, r, p.petals, rand() * Math.PI);
		placed++;
	}
}

8.10 Edge sparkles (not in Presets) — following the edge

Sparkles spaced evenly along the region's edge, a little inside it. This script still works when pasted into a Script Tone, but Edge sparkles is no longer in its Presets menu; use a path decoration for a new effect along a path.

// @jscg 1
// @name Edge Sparkles
// @param spacing 6..60 default 18 "Spacing"
// @param size 2..20 default 7 "Size"
// @param inset -10..20 default 4 "Inset"
// Four-point sparkles along the region's edge. Uses p.outline; without a
// drawn region it follows the rectangle's border instead.

function mulberry32(seed) {
	var a = seed >>> 0;
	return function () {
		a = (a + 0x6D2B79F5) >>> 0;
		var t = a;
		t = Math.imul(t ^ (t >>> 15), t | 1);
		t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}

// A four-point star: concave sides from four curves.
function sparkle(g, x, y, r) {
	var k = r * 0.12;
	g.moveTo(x, y - r);
	g.curveTo(x + k, y - k, x + k, y - k, x + r, y);
	g.curveTo(x + k, y + k, x + k, y + k, x, y + r);
	g.curveTo(x - k, y + k, x - k, y + k, x - r, y);
	g.curveTo(x - k, y - k, x - k, y - k, x, y - r);
	g.closePath();
}

function generate(p, g) {
	var rand = mulberry32(p.seed);
	var pts = p.outline;
	if (!pts) {
		pts = [0, 0, p.width, 0, p.width, p.height, 0, p.height];
	}
	var n = pts.length / 2;

	// Which way is "inside"? The sign of the polygon's area tells the winding.
	var 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];
	}
	var side = area > 0 ? 1 : -1;

	g.setFill(0, 0, 0, 1);
	var carry = 0;   // distance left over from the previous segment
	for (var i = 0; i < n; 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; }
		var nx = -dy / len * side, ny = dx / len * side;   // normal pointing inside
		for (var d = carry; d < len; d += p.spacing) {
			var t = d / len;
			var r = p.size * (0.5 + rand());
			sparkle(g, x0 + dx * t + nx * p.inset, y0 + dy * t + ny * p.inset, r);
			g.fill();
		}
		carry = (carry - len) % p.spacing;
		if (carry < 0) { carry += p.spacing; }
	}
}

8.11 Soft snow (in Presets) — a real blur

White snow with out-of-focus edges, the look of falling snow over a gradient. Lay it over a darker tone (a Dot Gradient, say): the white covers what is beneath and the soft rims fade into it. Each soft flake uses the far-away trick (§7.1), so what you see is a true blur of a disc.

Before shadows existed this was done by stacking many faint white circles, each a little smaller — the middle gets every layer and turns solid, the rim fades. That still works for any shape and needs no shadow, but takes about 2.5 layers per point of softness to hide the rings.

// @jscg 1
// @name Soft snow
// @param count int 5..400 default 70 "Flakes"
// @param size 1..40 default 7 "Size"
// @param big 0..0.5 default 0.08 "Big share"
// @param blur 0..1 default 0.35 "Softness"
// White snow with soft, out-of-focus edges. Lay it over a darker tone (a gradient)
// so the white reads. The blur is real: each soft flake is drawn far off to the
// side, and only its blurred white shadow lands where the flake belongs.

function mulberry32(seed) {
	var a = seed >>> 0;
	return function () {
		a = (a + 0x6D2B79F5) >>> 0;
		var t = a;
		t = Math.imul(t ^ (t >>> 15), t | 1);
		t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}

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();
}

// A white disc whose edge is blurred by `soft` points.
var FAR = 5000;   // far outside any region (shadow offsets go up to 10000): the sharp disc is cut away
function softDisc(g, x, y, r, soft) {
	if (soft < 0.5) {                        // too small to blur: just a crisp disc
		g.clearShadow();
		circle(g, x, y, r);
	} else {
		g.setShadow(-FAR, 0, soft, 1, 1, 1, 1);
		circle(g, x + FAR, y, r);
	}
	g.fill();
}

function generate(p, g) {
	var rand = mulberry32(p.seed);
	g.setFill(1, 1, 1, 1);
	for (var i = 0; i < p.count; i++) {
		var x = rand() * p.width, y = rand() * p.height;
		var isBig = rand() < p.big;
		// small flakes: nearly crisp dots; big ones: out of focus, so bigger and softer
		var r = isBig ? p.size * (2.2 + rand() * 2.8) : p.size * (0.12 + Math.pow(rand(), 2.5) * 0.7);
		softDisc(g, x, y, r, isBig ? r * p.blur : r * p.blur * 0.4);
	}
}

8.12 Clouds (not in Presets — Soft clouds replaces it) — white only, shading by leaving gaps

Manga cumulus over a gray sky tone. The script paints only white; every gray you see is the tone beneath showing through. Each cloud is a row of round lobes with small bumps on top, sitting on a flat base. Each lobe is drawn as:

  1. a solid white core — one big circle, kept above the underside;
  2. coarse dots in the lower part, thinning toward the base, so the tone shows through as patchy shading;
  3. fine grain in a thin outer ring, thinning toward the edge — the dry-brush fluff.

Dots are all built into one path and filled once, and a budget (40000 dots) keeps it quick on every device. It is the heaviest sample here (about 70 ms on a Mac) — use Generate rather than Live on iPad.

// @jscg 1
// @name Clouds
// @param clouds int 1..20 default 9 "Clouds"
// @param size 30..400 default 95 "Size"
// @param fluff 0.05..0.8 default 0.3 "Fluffy edge"
// @param shade 0..1 default 0.75 "Underside shade"
// @param grain 0.4..3 default 0.9 "Grain"
// White cumulus for laying over a gray tone. Each cloud is thousands of tiny
// white dots: dense inside (solid white), thinning toward the edge (grainy
// fluff), and sparser on the underside, so the tone beneath shows as shading.

function mulberry32(seed) {
	var a = seed >>> 0;
	return function () {
		a = (a + 0x6D2B79F5) >>> 0;
		var t = a;
		t = Math.imul(t ^ (t >>> 15), t | 1);
		t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}

function dot(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();
}

// One cloud: big lobes along a flat base, bigger in the middle, with small bumps on their tops.
function makeCloud(rand, cx, cy, w, h) {
	var n = 4 + Math.floor(rand() * 4), lobes = [];
	for (var i = 0; i < n; i++) {
		var t = i / (n - 1);
		var bell = Math.sin(t * Math.PI);                       // 0 at the ends, 1 in the middle
		var r = h * (0.3 + bell * 0.35 + rand() * 0.12);
		var x = cx + (t - 0.5) * w + (rand() - 0.5) * w * 0.08;
		var y = cy - r * (0.45 + rand() * 0.3);                 // sits on the base (cy)
		lobes.push({ x: x, y: y, r: r });
		var bumps = 2 + Math.floor(rand() * 3);                 // small bumps on the upper rim
		for (var b = 0; b < bumps; b++) {
			var a = -Math.PI * (0.15 + rand() * 0.7);
			lobes.push({ x: x + Math.cos(a) * r * 0.8, y: y + Math.sin(a) * r * 0.8, r: r * (0.3 + rand() * 0.25) });
		}
	}
	return { lobes: lobes, base: cy, h: h };
}

// Dots in the ring rIn..rOut of lobe L, each of size `dotSize`; `inner` marks the shaded inside.
function grain(g, rand, p, c, L, rIn, rOut, dotSize, shadeTop, budget) {
	if (rOut <= rIn) { return 0; }
	var count = Math.min(budget, Math.round(Math.PI * (rOut * rOut - rIn * rIn) / (dotSize * dotSize) * 0.3));
	var drawn = 0;
	for (var j = 0; j < count; j++) {
		var a = rand() * Math.PI * 2;
		var rho = Math.sqrt(rIn * rIn + rand() * (rOut * rOut - rIn * rIn));     // even over the ring
		var x = L.x + Math.cos(a) * rho, y = L.y + Math.sin(a) * rho;
		if (y > c.base + rand() * c.h * 0.05) { continue; }                       // flat, slightly ragged base
		var keep = Math.min(1, (1 - rho / L.r) / p.fluff);                        // thin toward the edge
		var under = Math.max(0, (y - shadeTop) / (c.base - shadeTop));
		keep *= 1 - p.shade * under * (0.6 + rand() * 0.4);                        // grayer, patchy underside
		if (rand() < keep) { dot(g, x, y, dotSize * (0.7 + rand() * 0.8)); drawn++; }
	}
	return drawn;
}

function generate(p, g) {
	var rand = mulberry32(p.seed);
	var budget = 40000;                                         // total dots: keeps every device quick
	g.setFill(1, 1, 1, 1);
	for (var k = 0; k < p.clouds && budget > 0; k++) {
		var w = p.size * (0.8 + rand() * 0.8), h = w * (0.4 + rand() * 0.15);
		var c = makeCloud(rand, rand() * p.width, rand() * p.height, w, h);
		var shadeTop = c.base - h * 0.4;                        // the underside band starts here
		for (var i = 0; i < c.lobes.length && budget > 0; i++) {
			var L = c.lobes[i];
			var rim = L.r * (1 - p.fluff);                       // inside this, the lobe is "inner"
			// 1. Solid core, kept above the shaded underside.
			var core = Math.max(0, Math.min(rim, shadeTop - L.y));
			if (core > 0) { dot(g, L.x, L.y, core); }
			// 2. Coarse dots fill the inner part the core can't (the shaded underside)…
			budget -= grain(g, rand, p, c, L, core, rim, p.grain * 3, shadeTop, budget);
			// 3. …and fine grain makes the fluffy rim.
			budget -= grain(g, rand, p, c, L, rim, L.r, p.grain, shadeTop, budget);
		}
	}
	g.fill();   // core and grain, one fill
}

8.13 Glowing sparkles (in Presets) — glow and drop shadow

White sparkles with a soft white glow (setShadow with no offset), and an optional soft ink drop shadow below-right that lifts them off a light tone. Each star is filled up to three times: shadow, glow, then the crisp star with clearShadow().

// @jscg 1
// @name Glowing sparkles
// @param count int 3..120 default 22 "Sparkles"
// @param size 4..40 default 14 "Size"
// @param glow 0..30 default 12 "Glow"
// @param drop 0..12 default 0 "Drop shadow"
// White four-point sparkles with a soft white glow, for laying over a tone.
// "Drop shadow" adds a soft ink shadow below-right, so they pop off a light tone.

function mulberry32(seed) {
	var a = seed >>> 0;
	return function () {
		a = (a + 0x6D2B79F5) >>> 0;
		var t = a;
		t = Math.imul(t ^ (t >>> 15), t | 1);
		t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}

function star(g, x, y, r, pinch) {
	var k = r * pinch;
	g.moveTo(x, y - r);
	g.curveTo(x + k, y - k, x + k, y - k, x + r, y);
	g.curveTo(x + k, y + k, x + k, y + k, x, y + r);
	g.curveTo(x - k, y + k, x - k, y + k, x - r, y);
	g.curveTo(x - k, y - k, x - k, y - k, x, y - r);
	g.closePath();
}

function generate(p, g) {
	var rand = mulberry32(p.seed);
	g.setFill(1, 1, 1, 1);
	for (var i = 0; i < p.count; i++) {
		var x = rand() * p.width, y = rand() * p.height;
		var r = p.size * (0.4 + Math.pow(rand(), 2) * 1.3);
		if (p.drop > 0) {                                   // 1. soft ink shadow, below-right
			g.setShadow(p.drop, p.drop, p.drop, 0, 0, 0, 0.55);
			star(g, x, y, r, 0.14);
			g.fill();
		}
		if (p.glow > 0) {                                   // 2. white glow: no offset, just blur
			g.setShadow(0, 0, p.glow * r / p.size, 1, 1, 1, 1);
			star(g, x, y, r, 0.14);
			g.fill();
		}
		g.clearShadow();                                    // 3. the crisp star on top
		star(g, x, y, r, 0.14);
		g.fill();
	}
}

8.14 Soft clouds (in Presets) — clouds from blurred shapes

The Clouds of §8.12 rebuilt with shadows instead of particles: about 4 ms instead of 70, and about a twentieth of the drawing calls. Each cloud is three blurred fills (the far-away trick, §7.1):

  1. Body — every lobe plus a row of small under-puffs along the base, as one path filled once in see-through white, so overlaps don't add up and the tone shows through as a grayer underside. The under-puffs make the bottom gently scalloped and a little lower in the middle, rather than ruler-flat.
  2. Lit tops — the upper part of each lobe in full white.
  3. Puffs — a few soft bumps just inside the upper rim, blurred like the rest. Small puffs ON the outline read as beads; a little inside it they swell the outline into bumps. Twice the size and blur went too soft (walk 2026-10-02) — tune with Softness and Puffs.

The look is airbrushed rather than dry-brush — keep both and pick per page.

// @jscg 1
// @name Soft clouds
// @param clouds int 1..20 default 9 "Clouds"
// @param size 30..400 default 95 "Size"
// @param soft 0..0.6 default 0.18 "Softness"
// @param shade 0..1 default 0.5 "Underside shade"
// @param puffs 0..1 default 0.5 "Puffs"
// White cumulus for laying over a gray tone, made of blurred shapes instead of
// thousands of dots: a see-through white body (grayer underside) with a gently
// scalloped base, fully white lit tops, and soft puffs on the rim.
// White only: the gray is the tone beneath.

function mulberry32(seed) {
	var a = seed >>> 0;
	return function () {
		a = (a + 0x6D2B79F5) >>> 0;
		var t = a;
		t = Math.imul(t ^ (t >>> 15), t | 1);
		t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}

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();
}

// One cloud: big lobes along a flat base, bigger in the middle, with small bumps on their tops.
function makeCloud(rand, cx, cy, w, h) {
	var n = 4 + Math.floor(rand() * 4), lobes = [];
	for (var i = 0; i < n; i++) {
		var t = i / (n - 1);
		var bell = Math.sin(t * Math.PI);
		var r = h * (0.3 + bell * 0.35 + rand() * 0.12);
		var x = cx + (t - 0.5) * w + (rand() - 0.5) * w * 0.08;
		var y = cy - r * (0.45 + rand() * 0.3);
		lobes.push({ x: x, y: y, r: r });
		var bumps = 2 + Math.floor(rand() * 3);
		for (var b = 0; b < bumps; b++) {
			var a = -Math.PI * (0.15 + rand() * 0.7);
			lobes.push({ x: x + Math.cos(a) * r * 0.8, y: y + Math.sin(a) * r * 0.8, r: r * (0.3 + rand() * 0.25) });
		}
	}
	return { lobes: lobes, base: cy, h: h };
}

var FAR = 5000;   // the far-away trick (guide §7.1): only the blurred shadow lands in the region

// Fill the union of `discs` in white at `alpha`, blurred by `blur` points.
// One fill of one path = one blurred shape, so overlaps don't add up.
function blurredUnion(g, discs, alpha, blur) {
	g.setShadow(-FAR, 0, blur, 1, 1, 1, alpha);
	for (var i = 0; i < discs.length; i++) { circle(g, discs[i].x + FAR, discs[i].y, discs[i].r); }
	g.fill();
}

function generate(p, g) {
	var rand = mulberry32(p.seed);
	g.setFill(1, 1, 1, 1);
	for (var k = 0; k < p.clouds; k++) {
		var w = p.size * (0.8 + rand() * 0.8), h = w * (0.4 + rand() * 0.15);
		var c = makeCloud(rand, rand() * p.width, rand() * p.height, w, h);
		var blur = h * p.soft;

		var left = c.lobes[0].x, right = c.lobes[0].x;
		for (var m = 0; m < c.lobes.length; m++) {
			left = Math.min(left, c.lobes[m].x);
			right = Math.max(right, c.lobes[m].x);
		}

		// 1. Body: every lobe, see-through white — the tone shows through as the shaded underside —
		//    plus a row of small under-puffs along the base: a scalloped bottom, a little lower in the middle.
		var body = [];
		for (var i = 0; i < c.lobes.length; i++) {
			var L = c.lobes[i];
			body.push({ x: L.x, y: L.y, r: Math.min(L.r, c.base - L.y + L.r * 0.15) });
		}
		var under = Math.max(3, Math.round((right - left) / (h * 0.3)));
		for (var u = 0; u <= under; u++) {
			var t = u / under;
			var r = h * (0.16 + rand() * 0.1);
			body.push({
				x: left + t * (right - left) + (rand() - 0.5) * h * 0.1,
				y: c.base - r * 0.75 + Math.sin(t * Math.PI) * h * 0.08 + (rand() - 0.5) * h * 0.05,
				r: r
			});
		}
		blurredUnion(g, body, 1 - p.shade * 0.55, blur);

		// 2. Lit tops: each lobe's upper part, fully white.
		var tops = [];
		for (var j = 0; j < c.lobes.length; j++) {
			var T = c.lobes[j];
			tops.push({ x: T.x, y: T.y - T.r * 0.22, r: T.r * 0.72 });
		}
		blurredUnion(g, tops, 1, blur * 0.8);

		// 3. Puffs: a few bigger soft bumps just inside the upper rim — they swell the outline, not bead on it.
		var puffs = [];
		var count = Math.round(c.lobes.length * 6 * p.puffs);
		for (var d = 0; d < count; d++) {
			var D = c.lobes[Math.floor(rand() * c.lobes.length)];
			var a = -Math.PI * (0.1 + rand() * 0.8);             // upper rim
			var rho = D.r * (0.7 + rand() * 0.2);                 // a little inside the edge
			puffs.push({ x: D.x + Math.cos(a) * rho, y: D.y + Math.sin(a) * rho, r: h * (0.08 + rand() * 0.07) });
		}
		blurredUnion(g, puffs, 1, blur * 0.7);
	}
	g.clearShadow();
}

8.15 Light from above (in Presets) — a linear gradient

White light fading in from one edge of the region, for laying over a tone. Angle tilts where it comes from; Reach is how far across it fades out.

// @jscg 1
// @name Light from above
// @param strength 0..1 default 0.9 "Strength"
// @param reach 0.1..1 default 0.6 "Reach"
// @param angle -60..60 default 0 "Angle"
// White light fading in from one edge of the region — lay it over a tone.
// A linear gradient: solid white at the edge, clear by `reach` of the way across.

function generate(p, g) {
	var a = p.angle * Math.PI / 180;
	var dx = Math.sin(a), dy = Math.cos(a);                       // the light's direction: down, tilted by angle
	var across = Math.abs(p.width * dx) + Math.abs(p.height * dy); // the box measured along that direction
	var cx = p.width / 2, cy = p.height / 2;
	var x0 = cx - dx * across / 2, y0 = cy - dy * across / 2;      // the edge the light comes from
	var x1 = x0 + dx * across * p.reach, y1 = y0 + dy * across * p.reach;

	g.addGradientStop(0, 1, 1, 1, p.strength);
	g.addGradientStop(1, 1, 1, 1, 0);
	g.moveTo(0, 0); g.lineTo(p.width, 0); g.lineTo(p.width, p.height); g.lineTo(0, p.height); g.closePath();
	g.fillLinearGradient(x0, y0, x1, y1);
}

8.16 Moon (in Presets) — a radial gradient

A white moon with a soft halo, for laying over a dark tone. The halo is a radial gradient from the moon's edge outwards — three stops make the fall-off softer than two; the moon is a plain white disc on top.

// @jscg 1
// @name Moon
// @param x 0..1 default 0.7 "Across"
// @param y 0..1 default 0.3 "Down"
// @param size 5..200 default 40 "Size"
// @param halo 0..5 default 2 "Halo"
// @param glow 0..1 default 0.7 "Glow"
// A white moon with a soft radial halo — lay it over a dark tone.
// The halo is a radial gradient around the moon; the moon is a plain white disc on top.

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();
}

function generate(p, g) {
	var cx = p.x * p.width, cy = p.y * p.height, r = p.size;

	// 1. Halo: from the moon's edge outwards, white fading to clear. Three stops give a softer fall-off.
	if (p.halo > 0) {
		g.addGradientStop(0, 1, 1, 1, p.glow);
		g.addGradientStop(0.35, 1, 1, 1, p.glow * 0.35);
		g.addGradientStop(1, 1, 1, 1, 0);
		g.moveTo(0, 0); g.lineTo(p.width, 0); g.lineTo(p.width, p.height); g.lineTo(0, p.height); g.closePath();
		g.fillRadialGradient(cx, cy, r, r * (1 + p.halo));
	}

	// 2. The moon itself.
	g.setFill(1, 1, 1, 1);
	circle(g, cx, cy, r);
	g.fill();
}

8.17 Shock (in Presets) — tapering ribbons and bristling bursts

An emotion tone — shock, anger, a sudden realisation: solid white bursts on black, joined by cracks that leave each burst as a wide wedge, thin to a hairline, and flare into sharp wedges on the way. Three techniques:

Turn on Invert for black bursts and cracks on white.

// @jscg 1
// @name Shock
// @param stars int 3..60 default 18 "Bursts"
// @param size 8..80 default 42 "Burst size"
// @param links int 0..4 default 3 "Cracks per burst"
// @param jag 0..1 default 0.5 "Jaggedness"
// @param glow 0..1 default 0.2 "Glow"
// @param ground 0..1 default 1 "Black ground"
// Solid white bursts and tapering cracks that grow out of them — for shock, anger, a sudden realisation.

function mulberry32(seed) {
	var a = seed >>> 0;
	return function () {
		a = (a + 0x6D2B79F5) >>> 0;
		var t = a;
		t = Math.imul(t ^ (t >>> 15), t | 1);
		t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}

// A jagged centre line from (x0, y0) to (x1, y1): midpoint displacement, `depth` times.
function jagged(rand, x0, y0, x1, y1, amount, depth) {
	var pts = [[x0, y0], [x1, y1]];
	var spread = Math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)) * 0.25 * amount;
	for (var d = 0; d < depth; d++) {
		var next = [pts[0]];
		for (var i = 1; i < pts.length; i++) {
			var a = pts[i - 1], b = pts[i];
			var dx = b[0] - a[0], dy = b[1] - a[1];
			var len = Math.sqrt(dx * dx + dy * dy) || 1;
			var off = (rand() - 0.5) * 2 * spread;
			next.push([(a[0] + b[0]) / 2 - dy / len * off, (a[1] + b[1]) / 2 + dx / len * off]);
			next.push(b);
		}
		pts = next;
		spread *= 0.48;
	}
	return pts;
}

// Fills a ribbon along `pts`, `widths[i]` wide at each point: hairlines that swell into sharp wedges.
function ribbon(g, pts, widths) {
	var n = pts.length, left = [], right = [];
	for (var i = 0; i < n; i++) {
		var a = pts[Math.max(0, i - 1)], b = pts[Math.min(n - 1, i + 1)];
		var dx = b[0] - a[0], dy = b[1] - a[1];
		var len = Math.sqrt(dx * dx + dy * dy) || 1;
		var h = widths[i] / 2;
		left.push([pts[i][0] - dy / len * h, pts[i][1] + dx / len * h]);
		right.push([pts[i][0] + dy / len * h, pts[i][1] - dx / len * h]);
	}
	g.moveTo(left[0][0], left[0][1]);
	for (var j = 1; j < n; j++) g.lineTo(left[j][0], left[j][1]);
	for (var k = n - 1; k >= 0; k--) g.lineTo(right[k][0], right[k][1]);
	g.closePath();
	g.fill();
}

// A crack between two bursts: wide where it leaves each burst, a hairline between, with a few sudden wedges.
function crack(g, rand, from, to, p) {
	var pts = jagged(rand, from.x, from.y, to.x, to.y, 0.5 + p.jag, 5);
	var n = pts.length, w = [];
	var wedges = [];
	for (var q = 0; q < 3 + Math.floor(rand() * 3); q++) wedges.push({ at: 0.15 + rand() * 0.7, width: 3 + rand() * 5, reach: 0.04 + rand() * 0.06 });
	for (var i = 0; i < n; i++) {
		var t = i / (n - 1);
		var v = 1.8;
		v = Math.max(v, from.r * 0.3 * (1 - t / 0.3));  // grows out of the burst…
		v = Math.max(v, to.r * 0.3 * (1 - (1 - t) / 0.3));  // …and into the other
		for (var k = 0; k < wedges.length; k++) {
			var d = Math.abs(t - wedges[k].at) / wedges[k].reach;
			if (d < 1) v = Math.max(v, wedges[k].width * (1 - d));
		}
		w.push(v);
	}
	ribbon(g, pts, w);

	// A few short offshoots, tapering to a point.
	for (var b = 2; b < n - 2; b++) {
		if (rand() > 0.05 + p.jag * 0.05) continue;
		var a0 = pts[b - 1], a1 = pts[b + 1];
		var dir = Math.atan2(a1[1] - a0[1], a1[0] - a0[0]) + (rand() < 0.5 ? -1 : 1) * (0.5 + rand() * 0.7);
		var len = 12 + rand() * 30;
		var bp = jagged(rand, pts[b][0], pts[b][1], pts[b][0] + Math.cos(dir) * len, pts[b][1] + Math.sin(dir) * len, 0.7 + p.jag, 3);
		var bw = [];
		for (var m = 0; m < bp.length; m++) bw.push(w[b] * 0.8 * (1 - m / (bp.length - 1)) + 0.1);
		ribbon(g, bp, bw);
	}
}

// A solid burst: a compact core bristling with spikes — main spikes at uneven angles, narrower teeth between them.
function burst(g, rand, s, glow) {
	var spikes = [], a = rand() * 2 * Math.PI, end = a + 2 * Math.PI;
	for (var k = 0; k < 60 && a < end - 0.1; k++) {                // at most 53 fit: the step is at least 0.12
		var main = k % 2 === 0 || rand() < 0.3;
		spikes.push({ a: a, r: s.r * (main ? 0.7 + rand() * 0.45 : 0.4 + rand() * 0.25) });
		a += 0.12 + rand() * 0.2;
	}
	if (glow > 0) g.setShadow(0, 0, s.r * 0.3 * glow, 1, 1, 1, 1);
	for (var i = 0; i < spikes.length; i++) {
		var sp = spikes[i], nx = spikes[(i + 1) % spikes.length];
		var na = i + 1 < spikes.length ? nx.a : nx.a + 2 * Math.PI;
		var tip = sp.a + (rand() - 0.5) * 0.05;
		var valley = (sp.a + na) / 2, vr = s.r * (0.26 + rand() * 0.08);
		if (i === 0) g.moveTo(s.x + Math.cos(tip) * sp.r, s.y + Math.sin(tip) * sp.r);
		else g.lineTo(s.x + Math.cos(tip) * sp.r, s.y + Math.sin(tip) * sp.r);
		g.lineTo(s.x + Math.cos(valley) * vr, s.y + Math.sin(valley) * vr);
	}
	g.closePath();
	g.fill();
	if (glow > 0) g.clearShadow();
}

function generate(p, g) {
	var rand = mulberry32(p.seed);
	var W = p.width, H = p.height;
	if (p.ground > 0) {
		g.setFill(0, 0, 0, p.ground);
		g.moveTo(0, 0); g.lineTo(W, 0); g.lineTo(W, H); g.lineTo(0, H); g.closePath();
		g.fill();
	}

	// A jittered grid that runs one cell past every edge, so the pattern reads as a crop of a larger one.
	var cell = Math.sqrt(W * H / p.stars);
	var cols = Math.max(1, Math.round(W / cell)), rows = Math.max(1, Math.round(H / cell));
	var cw = W / cols, ch = H / rows;
	var bursts = [];
	for (var row = -1; row <= rows; row++) {
		for (var col = -1; col <= cols; col++) {
			var inside = row >= 0 && row < rows && col >= 0 && col < cols;
			if (rand() < (inside ? 0.05 : 0.4)) continue;  // some cells stay empty: generous black gaps
			var big = rand() < 0.15;
			bursts.push({
				x: (col + 0.05 + rand() * 0.9) * cw,
				y: (row + 0.05 + rand() * 0.9) * ch,
				r: p.size * (big ? 1.05 + rand() * 0.35 : 0.6 + rand() * 0.4),
			});
		}
	}

	// Each burst sends cracks to its nearest neighbours, never two in nearly the same direction (no knots).
	g.setFill(1, 1, 1, 1);
	var joined = {};
	for (var i = 0; i < bursts.length; i++) {
		var near = [];
		for (var j = 0; j < bursts.length; j++) {
			if (j === i) continue;
			var dx = bursts[j].x - bursts[i].x, dy = bursts[j].y - bursts[i].y;
			near.push({ j: j, d: dx * dx + dy * dy, a: Math.atan2(dy, dx) });
		}
		near.sort(function (a, b) { return a.d - b.d; });
		var taken = [];
		for (var k = 0; k < near.length && taken.length < p.links; k++) {
			var clash = false;
			for (var t = 0; t < taken.length; t++) {
				var da = Math.abs(near[k].a - taken[t]);
				if (Math.min(da, 2 * Math.PI - da) < 0.9) clash = true;
			}
			if (clash) continue;
			taken.push(near[k].a);
			var key = Math.min(i, near[k].j) + "-" + Math.max(i, near[k].j);
			if (joined[key]) continue;
			joined[key] = true;
			crack(g, rand, bursts[i], bursts[near[k].j], p);
		}
	}

	for (var m = 0; m < bursts.length; m++) burst(g, rand, bursts[m], p.glow);
}

8.18 Flames (in Presets) — streaks riding a flow

An emotion tone — rage, passion, a burning resolve: a blaze in the way of a screentone shaved with a cutter. No flame is drawn as a shape; the fire is thousands of tapered white streaks, and its shape comes from where they crowd:

Turn on Invert for black flames on white.

// @jscg 1
// @name Flames
// @param count int 200..6000 default 2400 "Streaks"
// @param spread 0..1 default 0.5 "Fan out"
// @param curl 0..1 default 0.5 "Curl"
// @param dark 0..1 default 0.85 "Darkness"
// A roaring blaze, drawn the way a tone is shaved: thousands of tapered white streaks riding one rising flow —
// thick where the fire is strong, thinning into gray channels where it isn't. For rage, passion, a burning resolve.

function mulberry32(seed) {
	var a = seed >>> 0;
	return function () {
		a = (a + 0x6D2B79F5) >>> 0;
		var t = a;
		t = Math.imul(t ^ (t >>> 15), t | 1);
		t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}

function generate(p, g) {
	var rand = mulberry32(p.seed);
	var W = p.width, H = p.height, S = Math.min(W, H);

	// Smooth random waves: the same seed always shapes the same fire.
	var waves = [];
	for (var i = 0; i < 5; i++) waves.push({ fx: (0.6 + rand() * 1.6) / S, fy: (0.6 + rand() * 1.6) / S, ph: rand() * 6.3, k: rand() * 2 - 1 });
	function noise(x, y) {
		var v = 0;
		for (var i = 0; i < waves.length; i++) v += Math.sin(x * waves[i].fx * 6.3 + y * waves[i].fy * 6.3 * waves[i].k + waves[i].ph);
		return v / waves.length;   // about -1 … 1
	}
	var centre = W * (0.4 + rand() * 0.2);

	// How strong the fire is here: a surge in the centre, strongest low down, broken up by the waves.
	function strength(x, y) {
		var across = (x - centre) / (W * 0.45);
		var surge = Math.exp(-across * across);
		var rise = Math.min(1, (y / H) * 1.3 + 0.15);
		var raw = surge * 0.75 + rise * 0.45 + noise(x, y) * 0.7 - 0.35;
		var t = Math.max(0, Math.min(1, (raw - 0.05) / 0.8));
		return t * t * (3 - 2 * t);   // a sharp step: strong fire is solid, weak fire is open gray
	}
	// Which way the fire moves here: up, fanning out from the centre, swaying with the waves.
	function flow(x, y) {
		var dx = (x - centre) / W * p.spread * 1.6 + noise(y * 0.7, x * 0.7) * p.curl * 0.9;
		var len = Math.sqrt(dx * dx + 1);
		return [dx / len, -1 / len];
	}

	// 1. The ground: dark at the top, clear at the bottom.
	g.addGradientStop(0, 0, 0, 0, p.dark);
	g.addGradientStop(1, 0, 0, 0, 0);
	g.moveTo(0, 0); g.lineTo(W, 0); g.lineTo(W, H); g.lineTo(0, H); g.closePath();
	g.fillLinearGradient(0, 0, 0, H);

	// 2. Streaks, in two passes: a soft see-through one behind, then solid white.
	var passes = [{ alpha: 0.35, share: 0.4, boost: 0.25 }, { alpha: 1, share: 0.6, boost: 0 }];
	for (var q = 0; q < passes.length; q++) {
		var P = passes[q];
		g.setFill(1, 1, 1, P.alpha);
		var n = Math.round(p.count * P.share);
		for (var s = 0; s < n; s++) {
			var x = -W * 0.1 + rand() * W * 1.2, y = rand() * H * 1.1;
			var st = Math.min(1, strength(x, y) + P.boost);
			if (rand() > st) continue;                 // weak fire: fewer streaks, so the gray shows through
			// Thick streaks are long, thin ones short: one size draw sets both, with a little play in the width.
			var size = rand();
			var len = S * (0.04 + st * 0.22) * (0.5 + size);
			var w = 1 + Math.pow(st, 1.5) * S * 0.016 * (0.4 + size * 0.8 + rand() * 0.2);
			var steps = 12, pts = [];
			var px = x, py = y;
			for (var k = 0; k <= steps; k++) {
				pts.push([px, py]);
				var d = flow(px, py);
				px += d[0] * len / steps; py += d[1] * len / steps;
			}
			// A tapered ribbon: widest in the middle, both ends drawn out into the same fine needle — the root dissolves
			// like the tip, so where many ends meet they fade instead of notching.
			var left = [], right = [];
			for (var k2 = 0; k2 <= steps; k2++) {
				var t = k2 / steps;
				var a = pts[Math.max(0, k2 - 1)], b = pts[Math.min(steps, k2 + 1)];
				var dx = b[0] - a[0], dy = b[1] - a[1], l = Math.sqrt(dx * dx + dy * dy) || 1;
				var half = w / 2 * Math.pow(Math.sin(Math.PI * t), 1.8);
				left.push([pts[k2][0] - dy / l * half, pts[k2][1] + dx / l * half]);
				right.push([pts[k2][0] + dy / l * half, pts[k2][1] - dx / l * half]);
			}
			g.moveTo(left[0][0], left[0][1]);
			for (var j = 1; j <= steps; j++) g.lineTo(left[j][0], left[j][1]);
			for (var j2 = steps; j2 >= 0; j2--) g.lineTo(right[j2][0], right[j2][1]);
			g.closePath();
		}
		g.fill();
	}

	// 3. The base glows: white rising from the bottom edge.
	g.addGradientStop(0, 1, 1, 1, 1);
	g.addGradientStop(1, 1, 1, 1, 0);
	g.moveTo(0, H * 0.75); g.lineTo(W, H * 0.75); g.lineTo(W, H); g.lineTo(0, H); g.closePath();
	g.fillLinearGradient(0, H, 0, H * 0.75);
}

9. When something goes wrong

What you seeWhat it meansWhat to do
Done stays open: "Header line N: …"a // @param line is malformedfix that line (§3)
Done stays open: "Line N: SyntaxError …"the code doesn't parse — a missing } or ), an unclosed quotefix that line; nothing has run
⚠️ "generate() threw: …"the code ran and failed — often a misspelt name (p.cuont)fix it, then Generate
⚠️ "Opcode moveTo at index N has a NaN or infinite argument"a coordinate became NaN — usually the script reads a slider the header doesn't declare (p.size with no @param size), or divides by zerocheck every p.<name> against the header
⚠️ "… exceeded … entries", too many operationsthe drawing is too biglower the count (§10)
⚠️ "This script took too long."it ran past the time limit (§10) — usually an endless or huge loopfix the loop, then Generate (it retries once with a longer limit)
⚠️ "This script didn't finish — the app was closed while it ran."the app ended while it ranfix the loop (§10), then Generate
⚠️ "Scripts paused — quit and reopen MangaLoft."two scripts got stuck this sessionquit and reopen; fix the scripts that got stuck

A script that failed is not run again by itself — not on redraw, reopening, or relaunch — until you press Generate or Done. Every tone that uses the same script is spared.

An endless loop doesn't freeze the app, but it can't be stopped either. Scripts run off the main thread: past the time limit the app gives up on it, marks it "took too long", and carries on. The stuck script keeps one processor core busy until the app quits, and after two stuck scripts in one session scripts pause until you reopen the app. Make every loop count to a fixed number — for (var i = 0; i < p.count; i++) — and give "try until it fits" loops a ceiling (the Whole flowers sample stops after count × 20 tries).

10. Tips and tricks

Speed

Look

Structure

11. Asking an AI agent for a script

Give the agent this whole guide, then something like:

Write a MangaLoft Script Tone script following this guide. Effect: [e.g. "soft falling feathers, mostly small, slightly curved, drifting down to the right"]. Give me sliders for [count, size, angle]. Use only mulberry32 for randomness, keep loops bounded, start with // @jscg 1, and reply with the script only.

Then paste it into Edit Script… → Done. If Done stays open, paste the error line back to the agent.

12. Reference