Skip to content

Realistic watercolor drawings

TypeScript paintwatercolor

Make single-line text work well on phones

Claudebcc318d96bb0
diff --git a/src/draw/StrokeWriter.ts b/src/draw/StrokeWriter.ts
index 8b9cac8..5c00f5d 100644
--- a/src/draw/StrokeWriter.ts
+++ b/src/draw/StrokeWriter.ts
@@ -24,6 +24,8 @@ export interface WriteOptions {
wobble: number;
/** Name of the undo step the whole write becomes. */
label: string;
+ /** Widest footprint radius allowed, in document heights (the tool's own size when unset). */
+ maxRadius?: number;
}
export interface WriteCallbacks {
@@ -138,7 +140,7 @@ export class StrokeWriter {
}
if (this.phase === 'begin') {
const [x, y] = this.at(run, 0);
- if (this.engine.scriptBegin(tool, x, y, this.pressureAt(run, 0), this.first ? this.opts.label : false)) {
+ if (this.engine.scriptBegin(tool, x, y, this.pressureAt(run, 0), this.first ? this.opts.label : false, this.opts.maxRadius)) {
this.first = false;
this.phase = 'draw';
this.dist = 0;
diff --git a/src/engine/Engine.ts b/src/engine/Engine.ts
index 04f043f..d68c36c 100644
--- a/src/engine/Engine.ts
+++ b/src/engine/Engine.ts
@@ -154,6 +154,8 @@ export class WatercolorEngine {
fingerIsWater = true;
/** When false, pointers on the paper are ignored (another tool, e.g. text, is driving it). */
interactive = true;
+ /** Footprint scale of the scripted stroke in progress (see scriptBegin). */
+ private scriptScale = 1;
private nav = false;
private tipTextures = new Map<string, WebGLTexture>();
@@ -724,11 +726,14 @@ export class WatercolorEngine {
* Start a stroke driven by code rather than a pointer; it runs through the same
* pipeline (spacing, speed thinning, dwell, undo). Returns false while another
* stroke is still on the paper. A string `step` opens an undo step with that label;
- * pass false to group several strokes (the letters of a word) into one.
+ * pass false to group several strokes (the letters of a word) into one. `maxRadius`
+ * (document heights) shrinks a footprint that would be wider than that at full
+ * pressure, so a big brush still writes small letters legibly.
*/
- scriptBegin(tool: Tool, x: number, y: number, pressure: number, step: string | false = 'Stroke'): boolean {
+ scriptBegin(tool: Tool, x: number, y: number, pressure: number, step: string | false = 'Stroke', maxRadius?: number): boolean {
if (this.stroke) return false;
if (step !== false) this.beginStep('script', step);
+ this.scriptScale = maxRadius ? Math.min(1, maxRadius / this.radius(tool, 1, 0)) : 1;
this.stroke = {
pointerId: SCRIPT_POINTER, pointerType: 'pen', tool, started: false, ending: false,
x: 0, y: 0, cx: 0, cy: 0, pr: 0, t: performance.now(), speed: 0, simPressure: 0.45, carry: 0, travelled: 0, moved: false,
@@ -1233,7 +1238,7 @@ export class WatercolorEngine {
/* -------------------------------------------------------------- strokes */
- private sizeMult() { return Math.pow(3, (this.params.size - 0.5) * 2); }
+ private sizeMult() { return Math.pow(3, (this.params.size - 0.5) * 2) * (this.scripting ? this.scriptScale : 1); }
/** Footprint radius in document heights. */
private radius(tool: Tool, pr: number, speed: number) {
diff --git a/src/styles.css b/src/styles.css
index 99778b6..c6a1eed 100644
--- a/src/styles.css
+++ b/src/styles.css
@@ -297,7 +297,12 @@ input[type="range"]::-moz-range-thumb { width: 11px; height: 11px; border-radius
width: 316px; max-height: calc(100vh - 28px);
display: flex; flex-direction: column;
}
-.tool-body { display: flex; flex-direction: column; gap: 10px; padding: 10px 12px 10px; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; }
+.tool-body { display: flex; flex-direction: column; gap: 10px; padding: 10px 12px 8px; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; }
+.panel-head .head-actions { grid-column: 2; grid-row: 1 / span 2; display: flex; gap: 2px; }
+.panel button.fold { display: none; }
+.panel button.fold svg { transform: rotate(90deg); transition: transform 0.15s; }
+.panel button.fold.folded svg { transform: rotate(-90deg); }
+.tool-panel.folded .tool-body { display: none; }
.text-input {
width: 100%; font: 15px/1.3 "Avenir Next", "Helvetica Neue", system-ui, sans-serif; color: var(--ink);
padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line);
@@ -332,7 +337,7 @@ input[type="range"]::-moz-range-thumb { width: 11px; height: 11px; border-radius
.panel .seg button { flex: 1; padding: 5px 4px; border-radius: 7px; font-size: 9px; letter-spacing: 0.12em; text-transform: uppercase; }
.panel .seg button.on { background: rgba(255, 255, 255, 0.85); color: var(--ink); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06); }
-.tool-foot { display: flex; align-items: center; gap: 8px; min-height: 22px; font-size: 10px; color: var(--ink-soft); }
+.tool-foot { flex: none; display: flex; align-items: center; gap: 8px; min-height: 22px; padding: 0 12px 10px; font-size: 10px; color: var(--ink-soft); }
.tool-foot .status { flex: 1; }
.panel .stop { flex-direction: row; gap: 5px; padding: 4px 8px; border: 1px solid var(--line); color: #a0281c; }
.tool-foot .credit { font-size: 10px; color: var(--ink-faint); text-decoration: none; white-space: nowrap; }
@@ -341,9 +346,15 @@ input[type="range"]::-moz-range-thumb { width: 11px; height: 11px; border-radius
@media (max-width: 640px) {
.tool-panel {
left: var(--m-gap); right: var(--m-gap); top: auto; transform: none; width: auto;
- bottom: var(--m-bottom); max-height: min(56vh, calc(100dvh - var(--m-top) - var(--m-bottom) - 50px));
+ bottom: var(--m-bottom); max-height: min(46vh, calc(100dvh - var(--m-top) - var(--m-bottom) - 50px));
}
.tool-panel .panel-head .sub { display: none; }
+ .tool-panel .panel-head { padding-top: 8px; padding-bottom: 6px; align-items: center; }
+ .tool-panel .panel-head > span:first-child { align-self: center; }
+ .panel button.fold { display: flex; }
+ .tool-panel.folded .panel-head { border-bottom: 0; }
+ .tool-foot { font-size: 11px; color: var(--ink); padding-top: 2px; }
+ .panel .stop span { display: inline; }
.fontlist { max-height: 138px; }
.fontrow { padding: 3px 6px; }
.tool-foot .credit { display: none; }
diff --git a/src/text/TextStore.ts b/src/text/TextStore.ts
index 5d68b06..1519fc6 100644
--- a/src/text/TextStore.ts
+++ b/src/text/TextStore.ts
@@ -33,6 +33,9 @@ const DEFAULTS: TextSettings = {
size: 0.4, speed: 0.5, pressure: 0.6, taper: 0.5, spacing: 0, wobble: 0.2,
};
+/** Widest brush footprint radius for text, in em: thinner than a letter's counters. */
+const MAX_RADIUS_EM = 0.06;
+
/** Em height in document heights. */
export const emSize = (v: number) => 0.03 * Math.pow(12, v);
@@ -111,7 +114,7 @@ export class TextStore implements PanelTool {
this.progress.value = [0, strokes.length];
const text = s.text.trim();
this.writer.write(strokes, anchor, {
- tool: s.tool, ref: em, speed: penSpeed(s.speed), pressure: s.pressure, taper: s.taper, wobble: s.wobble,
+ tool: s.tool, ref: em, maxRadius: em * MAX_RADIUS_EM, speed: penSpeed(s.speed), pressure: s.pressure, taper: s.taper, wobble: s.wobble,
label: `Text “${text.length > 18 ? `${text.slice(0, 18)}…` : text}”`,
}, {
onProgress: (done, total) => { this.progress.value = [done, total]; },
diff --git a/src/ui/Panel.tsx b/src/ui/Panel.tsx
index 24059b0..73b4c9b 100644
--- a/src/ui/Panel.tsx
+++ b/src/ui/Panel.tsx
@@ -9,13 +9,14 @@ export function Panel({ class: cls, open = true, role, label, children, ...rest
return <div class={`panel ${cls}`} hidden={!open} role={role} aria-label={label} {...rest}>{children}</div>;
}
-/** Title, optional one-line explanation and an optional close button. */
-export function PanelHead({ title, sub, onClose }: { title: string; sub?: string; onClose?: () => void }) {
+/** Title, optional one-line explanation, optional extra buttons and an optional close button. */
+export function PanelHead({ title, sub, onClose, actions }: { title: string; sub?: string; onClose?: () => void; actions?: ComponentChildren }) {
+ const close = onClose && <button class="icon" title="Close" onClick={onClose}><Icon name="close" size={16} /></button>;
return (
<div class="panel-head">
<span>{title}</span>
{sub && <span class="sub">{sub}</span>}
- {onClose && <button class="icon" title="Close" onClick={onClose}><Icon name="close" size={16} /></button>}
+ {actions ? <div class="head-actions">{actions}{close}</div> : close}
</div>
);
}
diff --git a/src/ui/ToolPanel.tsx b/src/ui/ToolPanel.tsx
index 16f3e03..e7349e9 100644
--- a/src/ui/ToolPanel.tsx
+++ b/src/ui/ToolPanel.tsx
@@ -1,25 +1,36 @@
import type { ComponentChildren } from 'preact';
+import { useEffect, useState } from 'preact/hooks';
import { Icon } from './Icon';
import { Panel, PanelHead } from './Panel';
+const phone = () => window.matchMedia('(max-width: 640px)').matches;
+
/**
* The layout shared by the text, shape and fill tools: a head, a scrolling body of
* controls, and a foot with a status line and a Stop button while the tool is busy.
+ * On phones the body folds away (by hand, or when the tool starts drawing) so the
+ * paper underneath stays in view.
*/
export function ToolPanel({ title, sub, open, status, busy, onStop, onClose, class: cls = '', children, foot }: {
title: string; sub: string; open: boolean; status: string; busy: boolean;
onStop(): void; onClose(): void; class?: string; children: ComponentChildren; foot?: ComponentChildren;
}) {
+ const [folded, setFolded] = useState(false);
+ useEffect(() => { if (busy && phone()) setFolded(true); }, [busy]);
+ useEffect(() => { if (!open) setFolded(false); }, [open]);
+
+ const fold = (
+ <button class={`icon fold${folded ? ' folded' : ''}`} title={folded ? 'Show settings' : 'Hide settings'}
+ aria-expanded={!folded} onClick={() => setFolded(!folded)}><Icon name="chevron" size={16} /></button>
+ );
return (
- <Panel class={`tool-panel ${cls}`} open={open} role="dialog" label={title}>
- <PanelHead title={title} sub={sub} onClose={onClose} />
- <div class="tool-body">
- {children}
- <div class="tool-foot">
- <span class="status">{status}</span>
- {busy && <button class="stop" onClick={onStop}><Icon name="close" size={14} /><span>Stop</span></button>}
- {foot}
- </div>
+ <Panel class={`tool-panel ${cls}${folded ? ' folded' : ''}`} open={open} role="dialog" label={title}>
+ <PanelHead title={title} sub={sub} onClose={onClose} actions={fold} />
+ <div class="tool-body">{children}</div>
+ <div class="tool-foot">
+ <span class="status">{status}</span>
+ {busy && <button class="stop" onClick={onStop}><Icon name="close" size={14} /><span>Stop</span></button>}
+ {foot}
</div>
</Panel>
);