跪拜 Guibai
← All articles
Frontend · Vue.js · Uni-app

Signature Pad and Calligraphy Grids: uView Pro X’s Cross-Platform Canvas Component

By 前端梦工厂 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Cross-platform signature capture usually forces developers to write per-platform canvas logic for pixel alignment, undo stacks, and export quirks. ux-signature collapses that into one tag, so teams building for app, web, mini-program, and Harmony targets skip a whole class of canvas and coordinate-mapping bugs.

Summary

Handwritten signature capture in uni-app x usually means wrestling with touch coordinate alignment, pixel ratios, undo state, and export formats on each target platform. uView Pro X 2.0.15 bundles that into ux-signature: one canvas 2D component with built-in drawing, undo, clear, and export, supporting App, H5, WeChat mini-programs, and HarmonyOS from a single codebase.

The component exports the canvas as a base64 dataURL via a submit event, with PNG/JPEG format options, a quality clamp for JPEG, and separate on-screen vs. export background colors. Below the surface, it also offers a writing grid system—field grid, cross grid, nine-grid—with hand-drawn dashed lines that render identically across platforms, and guide-text for tracing characters. Events like start, signing, end, and submit, plus methods like done(), revoke(), and resize(), give developers control without touching platform-specific canvas code.

Takeaways
— ux-signature is added in uView Pro X 2.0.15 and requires HBuilderX 5.31+.
— The component exports the canvas as a base64 dataURL through the submit event; base64 is about one-third larger than the original image.
— line-color and line-width apply per stroke; changing them mid-signing only affects subsequent strokes.
— grid-type supports mi (cross grid), tian (field grid), and nine (nine-grid); dashed guide lines are hand-drawn rather than using setLineDash for consistent multi-platform rendering.
— guide-text provides tracing characters scaled to 76% of grid cell width, truncated when they exceed visible cells.
— grid-size clamps to 24–120 px; row count is the smaller of required tracing rows and available height; grid-align="top" stacks from the top.
— export-format can be png or jpeg; quality only applies to jpeg and clamps to 0–1.
— jpg has no alpha channel; without an export background, transparent areas turn black.
— H5 cannot use saveImageToPhotosAlbum; saving to album requires a download or prompt fallback.
— Undo on non-Harmony platforms restores pixel snapshots with a 50-step history; on Harmony it redraws strokes, and revoke() after clear does not recover cleared content.
— The tips slot overlays the canvas without intercepting touch events.
Conclusions

The undo implementation splits by platform—pixel snapshots on non-Harmony, stroke redraw on Harmony—so the component isn't just a wrapper; it has to branch on the runtime's canvas capabilities.

Hand-drawing dashed grid lines instead of relying on setLineDash points to how much uni-app's canvas APIs deviate across targets; even a simple line style needs manual work to look identical everywhere.

Separating on-screen background from export background, and keeping tracing guides out of exports unless export-grid is on, allows one component to serve both interactive writing and clean document output without extra image processing.

Concepts & terms
easycom
uni-app's automatic component registration mechanism. Any component with a ux- prefix in the components directory is auto-imported into pages, so you don't need a manual import statement.
tagới
Chinese calligraphy practice grids: 田字格 (field grid) is a square divided into four quadrants, 米字格 (cross grid) adds diagonal lines, and 九宫格 (nine-grid) is a 3x3 grid. They guide stroke placement for handwriting practice.
描红 (tracing/copybook)
A Chinese writing exercise where pre-printed characters are shown in a light color and the learner traces over them with a pen. In ux-signature, guide-text provides these characters for tracing, but they don't appear in exported images.
base64 dataURL
A data URI encoding an image's binary data as a base64 string. It can be embedded directly in HTML or passed to APIs, avoiding file path and upload timing issues, at the cost of about a 33% size increase.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗