Signature Pad and Calligraphy Grids: uView Pro X’s Cross-Platform Canvas Component
1. Start with a Basic Signature Board
When using uni-app x for mobile apps, handwritten signatures in forms are very common. It isn't hard to implement, but multi-platform compatibility is troublesome: touch coordinates must align with canvas pixels, different devices have different pixel ratios; undoing requires maintaining state; export also has to consider format and background color. Each platform may handle these differently.
And these needs appear frequently in business—sign-off, agreements, acceptance, approval—almost every form project hits them.
uView Pro X 2.0.15 adds ux-signature to bundle all this into one component: a canvas 2D signature board, plus optional writing grids—field grid (田字格), cross grid (米字格), nine-grid (九宫格)—and even tracing text. Handwriting, undo, clear, and export are all built-in, working on App, H5, and WeChat mini-programs with a single codebase.
2. Minimal Usable Version
The component uses the ux- prefix, is auto-registered via easycom, and doesn't require an import in the page. The minimal usable version is just one tag:
<template>
<ux-signature @submit="onSubmit" />
<ux-modal v-model="showPreview" title="Signature Image" :show-cancel-button="true">
<image :src="preview" mode="widthFix" />
</ux-modal>
</template>
<script setup lang="ts">
const showPreview = ref(false);
const preview = ref<string>('');
function onSubmit(image: string): void {
if (image == '') return;
preview.value = image;
showPreview.value = true;
}
</script>
When you tap "OK", the component exports the canvas as a base64 dataURL and sends it back via submit. The advantage of base64 is that you don't need to worry about file paths or upload timing; local preview and upload interfaces both use the same string. The cost is that the size is about one-third larger than the original image; single-line long signatures can also be exported as jpeg before uploading.
If you don't want to use the built-in confirm button, you can give the component a ref and call done() when you need to fetch the image:
type SignatureExpose = { clear: () => void; revoke: () => void; done: () => string };
const signatureRef = ref<SignatureExpose | null>(null);
const image: string = signatureRef.value?.done() ?? '';
3. Signature Board: Adjust the Stroke to Your Liking
The pen has just two properties: line-color and line-width. Each stroke records the color and thickness at the time of drawing. Changing the color mid-write only affects subsequent strokes; previously written characters don't change, which is handy for multi-color signing. If line-color isn't provided, it follows the theme's main text color, so even with a light export background it won't be hard to see in dark mode.
The tip layer is a translucent watermark by default: use tips for the text and tips-size for the font size. For icons or multi-line instructions, use the tips slot—it overlays the canvas but doesn't intercept touch, so you can still write. When no tips are needed, set show-tips="false" to turn them off.
Buttons are controlled by show-clear / show-revoke / show-confirm. Their text can be changed with clear-text / revoke-text / confirm-text to something like "Rewrite" / "Undo" / "Save". When all three buttons are hidden, ref still exposes clear(), revoke(), done(), and resize(). You decide whether the buttons sit at the bottom of the form or inside a modal.
<template>
<ux-signature
ref="signatureRef"
line-color="#1f2d3d"
:line-width="4"
:height="180"
:show-clear="false"
:show-confirm="false"
:show-revoke="false"
>
<template #tips>
<text class="sign-tips">Please sign with neat handwriting</text>
</template>
</ux-signature>
<view class="sign-actions">
<ux-button size="small" @click="signatureRef?.clear()">Rewrite</ux-button>
<ux-button size="small" type="primary" @click="onDone">Save</ux-button>
</view>
</template>
Dynamic scenarios also require attention to canvas size: switching from portrait to landscape, expanding a collapse panel, or a modal going from hidden to visible all invalidate old coordinates. Call resize() once to rebuild the canvas at the new size; strokes are preserved and redrawn.
After signing, the business logic needs the result. Three properties matter for export: export-format chooses png or jpeg; quality applies only to jpeg and clamps out-of-range values to 0–1; bg-color and export-bg-color are two different things—the former is only the container's on-screen background, while the latter is what gets written into the exported image.
Two otherwise easy-to-miss points: jpeg has no alpha channel—if you don't set an export background, transparent areas turn black; and on H5, saveImageToPhotosAlbum is unavailable, so saving to the album must fall back to downloading or showing a prompt.
<ux-signature
export-format="jpeg"
:quality="0.8"
export-bg-color="#ffffff"
line-color="#303133"
tips="White background · jpeg export"
@submit="onSubmit"
/>
4. Writing Grid: Signature Guide Lines and Calligraphy Practice
Adding grid lines to the signature board requires only a grid-type: mi for cross grid (米字格), tian for field grid (田字格), nine for nine-grid (九宫格). Without a value, there are no grid lines. The outer frame is a solid line, inner reference lines are dashed. The dashed lines are hand-drawn and don't rely on setLineDash, so they look identical across all three platforms.
<ux-signature grid-type="mi" guide-text="米" :height="160" />
<ux-signature grid-type="tian" guide-text="田" :height="160" />
<ux-signature grid-type="nine" guide-text="九" :height="160" />
After choosing the grid type, adjust the cell size: grid-size controls the cell edge length in px and clamps any out-of-range value to 24–120. The column count is calculated from the canvas width; the row count is the smaller of "rows needed for tracing" and "rows that fit the available height". The whole block is centered; to start from the top, set grid-align to top. Cell size and canvas height should be considered together: with a 24px cell, many cells fit in one row, good for long sentences; with 96px, only a few cells fit per row, better for single-character practice.
Tracing uses guide-text, which fills characters into cells one by one. The font size is 76% of the grid width. Text that exceeds the visible cell count is truncated automatically, so before writing a long sentence, make sure height is sufficient. The tracing character color is guide-color, and the grid line color is grid-color. Both default to the reference red; switching to blue or grey gives a pen/pencil copybook feel.
<!-- Calligraphy practice: cross grid + tracing, white background, dark strokes -->
<ux-signature
grid-type="mi"
guide-text="永"
:grid-size="64"
:height="200"
grid-align="top"
export-bg-color="#ffffff"
line-color="#303133"
@submit="onSubmit"
/>
On export, tracing characters don't appear—they're only on-screen hints. Grid lines are controlled by export-grid: default true, so the exported image matches the screen; set it to false, and the component clears the screen, lays down only the background and strokes, then captures the image, yielding a clean signature. After undo, clear, or size changes, grid lines are automatically re-laid. Grid line color and tracing characters don't switch with the theme; they remain the same reference colors in dark mode. When using a light export background, remember to switch the stroke to a dark color.
Two common combinations: for calligraphy practice, use cross grid with tracing, white background and dark strokes; for signature guidance, use field grid without tracing, and turn off grid lines on export.
5. Related API
Here are the high-frequency properties; see the official docs for the full list and defaults:
| Purpose | Property |
|---|---|
| Size & disabled | width, height, disabled |
| Pen | line-color, line-width |
| Tips | tips, tips-size, show-tips |
| Export | export-format, quality, export-bg-color |
| Writing grid | grid-type, grid-size, guide-text, export-grid |
| Buttons | show-clear, show-revoke, show-confirm |
Events: start (pen down), signing (while writing), end (pen up or touch cancel), submit (confirm button clicked, returns base64). start and end are suitable for showing writing state, like enabling a submit button only after pen-up. signing fires frequently; don't do heavy work in its callback.
Methods: clear() (clear), revoke() (undo), done() (fetch image), resize() (recalculate canvas). Slots: tips, for fully customizing the tip layer.
Undo implementation depends on the platform: on non-Harmony, it restores pixel snapshots, keeping the last 50 steps; on Harmony, it redraws strokes, and after clearing, revoke() won't restore cleared content. If you need undoable clearing, save an exported image at the business layer first.
6. Use Cases
Contract and agreement signing: embed a signature board in the form, set the export background to white, strokes to dark, and paste the image directly into a PDF or upload to storage.
Courier sign-off, on-site service confirmation: handwritten signature with one-time phone number confirmation, enable the undo button so a wrong stroke can be undone.
Signature guidance and multi-character writing: use field grid or nine-grid as a background pattern, choose whether to include the grid on export, long copy can be written neatly.
Calligraphy practice and homework: cross grid with tracing, grid-align="top" for multiple rows, export-grid enabled, the exported result is a gridded practice sheet.
7. How to Install
uView Pro X provides both a TypeScript version and a UTS version, published on the DCloud plugin marketplace. Choose based on your project's tech stack; don't mix them.
TypeScript version
- Plugin marketplace: https://ext.dcloud.net.cn/plugin?name=uview-pro-x
- Docs: https://x.uviewpro.cn
- Requires HBuilderX 5.31+,
ux-signaturesince 2.0.15
UTS version
- Plugin marketplace: https://ext.dcloud.net.cn/plugin?name=uview-pro-x-uts
- Docs: https://uts.uviewpro.cn
The component names and APIs are identical in both versions; install one according to your project language.