Skip to content

ldlsn1/cradic

Repository files navigation

Cradic

English | 简体中文

A library that deconstructs Chinese characters into radicals and reassembles them for intriguing visual effects.

cradicchinese radical

Installation

npm install cradic

Features

  • Multi‑format output: HTML, SVG, Typst, Plain text
  • Chainable API: Fluent interface for configuration
  • Browser + Node.js: Works in both environments

Quick Start

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 .

HTML render example

API

from(string)

Create a Cradic instance with input string.

const cr = from('汉字');

.to(type)

Set output format. Default is html.

  • html – HTML document with CSS (recommended)
  • svg – SVG grid layout
  • typ – Typst document
  • text – Plain text with radical replacements
  • png / jpg / pdf – Binary files (Node.js only, requires external tools)
from('汉字').to('svg')

.with(params)

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.

.log()

Print output to console.

from('汉字').to('html').log();

.saveAs(filename)

Save to file.

  • Node.js: Write to local file
  • Browser: Trigger download
await from('汉字').to('html').saveAs('output.html');

.toStr()

Get output as string. This method returns a Promise<string>, so please use await.

const html = await from('汉字').to('html').toStr();

Browser Usage

<script type="module">
  import { from } from './cradic.browser.js';

  const html = await from('汉字').to('html').toStr();
  document.body.innerHTML = html;
</script>

Node.js Usage

import { from } from 'cradic';

await from('汉字').to('html').saveAs('output.html');

You can also check tests/demos.

Binary Files Generation (Node.js only)

Requires external tools:

  • rsvg-convert for PNG/JPG
  • typst for PDF
await from('汉字').to('png').saveAs('output.png');
await from('汉字').to('pdf').saveAs('output.pdf');

Build

npm run build:node  # Build for nodejs
npm run build:browser  # Build for browser

License

The 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.

About

A library that deconstructs Chinese characters into radicals and reassembles them for intriguing visual effects.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors