A pure Rust brush engine inspired by libmypaint, designed for WebAssembly and native targets.
🎨 Try the live demo — draws in your browser using the real libmypaint brushes, with stylus pressure and tilt where the device supports it.
- 🦀 Pure Rust, no
unsafe— clean WASM (wasm32-unknown-unknown) story. - 📦 libmypaint
.mybJSON compatibility — brushes authored for MyPaint / Krita load and round-trip without translation. - 🎯 Pixel-level parity with libmypaint — same fix15 math, same tile layout, same stroke math. Compatibility is the design priority; the "Hokusai" name does not imply behavioural divergence.
- 🔌 Pluggable surfaces via the
TiledSurfacetrait. Backends are split into feature-gated crates. - 🗺️ Tile-based infinite canvas — 64×64 RGBA fix15 tiles, matching libmypaint exactly so dab traversal and rounding stay bit-identical.
hokusai/
├── crates/
│ ├── hokusai-core/ # Brush types, stroke engine, fix15, tiles, brushmodes
│ ├── hokusai-brush/ # libmypaint `.myb` JSON read / write
│ ├── hokusai-tile-mem/ # Reference in-memory TiledSurface
│ ├── hokusai-tiny-skia/ # Flatten TiledSurface tiles into a tiny-skia Pixmap
│ ├── hokusai-compat/ # Snapshot regression harness (libmypaint parity track)
│ └── hokusai-wasm/ # wasm-bindgen bindings + browser demo
└── hokusai/ # Umbrella crate that re-exports the above via features
└── examples/ # stroke_to_png, myb_to_png (+ vendored .myb fixtures)
use hokusai::{Brush, BrushSetting, BrushState};
use hokusai::myb;
use hokusai::tile_mem::MemSurface;
let json = std::fs::read_to_string("charcoal.myb")?;
let brush: Brush = myb::from_str(&json)?;
let mut state = BrushState::default();
let mut surface = MemSurface::new();
// First call seeds position only; subsequent calls emit dabs.
brush.stroke_to(&mut state, &mut surface, 10.0, 50.0, 0.0, 0.0, 0.0, 0.01);
brush.stroke_to(&mut state, &mut surface, 200.0, 50.0, 1.0, 0.0, 0.0, 0.01);
# Ok::<(), Box<dyn std::error::Error>>(())Run the bundled examples to render to PNG:
cargo run --example stroke_to_png --features tile-mem
cargo run --example myb_to_png --features "tile-mem myb-json" -- \
hokusai/examples/fixtures/calligraphy.myb out.png| Feature | Default | What it enables |
|---|---|---|
myb-json |
✅ | .myb JSON parser / serializer |
tile-mem |
✅ | Reference HashMap-backed TiledSurface |
tiny-skia |
— | tiny-skia Pixmap flattening helpers |
(hokusai-wasm ships as its own cdylib crate rather than an umbrella feature — point your wasm-pack at crates/hokusai-wasm directly.)
188 / 196 stock brushes (≈ 96 %) match libmypaint at MAD ≤ 0.5 under the brush-pack parity harness; the remaining 8 are inside MAD ≤ 4 and none are red. Measured against libmypaint v1.6.1 + the upstream mypaint-brushes pack via cargo xtask brush-pack-report (see CONTRIBUTING.md for the setup). Remaining gaps are tracked in TODO.
🖌️ Brush data
- All ~50 libmypaint settings as a strongly-typed enum with canonical string keys
- All inputs (
pressure,speed1/2,random,stroke,direction,tilt,custom,gridmap_*,attack_angle,viewzoom,barrel_rotation,brush_radius,tilt_declinationx/y, …) .mybv3 JSON parse / serialize, round-trip safe (unknown top-level settings preserved verbatim)
✏️ Stroke engine (libmypaint-faithful port of update_states_and_setting_values)
- Per-dab setting evaluation (
base_value + Σ curve(input)) with per-dab interpolation of pressure / speed across each segment slow_tracking+slow_tracking_per_dabcursor lag, withcount_dabs_tore-counted after every dab- Speed low-pass (
speed1_slowness/speed2_slowness) andspeed1_gamma/speed2_gammalog mapping direction_filterlow-pass on stroke direction (with the 180°-folded variant for 1D direction curves)tracking_noisegaussian jitter (radius-scaled, distance-coalesced viaskip_distance)offset_by_random/offset_by_speedjitter; fulldirectional_offsetsport (offset_x/y,offset_angle*,offset_multiplier,STATE.FLIPmirroring)radius_by_randomper-dab radius jitter with libmypaint's(orig / new)²opacity correctionopaque_linearizeper-dab overlap compensation- Tilt inputs (
tilt,tilt_declination,tilt_ascension) with the libmypaint 90° declination default and ramp-from-zero seeding attack_angle(signed angular difference between pen ascension and stroke direction + 90°)Strokeinput withstroke_duration_logarithmic,stroke_holdtime,stroke_thresholdgating- Gridmap inputs (
gridmap_x,gridmap_y) sampled fromSTATE.ACTUAL_X/Yviagridmap_scale[_x/_y] - Custom input chain (
custom_input_slownesssmoothingSETTING(custom_input)intoINPUT(CUSTOM)) - Per-dab HSV / HSL drift (
change_color_h/_v/_hsv_s/_hsl_s/change_color_l) - Spectral pigment mix (
paint_mode) — 10-channel WGM viargb_to_spectral/spectral_to_rgbwith thespectral_blend_factorsigmoid - Smudge bucket sampling + mixing with lazy
smudge_length_log-gated resample (PREV_COL_RECENTNESS),apply_smudge/eraser_target_alphasource-alpha bias, andsmudge_transparencyopacity-gated rejection - Fresh-stroke / long-pause detection
🎨 Pixel blending (draw_dab)
- Normal + Eraser blend in linear sRGB fix15 (premultiplied alpha)
- Spectral
paint_modeblend (BlendMode_Normal_and_Eraser_Paint) with low-alpha additive fade - Colorize blend (replace hue/sat, keep value)
- Two-segment hardness falloff with
anti_aliasingsub-pixel edge feathering (port ofcalculate_rr_antialiased) - Elliptical dabs (
aspect_ratio,angle) lock_alphamasking,posterizequantization as its own post-pass (sopaint_mode = 1brushes still posterize)- Spectral
get_color(Surface2::get_color_pigment) for smudge sampling whenpaint_mode > 0
🔬 Compatibility
- Knuth lagged-Fibonacci PRNG port of libmypaint's
rng-double.c(TAOCP 3.6-15, KK=10 LL=7 TT=7, seed 1000) with the samerand_gaussscaling - libmypaint-sourced PNG goldens via a small C wrapper around
mypaint_brush_stroke_to_2(cargo xtask regenerate-goldens) - Brush-pack parity tool (
cargo xtask brush-pack-report) — drives all 196 stock brushes through a fixed pressure-ramp curve. Current state: 188 / 196 stock brushes pass MAD ≤ 0.50, 8 amber (≤ 5.0), 0 red - Per-step input tracing (
HOKUSAI_TRACE_INPUTS=1) prints libmypaint'sprint_inputs-format lines from both engines — on the trickiest scatter brushes the streams match line-for-line - Per-dab tracing (
HOKUSAI_TRACE_DABS=1) prints identical-format dab lines from both engines forpaste-diff debugging HOKUSAI_UPDATE_GOLDENS=1snapshot harness for in-tree regression
🧱 Infrastructure / backends
- Tile-aware traversal across arbitrary canvas extents (64×64 RGBA fix15, libmypaint-identical)
hokusai-tiny-skia— flatten anyTiledSurfaceinto atiny_skia::Pixmaphokusai-wasm—wasm-bindgenJS bindings + browser demo- CI: fmt, clippy
-Dwarnings, test on Linux/macOS/Windows, wasm32 build check, MSRV 1.88
The brush-pack-report (cargo xtask brush-pack-report) is the source of truth
for what's left. As of the latest run, 0 brushes are red (MAD > 5) and 8 are
amber (worst: texture-06 at 3.8). The remaining ambers are residual,
not structural — dab positions, inputs and colours track the reference
bit-for-bit over long prefixes; the gap comes from sub-display-precision
drift that occasionally flips a dab-count threshold, after which the RNG
streams shift and the strokes scatter differently. Known contributors:
- Cross-implementation last-ulp noise — tiny disagreements in float
evaluation order or libm last-ulp results feed brushes whose user
curves are steep (e.g.
elliptical_dab_angle(random)swinging ±180°,exp(offset_multiplier)-scaled offsets in theTail_Feathersfamily); pinning these down needs instrumented libmypaint builds, not more porting. - Smudge feedback loops —
Round#1/WateryFlatbrushresample the canvas into the smudge bucket every few dabs, so any pixel-level residue compounds. - Sparse
get_colorsampling parity is macOS/BSD-specific — radii above 2 px subsample with libcrand(), ported as the BSD Park–Miller generator; against a glibc-built libmypaint the sampled subset differs.
See CONTRIBUTING.md for build / test / snapshot workflow, fixture conventions, and commit-message style.
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Vendored brush fixtures under hokusai/examples/fixtures/ are unmodified copies from mypaint-brushes (CC0 1.0).