Skip to content

Signature Writing Grid ​

1:35

Description ​

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 ​

PlatformSupport
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
vue
<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
vue
<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
vue
<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
vue
<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
vue
<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
vue
<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
vue
<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 ​

PropertyDescriptionTypeDefaultVersion
customStyleCustom root element stylestring | UTSJSONObject--
customClassCustom root element style classstring''-
widthComponent width; number (px), percentage or any CSS lengthnumber | string'100%'-
heightCanvas area height in pxnumber | string200-
exportFormatExport format: png | jpeg (default png; transparent areas turn black in jpeg, so set exportBgColor as well)string'png'-
qualityJPEG export quality, 0-1number0.8-
disabledDisable touch writingbooleanfalse-
showTipsShow the tips layerbooleantrue-
tipsTip text, falls back to the built-in locale textstring''-
tipsSizeTip font size in pxnumber | string24-
tipsColorTip text color, falls back to a translucent watermark colorstring''-
lineColorStroke color, falls back to the theme primary text colorstring''-
lineWidthStroke width in pxnumber3-
bgColorSignature area container background, painted on screen only and never exportedstring''-
exportBgColorCanvas fill color written into the export; transparent when emptystring''-
exportGridKeep the writing grid in the exportbooleantrue-
gridTypeGrid line type: empty disables it; tian | mi | ninestring''-
gridSizeCell edge length in px (clamped to 24-120)number56-
gridColorGrid line color, falls back to reference redstring''-
guideTextTracing text, one character per cellstring''-
guideColorTracing text color, falls back to translucent redstring''-
gridAlignVertical grid alignment: center | topstring'center'-
showClearShow the clear buttonbooleantrue-
showRevokeShow the undo buttonbooleanfalse-
showConfirmShow the confirm buttonbooleantrue-
clearTextClear button label, falls back to the built-in locale textstring''-
revokeTextUndo button label, falls back to the built-in locale textstring''-
confirmTextConfirm button label, falls back to the built-in locale textstring''-

Events ​

EventDescriptionParamsVersion
startEmitted when a stroke starts--
signingEmitted continuously while writing--
endEmitted on stroke end or touch cancel--
submitEmitted on confirm with a base64 data URL; empty string when nothing was drawnimage: string-

Slots ​

SlotDescriptionVersion
tipsCustom tips layer above the canvas; it does not block writing-

Notes ​

  1. 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.
  2. grid accepts only tian / mi / nine; any other value disables the grid. The outer border is solid while inner guides are hand-drawn dashes (no setLineDash), so all platforms render the same.
  3. Tracing: guide-text is 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.
  4. Ink-only export: with export-grid=false the 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.
  5. Colors: grid lines default to reference red and tracing to translucent red, neither follows the theme; custom grid-color / guide-color apply on the next repaint.
  6. Undo, clear and resize all repaint the grid and tracing; on HarmonyOS revoke() cannot restore content after a clear (stroke records are dropped).
  7. Background and export behave like the signature pad: export-bg-color is exported, bg-color is on-screen only, and jpeg turns transparent areas black.
  8. In dark mode the grid keeps its fixed reference color while strokes use the theme primary text color; pair a light export-bg-color with a dark line-color.