SVG to Canvas Precision Replica Pipeline
Teaching Tool · Utility module. Pure frontend Canvas 2D, zero dependencies.
What This Is (For Humans)
Do you have a premade paper template SVG (e.g., exercise sheets, calligraphy paper, tables) and want to turn it into a canvas that you can freely edit text, zoom, and export as PDF inside a mini-program or web page? This skill moves every coordinate, line, text, and image from the SVG onto a Canvas with zero error — not a screenshot, but a vectorized, re-editable replica. School/institution template variants can be toggled with one click. Works for replicating any paper template.
- Includes
selftest.js: Uses a stubctxto verify coordinate conversion is correct. Only trust it if it reports PASS.
When to Use
- "Replicate this SVG template onto Canvas" / "Draw this SVG onto a canvas"
- Need pixel-perfect restoration of a paper template (coordinates, line styles, font sizes consistent)
- Template has school version and institution version styles to switch between
Coordinate System Conversion (AI Must Read)
| Coordinate System | Dimensions | Relationship |
|---|---|---|
| SVG viewBox | 21000 × 29700 | 1 unit = 0.01mm |
| Physical | 210 × 297 mm | A4 |
| Canvas px | 1240 × 1754 | 150dpi |
var SVG_TO_PX = 0.0590476; // SVG unit → Canvas px (use this for daily work)
// Keep element coordinates in SVG units during parsing; multiply by SVG_TO_PX at render time
Data Extraction Process
- Read SVG text → parse
<style>dynamically for classes →{stroke,strokeWidth,dashArray}/fill/{fontSize,fontFamily,fontWeight} - Parse
<rect>,<line>,<polygon>,<text>,<image>— keep coordinates in SVG units (do not convert yet) - School/Institution toggle: different row counts (
rows/rowsInst), core drawing logic shared - At render time,
renderElements(ctx, elements)multiplies each coordinate bySVG_TO_PX
Core Function Signatures (For AI to Call Directly)
// Parse SVG text → { source, totalElements, elements:[{type,x,y,w,h,stroke,fill,classes,...}] }
SvgToElements.parseSVG(svgText, sourceName, imgBase)
// Replica render: automatically *SVG_TO_PX; for images, caller must provide drawImage load logic
Replica.renderElements(ctx, elements, { scale?, drawImage? })
// Page number detection: class contains 'footer-text' and text is purely numeric → returns that element (for drawAll to substitute with page number)
Replica.detectPageNo(elements) // -> element | null
// Template configuration JSON structure (based on SVG measurements)
// { name, grid:{ cellW, cellH, cols, rows, rowsInst, startX, startY },
// features:{ errorRow, pinyin, correctionCol },
// branding:{ logo:{x,y,w,h}, qr:{x,y,w,h}, slogan } }
Anti-Patterns (Avoid)
- ❌ Manually calculating coordinates (error-prone): Always multiply by
SVG_TO_PX; zero manual deduction. - ❌ Losing class styles after replica:
<style>must be dynamically parsed; hardcode no colors. - ❌ Drawing institution version with school version row count: Before rendering, select
rowsInstbased onisInstitution. - ❌ Directly dumping images: For
<image>, the caller must load the image fromdefaultSrcbefore usingdrawImage.
Self-Test
node references/selftest.js # Verifies parsing/coordinate conversion/page detection/replica coordinates; only deliver if PASS
Reference Files
references/svg-to-elements.js— SVG parser module (UMD)references/replica.js— Canvas replica render + page detection (UMD)references/selftest.js— Node self-test (stub ctx verifies coordinate conversion)
Notes
- Element coordinates are in SVG units; render scales uniformly; Canvas internal size is fixed at 1240×1754.
- Complements
canvas-grid-engine(which "draws" new grids): This skill handles "replicating" existing templates.
Teaching Tool Canvas Toolchain (Use in Combination)
This skill is part of the Teaching Tool · Canvas Toolchain, with four components covering a complete loop of "Draw → Replicate → Export → Verify":
- Canvas Learning Paper Grid Engine (Draw): 13 educational grids + Chinese character centering + pinyin annotation, generates mi-zi-ge/tian-zi-ge character sheets.
- SVG to Canvas Replica Engine (Replicate): Reproduces existing paper template SVGs onto editable, printable Canvas with zero error.
- Canvas Multi-Page PDF Export (Export): Combines multiple Canvas pages into a single standard A4 PDF for direct printing.
- Mini-Program Canvas Verification Workflow (Verify): Local preview + screenshot visual regression, eliminates misaligned grids and skewed text.
See the full solution and demo at the "Teaching Tool Canvas Toolchain Landing Page" (link to be added upon publication).