跪拜 Guibai
← All articles
React.js · Electron · Open Source

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

By niyongsheng ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

The project demonstrates how to ship a single React codebase as both a static web app and an Electron desktop app, and it records the practical gotchas of exporting pixel-perfect A4 pages, including html2canvas's SVG tiling bug and the decision to embed Word pages as full-page images for guaranteed layout fidelity. It also shows a minimal infrastructure approach: no backend, minimal analytics, and trial licensing with offline activation.

Summary

The tool covers 24 worksheet categories including name stroke order, Chinese characters, phrases, pinyin, poems, math drills, clock practice, and more, plus 45 ready-made templates organized by textbook sync, structural patterns, radicals, and other categories. Everything is generated locally: this is a static web app in the browser and an Electron shell on desktop, with a single React renderer driving preview, worksheet compilation, and export. That sharing avoids the classic 'preview looks different from the printout' problem.

Behind the scenes, A4 pages are laid out at 794x1123 CSS pixels (96dpi), with eight grid styles defined as 0-100 normalized geometry, rendered as SVG data URLs and tiled as backgrounds. Export captures each page at 2x resolution via html2canvas, then assembles PDFs with jsPDF or packs PNGs into Word documents as full-page images. A documented html2canvas bug makes SVG tiling unreliable, so the code temporarily replaces grid backgrounds with rasterized PNGs before capture. Stroke-order data comes from hanzi-writer-data with a three-CDN fallback, and pinyin-pro supplies tone-marked pinyin.

Deliberate trade-offs keep the app lean: no backend (so no cloud sync), Word exports are images rather than editable text (guaranteeing pixel-perfect layout), and only system fonts are used. Desktop builds include a 30-trials/14-day trial with offline activation based on HMAC-SHA256 and Crockford32 codes. Analytics are minimal: only print and export events are reported, with no worksheet content, names, or machine fingerprints. The codebase sits around 20k lines, 12k of them TypeScript, and is MIT-licensed.

Takeaways
— 24 worksheet types and 45 built-in templates are available, covering name stroke order, characters, phrases, poems, math drills, clocks, and more.
— A4 pages are laid out at 794 x 1123 CSS pixels at 96dpi, with all internal dimensions in CSS pixels and printing via @page rules.
— Eight grid styles are defined with normalized 0-100 coordinates and rendered as SVG data URLs, with the same geometry driving preview, icons, and print.
— Export uses html2canvas at 2x resolution, then jsPDF for PDF and docx for Word.
— Word exports embed full-page PNGs to guarantee cross-platform layout consistency, sacrificing editability.
— Stroke-order data comes from hanzi-writer-data with CDN fallbacks: jsDelivr, Fastly, and unpkg.
— Zustand persistence debounces writes to localStorage with a 300ms delay to keep slider interactions smooth.
— Electron builds ship for macOS, Windows, and Linux via electron-builder, with auto-update via electron-updater and GitHub Releases.
— Offline activation uses a machine fingerprint, HMAC-SHA256, and Crockford32 codes with separate message domains for trial and activation.
— Only print and export events are tracked, with no worksheet content, names, machine fingerprints, or activation codes in event attributes.
— The project is MIT-licensed, with about 20k lines of code, 12k of them TypeScript, and no backend.
Conclusions

A single React rendering layer powering preview, compilation, and export is a sound architectural choice that prevents drift between what users see and what gets printed.

Embedding full-page images in Word is a pragmatic trade-off for a print-oriented tool: visual fidelity matters more than text editability, and it eliminates layout-engine differences across platforms.

The 300ms debounced persistence is a classic performance trick that keeps the UI responsive when dragging sliders, since serializing the entire state tree on every change would block the main thread.

The decision to skip a backend entirely keeps privacy high and deployment cost at zero, but it also eliminates any possibility of cloud sync or collaboration, a trade-off the author explicitly accepts.

Concepts & terms
SheetModel
A discriminated union of TypeScript types representing different worksheet configurations (grid, name stroke, four-line, poem, etc.), used to normalize how each worksheet is built and rendered.
gridGeometry
A function that returns normalized 0-100 coordinate definitions for eight grid styles (Tian, Mi, Huigong, Jiugong, etc.), used to draw both the tiled background and the small icons in the settings panel.
Crockford32
A base32 encoding variant that eliminates ambiguous characters; used here to format offline activation codes alongside HMAC-SHA256 signatures.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗