Community研究与数据分析github.com

shuangying0001-beep/canvas-grid-engine

字帖/作业纸生成器(米字格田字格13种):一键生成可打印中文字帖与练习纸,支持汉字居中、拼音标注。适合书法练字、小学语文作业、早教字卡。

canvas-grid-engine 是什么?

canvas-grid-engine is a Claude Code agent skill that 字帖/作业纸生成器(米字格田字格13种):一键生成可打印中文字帖与练习纸,支持汉字居中、拼音标注。适合书法练字、小学语文作业、早教字卡。.

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add shuangying0001-beep/canvas-grid-engine

Installed? Explore more 研究与数据分析 skills: affaan-m/uspto-database, affaan-m/scholar-evaluation, affaan-m/literature-review · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

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

相关技能