English | 简体中文
A library that deconstructs Chinese characters into radicals and reassembles them for intriguing visual effects.
cradic ≈ chinese radical
npm install cradic- Multi‑format output: HTML, SVG, Typst, Plain text
- Chainable API: Fluent interface for configuration
- Browser + Node.js: Works in both environments
import { from } from 'cradic';
// Generate html and save
from('一个简单的汉字结构拆解测试').to('html').saveAs('output.html');
// Generate text and output to console
from('一个简单的汉字结构拆解测试').to('text').log();
// -> '一人丨⺮间单白勺氵又宀子纟吉木勾扌斥解氵则讠式'You can also directly try the Live Demo .
Create a Cradic instance with input string.
const cr = from('汉字');Set output format. Default is html.
html– HTML document with CSS (recommended)svg– SVG grid layouttyp– Typst documenttext– Plain text with radical replacementspng/jpg/pdf– Binary files (Node.js only, requires external tools)
from('汉字').to('svg')Override default parameters. Parameters are merged with the defaults of the chosen output type (set by .to()).
mode('b'|'h'|'v') – Controls split directions (works for all types). Default'b'.sep– Separator for text output (only works with.to('text')). Use'auto'for smart separator: two newlines before vertically-split characters, space before others.
from('汉字').to('text').with({ mode: 'h' }).log(); // '氵又字'
from('汉字').to('text').with({ sep: ' ' }).log(); // '氵又 宀子'For a full list of type-specific parameters (cell size, colors, fonts, etc.), see docs/params.md.
Print output to console.
from('汉字').to('html').log();Save to file.
- Node.js: Write to local file
- Browser: Trigger download
await from('汉字').to('html').saveAs('output.html');Get output as string. This method returns a Promise<string>, so please use await.
const html = await from('汉字').to('html').toStr();<script type="module">
import { from } from './cradic.browser.js';
const html = await from('汉字').to('html').toStr();
document.body.innerHTML = html;
</script>import { from } from 'cradic';
await from('汉字').to('html').saveAs('output.html');You can also check tests/demos.
Requires external tools:
rsvg-convertfor PNG/JPGtypstfor PDF
await from('汉字').to('png').saveAs('output.png');
await from('汉字').to('pdf').saveAs('output.pdf');npm run build:node # Build for nodejs
npm run build:browser # Build for browserThe source code of this project is released under the MIT License.
The data in assets/h.json and assets/v.json is partly sourced from (https://github.com/kfcd/chaizi) and is licensed under CC BY 3.0.
