Signature Writing Grid
1:35Description
Overlay rice, field or nine-cell grids and tracing text on the signature pad, usable as signature guides or a handwriting worksheet. The grid is painted inside the canvas, so exports match the screen.
Compatibility
| Platform | Support |
|---|---|
| App | ✅ Supported |
| H5 | ✅ Supported |
| WeChat Mini Program | ✅ Supported |
Examples
Grid Type
grid-type accepts mi (rice), tian (field) or nine-cell lines: a solid outer border with dashed inner guides; omit grid-type to hide it.
View source
<template>
<ux-signature grid-type="mi" guide-text="米字格" :height="160" export-bg-color="#ffffff" line-color="#303133" :show-tips="false" @submit="onSubmit" />
<ux-gap :height="8" />
<ux-signature grid-type="tian" guide-text="田字格" :height="160" export-bg-color="#ffffff" line-color="#303133" :show-tips="false" @submit="onSubmit" />
<ux-gap :height="8" />
<ux-signature grid-type="nine" guide-text="九宫格" :height="160" export-bg-color="#ffffff" line-color="#303133" :show-tips="false" @submit="onSubmit" />
</template>
<script setup lang="uts">
// grid-type:mi 米字格 | tian 田字格 | nine 九宫格,空串关闭格线
// 格线绘制在画布内,导出图保留网格;描红只作屏幕提示,不写入导出图,笔迹压在描红之上
function onSubmit(image: string): void {
console.log('base64 长度', image.length);
}
</script>Grid Size
grid-size sets the cell edge in px (clamped to 24-120); columns and rows follow the canvas size, so larger cells leave fewer per row.
View source
<template>
<ux-signature grid-type="mi" :grid-size="32" :height="140" :show-tips="false" @submit="onSubmit" />
<ux-gap :height="8" />
<ux-signature grid-type="mi" :grid-size="64" :height="140" :show-tips="false" @submit="onSubmit" />
<ux-gap :height="8" />
<ux-signature grid-type="mi" :grid-size="96" :height="140" :show-tips="false" @submit="onSubmit" />
</template>
<script setup lang="uts">
// grid-size 单位 px,内部钳制在 24-120;列数行数按画布尺寸自动计算
// 格子越大每行格数越少,无描红时铺满可视区域
function onSubmit(image: string): void {
console.log('base64 长度', image.length);
}
</script>Grid and Tracing Colors
grid-color sets the grid line color and guide-color the tracing text; both default to reference red, while blue or gray gives a pen or pencil worksheet look.
View source
<template>
<ux-signature
grid-type="tian"
guide-text="水墨"
grid-color="rgba(64, 158, 255, 0.75)"
guide-color="rgba(64, 158, 255, 0.35)"
:height="140"
export-bg-color="#ffffff"
line-color="#1f2d3d"
:show-tips="false"
@submit="onSubmit"
/>
<ux-gap :height="8" />
<ux-signature
grid-type="mi"
guide-text="听雨"
grid-color="#c0c4cc"
guide-color="rgba(96, 98, 102, 0.35)"
:height="140"
export-bg-color="#f7f8fa"
line-color="#303133"
:show-tips="false"
@submit="onSubmit"
/>
</template>
<script setup lang="uts">
// grid-color 控制网格线颜色,guide-color 控制描红文字颜色
// 两者默认都是参考线红,换成蓝/灰可得到钢笔、铅笔字帖的观感
function onSubmit(image: string): void {
console.log('base64 长度', image.length);
}
</script>Tracing Content
guide-text is written one character per cell; rows grow with the text length and the block is vertically centered by default, while characters that no longer fit are truncated.
View source
<template>
<ux-signature grid-type="mi" guide-text="永" :height="140" export-bg-color="#ffffff" line-color="#303133" :show-tips="false" @submit="onSubmit" />
<ux-gap :height="8" />
<ux-signature
grid-type="mi"
guide-text="书山有路勤为径学海无涯苦作舟"
:height="140"
export-bg-color="#ffffff"
line-color="#303133"
:show-tips="false"
@submit="onSubmit"
/>
</template>
<script setup lang="uts">
// guide-text 逐字写入格子,行数随文字长度增加,默认整块垂直居中
// 超出当前行数能容纳的字会截断,画布高度不够时调大 height 或多写几行
function onSubmit(image: string): void {
console.log('base64 长度', image.length);
}
</script>Grid Alignment
grid-align sets the vertical placement: center (default) keeps a single row of tracing balanced in the canvas; top aligns the first row to the top edge for worksheet-style multi-row writing.
View source
<template>
<ux-signature grid-type="mi" guide-text="居中" :height="160" export-bg-color="#ffffff" line-color="#303133" :show-tips="false" @submit="onSubmit" />
<ux-gap :height="8" />
<ux-signature grid-type="mi" guide-text="顶齐" grid-align="top" :height="160" export-bg-color="#ffffff" line-color="#303133" :show-tips="false" @submit="onSubmit" />
</template>
<script setup lang="uts">
// grid-align:center 居中(默认)| top 顶对齐
// 单行或少量描红居中观感更平衡;多行字帖式书写可设为 top,让第一行贴齐顶部
function onSubmit(image: string): void {
console.log('base64 长度', image.length);
}
</script>Export and Undo
The grid lives inside the canvas, so undo and clear repaint it and exports keep the grid. Tracing text is a screen-only hint and never appears in exports. Without export-bg-color the export is transparent; set export-bg-color with a dark line-color for a paper look.
View source
<template>
<ux-signature
grid-type="mi"
guide-text="签名练习"
:show-revoke="true"
export-bg-color="#ffffff"
line-color="#303133"
:show-tips="false"
@submit="onSubmit"
/>
<ux-gap :height="8" />
<ux-signature grid-type="mi" guide-text="签到" :height="140" :show-tips="false" @submit="onSubmit" />
<ux-modal
v-model="showPreview"
title="导出图片(含格)"
cancel-text="保存图片"
confirm-text="确认"
:show-cancel-button="true"
>
<image :src="preview" mode="widthFix" />
</ux-modal>
</template>
<script setup lang="uts">
const showPreview = ref(false);
const preview = ref<string>('');
function onSubmit(image: string): void {
if (image == '') {
uni.showToast({ title: '请先手写', icon: 'none' });
return;
}
preview.value = image;
showPreview.value = true;
}
</script>
<!-- 格线在画布内:撤销、清空后自动重铺,导出图保留网格;描红只作屏幕提示,不写入导出图;
不设 bg-color 时导出透明背景(只保留网格与笔迹),白纸效果需设置 bg-color 并搭配深色 line-color。
保存图片:uni.base64ToArrayBuffer + getFileSystemManager().writeFile 落临时文件,
再调用 uni.saveImageToPhotosAlbum({ filePath });H5 端不支持保存到相册。 -->Ink-Only Export
Same board for comparison: export-grid defaults to true and keeps the grid with the strokes; set it to false to export only the background color and strokes. Only the grid differs between the two, tracing is never exported.
View source
<template>
<ux-signature
grid-type="mi"
guide-text="签名练习"
:export-grid="false"
export-bg-color="#ffffff"
line-color="#303133"
:show-tips="false"
@submit="onSubmit"
/>
<ux-gap :height="8" />
<ux-signature
grid-type="mi"
guide-text="签名练习"
:export-grid="true"
export-bg-color="#ffffff"
line-color="#303133"
:show-tips="false"
@submit="onSubmit"
/>
<ux-modal
v-model="showPreview"
title="导出图片"
cancel-text="保存图片"
confirm-text="确认"
:show-cancel-button="true"
>
<image :src="preview" mode="widthFix" />
</ux-modal>
</template>
<script setup lang="uts">
const showPreview = ref(false);
const preview = ref<string>('');
function onSubmit(image: string): void {
if (image == '') {
uni.showToast({ title: '请先手写', icon: 'none' });
return;
}
preview.value = image;
showPreview.value = true;
}
</script>
<!-- export-grid 默认 true:导出保留网格与笔迹;描红只作屏幕提示,任何导出图都不包含;
设为 false 时先清屏、只铺 bg-color 与笔画再取图,导出图不含网格,屏幕显示不变;
点「确定」在弹窗里对照上方(纯净)与下方(含格)两张图。
降级:无笔画或尺寸变化后自动回退为屏幕快照,不会返回空图。 -->API
Props
| Property | Description | Type | Default | Version |
|---|---|---|---|---|
customStyle | Custom root element style | string | UTSJSONObject | - | - |
customClass | Custom root element style class | string | '' | - |
width | Component width; number (px), percentage or any CSS length | number | string | '100%' | - |
height | Canvas area height in px | number | string | 200 | - |
exportFormat | Export format: png | jpeg (default png; transparent areas turn black in jpeg, so set exportBgColor as well) | string | 'png' | - |
quality | JPEG export quality, 0-1 | number | 0.8 | - |
disabled | Disable touch writing | boolean | false | - |
showTips | Show the tips layer | boolean | true | - |
tips | Tip text, falls back to the built-in locale text | string | '' | - |
tipsSize | Tip font size in px | number | string | 24 | - |
tipsColor | Tip text color, falls back to a translucent watermark color | string | '' | - |
lineColor | Stroke color, falls back to the theme primary text color | string | '' | - |
lineWidth | Stroke width in px | number | 3 | - |
bgColor | Signature area container background, painted on screen only and never exported | string | '' | - |
exportBgColor | Canvas fill color written into the export; transparent when empty | string | '' | - |
exportGrid | Keep the writing grid in the export | boolean | true | - |
gridType | Grid line type: empty disables it; tian | mi | nine | string | '' | - |
gridSize | Cell edge length in px (clamped to 24-120) | number | 56 | - |
gridColor | Grid line color, falls back to reference red | string | '' | - |
guideText | Tracing text, one character per cell | string | '' | - |
guideColor | Tracing text color, falls back to translucent red | string | '' | - |
gridAlign | Vertical grid alignment: center | top | string | 'center' | - |
showClear | Show the clear button | boolean | true | - |
showRevoke | Show the undo button | boolean | false | - |
showConfirm | Show the confirm button | boolean | true | - |
clearText | Clear button label, falls back to the built-in locale text | string | '' | - |
revokeText | Undo button label, falls back to the built-in locale text | string | '' | - |
confirmText | Confirm button label, falls back to the built-in locale text | string | '' | - |
Events
| Event | Description | Params | Version |
|---|---|---|---|
start | Emitted when a stroke starts | - | - |
signing | Emitted continuously while writing | - | - |
end | Emitted on stroke end or touch cancel | - | - |
submit | Emitted on confirm with a base64 data URL; empty string when nothing was drawn | image: string | - |
Slots
| Slot | Description | Version |
|---|---|---|
tips | Custom tips layer above the canvas; it does not block writing | - |
Notes
- Grid layout: columns come from the canvas width and
grid-size(clamped to 24-120); rows take the smaller of the rows needed by the tracing text and the rows that fit the height, and the block is centered.grid-align="top"starts from the top edge for multi-row worksheets. gridaccepts onlytian/mi/nine; any other value disables the grid. The outer border is solid while inner guides are hand-drawn dashes (nosetLineDash), so all platforms render the same.- Tracing:
guide-textis centered one character per cell with a font size of 0.76 × the cell edge; characters beyond the visible cells are truncated, and tracing never appears in exports. - Ink-only export: with
export-grid=falsethe image holds only background and strokes; if the stroke records no longer match the canvas (undo after clear, resize), the component falls back to the current screen snapshot with the grid instead of returning an empty image. - Colors: grid lines default to reference red and tracing to translucent red, neither follows the theme; custom
grid-color/guide-colorapply on the next repaint. - Undo, clear and resize all repaint the grid and tracing; on HarmonyOS
revoke()cannot restore content after a clear (stroke records are dropped). - Background and export behave like the signature pad:
export-bg-coloris exported,bg-coloris on-screen only, andjpegturns transparent areas black. - In dark mode the grid keeps its fixed reference color while strokes use the theme primary text color; pair a light
export-bg-colorwith a darkline-color.