跪拜 Guibai
← Back to the summary

CopyCraft Spawns A4 Calligraphy Worksheets Locally Across Web and Desktop with One React Codebase

CopyCraft 字帖工坊: Building a Cross-Platform Local Calligraphy Worksheet Generator with React + Electron

An open-source project born out of a parent's desire to help a child practice writing: 24 worksheet types and tools, 45 built-in templates, A4-level layout, PDF/Word export, and one shared React rendering layer for the web and three desktop platforms.

Origin: A Project Forced into Existence by a Calligraphy Worksheet

My child started first grade, and every day's homework included "calligraphy practice": new characters need pinyin, need a grid, and ideally a stroke-order demonstration; on weekends we wanted to practice a classical poem, so we had to find our own worksheet templates; and the practice books issued by the school didn't always match our child's specific needs.

I tried several online worksheet generators, and the problems were roughly these:

So I decided to write my own: CopyCraft 字帖工坊. It's now open source (MIT), supports Web, macOS, Windows, and Linux, and all worksheets are generated locally. The project is roughly 20,000 lines of code, of which 12,000 are TypeScript.

Online experience: https://copycraft.looplooptech.com/

First Look

The main interface is a three-column layout: "choose worksheet on the left, adjust parameters in the middle, real-time A4 page preview on the right." Changing a parameter immediately updates the preview:

CopyCraft name worksheet interface

Names can be demonstrated stroke by stroke, with options for "no duplicate generation" and "one character per line," and pinyin is automatically annotated.

The built-in 45 templates are organized by textbook sync, structural patterns, radical parts, new-character word formation, classical poems, and practice-book categories, and support ?preset= share links (the link only contains the template ID, not any child's content):

template library

The classical poem template supports pinyin, vertical layout, and annotations, generating a printable A4 directly:

classical poem worksheet

Math, English, pinyin, and pen-control exercises all live in the same editor, such as vertical arithmetic:

screencapture-copycraft-looplooptech-2026-11-27-09_45_12.png

screencapture-copycraft-looplooptech-2026-11-27-09_44_59.png

vertical arithmetic worksheet

screencapture-copycraft-looplooptech-2026-11-27-09_36_05.png

Export goes directly to PDF and Word, usable for print shops, class groups, or home printers:

screencapture-copycraft-looplooptech-2026-11-27-09_42_22.png

Feature Summary

Tech Stack and Overall Architecture

The entire application has no backend. The web version is a pure static site; the desktop version wraps the same rendering layer inside Electron, with the main process handling only windows, menus, auto-updates, and authorization.

1. One Model, Reused in Three Places

Worksheet types and layout parameters are first normalized into SheetSettings, then buildSheetModel() generates a SheetModel with pagination information. Different worksheets correspond to different models, such as grid-based, four-line-three-grid, and vertical arithmetic:

type SheetModel =
  | GridModel
  | NameStrokeModel
  | FourLineModel
  | PenModel
  | MathModel
  | VerticalModel
  | AbacusModel
  | ExpressionModel
  | PoemModel
  | ClockModel;

SheetView dispatches to specific components based on model.kind. Single-sheet preview, practice-book composition, and export capture all share the same set of React rendering components, so the problem of "preview shows one thing, export produces another" does not arise.

2. A4 Layout: Using the Browser as a Layout Engine

The preview page renders at A4 pixel dimensions at 96dpi:

export const PAGE_WIDTH = 794;   // A4 @ 96dpi
export const PAGE_HEIGHT = 1123;

All internal dimensions are calculated in CSS pixels, then the whole thing is scaled to fit the window during display; for printing, the A4 millimeter dimensions and the @page rule are used. This way screen preview, browser printing, and Electron printing all follow the same layout.

3. Grids: A Set of Normalized Geometry, No Drift Between Preview and Export

The auxiliary lines for the eight grid styles (田字格, 米字格, 回宫格, 九宫格, 田回格, 方格, 四线三格, 横线格) are not hardcoded CSS; they are first defined as normalized geometry from 0 to 100:

export function gridGeometry(style: GridStyleId, y0 = 0): GridGeometry {
  // 田字格: horizontal and vertical center lines
  // 米字格: add two diagonals
  // 回宫格 / 田回格: add an inner rectangle
  // 四线三格 / 横线格: convert to horizontal rule lines
}

The same gridGeometry() function drives two things at once: it renders an SVG data URL as a tiled background, and it generates the small grid icons in the settings panel. A single source of truth guarantees that the icons, the preview, and the printed result all match.

4. Export: DOM → Bitmap → PDF / Word

The export flow is straightforward:

  1. Use html2canvas to capture each A4 page at 2x resolution as a PNG.
  2. For PDF: use jsPDF to create an A4 document, one PNG per page, with compression enabled.
  3. For Word: use docx to create an A4 document with zero margins, embedding a high-resolution 790×1117 image per page and inserting page breaks.

Here is a real pitfall: html2canvas does not reliably tile SVG data-URL backgrounds. Direct screenshots can cause grid lines to be misplaced or lost. The solution is to rasterize each unique grid tile into a PNG of the same CSS size before capture, temporarily replace the background, and restore it after capture:

const png = await tileCache.get(cacheKey);
restores.push({ element: row, backgroundImage: row.style.backgroundImage /* ... */ });
row.style.backgroundImage = `url("${png}")`;
// ... capture ...
restores.forEach((r) => { r.element.style.backgroundImage = r.backgroundImage; });

The Word export does not perform native layout; it embeds full-page images instead. This is a deliberate trade-off: sacrifice text editability for completely consistent cross-platform layout. For a "what you see is what you get" scenario like worksheets, layout consistency matters more than editability.

5. Stroke Order and Pinyin

Stroke-order data comes from hanzi-writer-data, fetched per character as JSON (strokes stroke paths + medians midlines), cached in memory, with three CDN sources (jsDelivr, Fastly, unpkg) configured as fallbacks. Only the first N strokes are drawn during rendering, and the last stroke is highlighted:

const strokes = data.strokes.slice(0, Math.max(0, stage));
return (
  <svg viewBox="0 0 1024 1024">
    <g transform="translate(0 900) scale(1 -1)">
      {strokes.map((path, index) => (
        <path d={path} fill={index === strokes.length - 1 ? highlight : color} />
      ))}
    </g>
  </svg>
);

Pinyin uses pinyin-pro, which returns tone-marked results per character, with a Map cache; non-Chinese characters return an empty string and are left for the layout layer to handle punctuation and English.

6. A Small Optimization for State Persistence

All worksheet content, layout, and titles are stored in Zustand and persisted via the persist middleware to localStorage. The issue is that when the user drags the font-size slider, if every change serializes the entire state tree, the main thread gets clogged with repeated writes to disk.

So the storage layer uses debounced writes of 300ms: state updates only mark "pending write," and the timer triggers the actual JSON.stringify + localStorage.setItem when it fires. When dragging the slider, the write frequency drops from once per frame to once per 300ms, and the interaction becomes noticeably smoother.

7. Electron: The Same UI, Three Installers

The desktop version does not write a separate interface; it simply puts the Vite build output into Electron. The main process is responsible for:

Packaging is done with electron-builder:

A Few Deliberate Trade-offs

  1. No backend. Generation, layout, and export all happen locally, which is privacy-friendly and has zero deployment cost; the cost is no cloud sync or collaboration.
  2. Word uses full-page images. This guarantees 100% layout consistency, but you cannot edit text directly in Word. If you need an editable Word file, you can build on the source and switch to native table layout.
  3. Fonts rely on system fonts. Kai, Xingkai, and hand-written styles prioritize system fonts and fall back to serif fonts if unavailable. This keeps the installer from bundling several megabytes of fonts, but on Linux you may not get the ideal Kai font.
  4. Desktop trial + open source. The web version is free to use; the desktop installer includes a 30-times / 14-day trial, after which activation is needed to cover signing and distribution costs. The source is MIT-licensed, so you can build it yourself.
  5. Minimal analytics. The web version only reports two whitelisted events, print and export, and the event attributes never include worksheet content, names, machine fingerprints, or activation codes.

How to Use

Online (no installation): https://copycraft.looplooptech.com/

Download desktop: https://github.com/niyongsheng/CopyCraft/releases

Local development:

git clone https://github.com/niyongsheng/CopyCraft.git
cd CopyCraft
npm install
npm run dev          # Web: http://localhost:5178
npm run dev:desktop  # Electron + Vite HMR
npm run dist:mac     # also dist:win / dist:linux

Closing

CopyCraft began as a small tool "to make a worksheet for my own child," and features kept accumulating: stroke order, pinyin, classical poems, math, practice books, five languages, and three desktop platforms. It is surely not perfect: Linux fonts, Word editability, and more textbook versions are all areas that could improve.

The project is open source under the MIT license, and the repository is here:

https://github.com/niyongsheng/CopyCraft

If it happens to help you, or if you also have a child who is practicing writing, you are welcome to try it out, file issues, submit PRs, and give it a Star. If this project is useful to you, that is the biggest motivation for it to keep iterating.

Originally published on Juejin. Please indicate the source if you repost.

Comments

Top 1 from juejin.cn, machine-translated. The original thread is authoritative.

hzzou

It does work, but blogger, I'll wait until I have kids, haha. But why aren't you monetizing this? There's so much potential value here.