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 factorSVG→px ≈ 0.0590476. - Use
canvas.style.width/heightfor display scaling, never changecanvas.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)
| Pitfall | Correct Approach |
|---|---|
| Blurry text | First set ctx.font='48px serif', then fillText; Mini Program: canvas.width = cssW*dpr |
| Text too high | Manually set ctx.textBaseline='middle' |
| Lines not straight | Use even lineWidth, integer coordinates |
| Blank export | Assign canvas.width/height before drawing |
| Border overflow | Use 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/drawCharInCellrequire a 2D context; Node self-tests only verify math, no drawing. - Falls back to system
seriffont if missing — does not affect layout; for production, replace withwx.loadFontFace/@font-face. - Complements
canvas-multipage-pdfandsvg-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).