CommunityResearch & Data Analysisgithub.com

Canvas Grid Engine

A printable Chinese character practice sheet and exercise paper generator, supporting 13 grid types (including Mi-zi grid and Tian-zi grid), automatic character centering, and Pinyin annotation. Ideal for calligraphy practice, elementary Chinese homework, and early education word cards.

What is Canvas Grid Engine?

The Canvas Grid Engine is a pure front-end Canvas 2D module designed to generate precise educational grids (such as Mi-zi, Tian-zi, Hui-gong grids) for printable Chinese character practice sheets. It requires zero external dependencies and works seamlessly in both browsers and WeChat Mini Programs. The engine provides a set of ready-to-use functions: `computeLayout` for automatic grid layout, `gridSegments` for rendering 13 different grid element types, `drawGrid` and `drawCharInCell` for drawing grids and centered characters with optional Pinyin annotations. It handles common pitfalls like text blurriness or misalignment through built-in best practices (e.g., using integer coordinates, proper `textBaseline` settings, and respecting the fixed 1240×1754 A4 internal resolution). The package includes a minimal demo HTML file (`minimal-grid.html`) and a Node.js self-test script (`selftest.js`) to verify layout math and segment correctness before production use. This skill is part of a larger Canvas toolchain, complementing SVG replication, multi-page PDF export, and Mini Program validation workflows.

Works with~Claude Code~Codex CLI~Cursor
npx skills add shuangying0001-beep/canvas-grid-engine

Installed? Explore more Research & Data Analysis skills: affaan-m/uspto-database, affaan-m/scholar-evaluation, affaan-m/literature-review · View all 6 →

Ask in your favorite AI

Open a new chat with this agent skill pre-loaded.

Documentation

Canvas Grid Engine for Learning Sheets

Teaching aid · Tool module. Pure front-end Canvas 2D, zero dependencies, compatible with browsers and WeChat Mini Programs.

What It Is (For Humans)

When creating character practice sheets, exercise papers, or printable learning sheets, the most tedious tasks are drawing grids (Mi-zi, Tian-zi, Hui-gong, etc.) precisely, centering Chinese characters correctly, and automatically adding Pinyin annotations. This skill provides a ready-to-use grid engine: import references/grid-engine.js, call a few functions, and you can render any grid type with centered characters, then directly export as PNG or pass to PDF. Essential for teachers, educational aids, and Chinese culture institutions producing print materials.

  • Includes minimal-grid.html: open in a browser to see the effect (Mi-zi grid + 12 characters).
  • Includes selftest.js: run via Node to verify layout, segments, and typography math — proof the algorithm is reliable.

When to Use

  • "Generate a Mi-zi grid character sheet" / "Create Tian-zi grid practice paper" / "Draw 13 grid types"
  • Center text within grid cells and auto-annotate Pinyin in a Mini Program
  • Need A4 1240×1754 standard resolution output

Coordinate System and Tokens (AI Must Read)

  • Fixed A4 internal resolution: 1240×1754 (150dpi), corresponding to SVG source viewBox 21000×29700. Conversion factor SVG→px ≈ 0.0590476.
  • Use canvas.style.width/height for display scaling, never change canvas.width/height (otherwise exported images become blurry).
  • Design tokens TOKENS: green #07C160, black #2C2C2C, red #CC1414, blue #2B5FD9, gridLine #BBBBBB, innerDash #CCCCCC, pinyin #999999.
  • Internal guide lines are always dashed setLineDash([6,4]), line width 1, color #CCCCCC; outer frame uses solid lines.

Core Function Signatures (AI Direct Call)

// Layout: returns { startX, startY, cells:[{row,col,x,y,size}] }, automatically horizontally centered
GridEngine.computeLayout({ gridSize, cols, rows, margin, pageW?, pageH?, startY? })

// Single-cell grid primitives (local coordinates): supports mizi/tianzi/huigong/jiugong/fangge/
//   hengxian/sixian/wuge/mizihuigong/tianzisidian/dianzhen/huangjinyuan
GridEngine.gridSegments(type, size)  // -> [{t:'line'|'dot'|'circle', ...}]

// Draw a single-cell grid (requires 2D ctx)
GridEngine.drawGrid(ctx, type, cell, { color?, lineWidth? })

// Draw a character centered in the cell (automatically scaled by width, never overflows); can include Pinyin (top quarter of cell)
GridEngine.drawCharInCell(ctx, char, cell, { font?, color?, pinyin? })

// Font size auto-fit: no more than 0.85 of cell's short side
GridEngine.fitFontSize(char, cellSize, ctx, baseFont)  // -> px

// Arrange characters sequentially: skip spaces, wrap at column end (starts at top-left cell)
GridEngine.layoutCharacters(text, { startX, startY, gridSize, cols, rows })

Drawing Order (Do Not Change)

1. ctx.fillStyle=background → fillRect covering entire canvas
2. drawGrid (grid lines)            ← grid is the bottom layer
3. drawCharInCell (characters)      ← characters are the top layer
4. Export image (toDataURL / wx.canvasToTempFilePath)

Common Pitfalls (Auto Avoid)

PitfallCorrect Approach
Blurry textFirst set ctx.font='48px serif', then fillText; Mini Program: canvas.width = cssW*dpr
Text too highManually set ctx.textBaseline='middle'
Lines not straightUse even lineWidth, integer coordinates
Blank exportAssign canvas.width/height before drawing
Border overflowUse box-sizing:border-box; grid coordinates in px, not rpx

Self-Test

node references/selftest.js   # Verify layout, segments, typography math. Only deliver if PASS

Reference Files

  • references/grid-engine.js — Engine core (UMD, works in browser/Node)
  • references/minimal-grid.html — Runnable demo (Mi-zi grid + 12 characters)
  • references/selftest.js — Node self-test

Notes

  • The engine is pure functions (no DOM dependency); drawGrid/drawCharInCell require a 2D context; Node self-tests only verify math, no drawing.
  • Falls back to system serif font if missing — does not affect layout; for production, replace with wx.loadFontFace / @font-face.
  • Complements canvas-multipage-pdf and svg-to-canvas-replica: this skill handles "drawing", the other two handle "export/replication".

Teaching Tool Canvas Toolkit (Combo Use)

This skill is part of the "Teaching Tool · Canvas Toolkit" — four components covering the complete loop: Draw → Replicate → Export → Verify:

  • Canvas Grid Engine for Learning Sheets (Draw): 13 educational grids + centered characters + Pinyin annotation, generates Mi-zi/Tian-zi character sheets.
  • SVG to Canvas Replication Engine (Replicate): Zero-error replica of existing paper template SVGs into editable, printable Canvas.
  • Canvas Multi-Page PDF Export (Export): One-click synthesis of multiple Canvas pages into standard A4 PDF for direct printing.
  • Mini Program Canvas Verification Workflow (Verify): Local preview + screenshot visual regression to ensure no misaligned grids or skewed characters.

For the complete solution and demos, see the "Teaching Tool Canvas Toolkit Landing Page" (link to be added upon release).

Related Skills