npm i @rtdui/signature-pad
tip: 记得安装
@rtdui/hooks和@rtdui/core配对依赖, 如果还未安装的话.
@rtdui/signature-pad包提供了SignaturePad组件, 用于手写签名, 它基于canvas实现平滑流畅的书写.
传递你的ref给SignaturePad, 此后就可以通过你的ref调用SignaturePad的API:
import { SignaturePad, SignaturePadHandle } from "@rtdui/signature-pad";
export default function Demo() {
const signPadRef = useRef<SignaturePadHandle>(null!);
const handleBtnClick = () => {
signPadRef.current.toDataURL(); // 默认为image/png格式
// signPadRef.current.toDataURL("image/jpeg"); // image/jpeg格式
// signPadRef.current.toDataURL("image/jpeg", 0.5); // image/jpeg格式, 带输出图像质量
// signPadRef.current.toDataURL("image/svg+xml"); // image/svg+xml格式
// signPadRef.current.toDataURL("image/svg+xml", {includeBackgroundColor: true,}); // image/svg+xml格式, 带背景输出
};
return (
<>
<SignaturePad ref={signPadRef} />
<Button onClick={handleBtnClick}>输出</Button>
</>
);
}
通过ref可调用的API:
type SignaturePadHandle = {
/** Clears the canvas */
clear(): void;
/** Returns true if canvas is empty, otherwise returns false */
isEmpty(): boolean;
/** Draws signature image from data URL and alters it with the given options */
fromDataURL(
dataUrl: string,
options?: {
ratio?: number;
width?: number;
height?: number;
xOffset?: number;
yOffset?: number;
}
): Promise<void>;
/** Returns signature image as SVG data URL with {includeBackgroundColor} option */
toDataURL(type: "image/svg+xml", encoderOptions?: ToSVGOptions): string;
/** Returns signature image as data URL */
toDataURL(type?: string, quality?: number): string;
/** Draws signature image from an array of point groups */
fromData(pointGroups: PointGroup[], option?: FromDataOptions): void;
/** Returns signature image as an array of point groups */
toData(): PointGroup[];
/** Return svg string without converting to base64 */
toSVG(option?: ToSVGOptions): string;
undo(): void;
redo(): void;
};
toDataURL, toData, toSVG API方法用于输出图像.
SignaturePad组件的签名输出支 持:
<svg...</svg>backgroundColor属性可以设置画布的背景色, 默认是透明的, 在透明背景无法输出JPEG格式的图像, 当你想要输出JPEG格式的图像时, 必须设置一个非透明的背景色.
penColor属性可以设置笔刷的颜色, 默认为: 黑色minWidth属性可以设置笔刷的最小宽度, 默认为: 0.5maxWidth属性可以设置笔刷的最大宽度, 默认为: 2.5通过slots样式槽属性可以为其内部的元素添加自定义样式, 例如为清除按钮和确认按钮添加自定义的CSS类名:
<SignaturePad
ref={signPadRef}
slots={{
clearBtn: "myClearBtnClass",
confirmBtn: "myConfirmBtnClass",
}}
/>
结合TailwindCss使用可以很方便的加入样式:
<SignaturePad
ref={signPadRef}
slots={{
clearBtn:
"w-20 h-8 min-h-0 p-1 bg-red-500 text-white hover:bg-red-700 rounded-full",
confirmBtn:
"w-20 h-8 min-h-0 p-1 bg-blue-500 text-white hover:bg-blue-700 rounded-full",
}}
/>
| 属性名 | 类型 | 是否必须 | 默认值 | 说明 |
|---|---|---|---|---|
| backgroundColor | Fill | undefined | no | "transparent" | Color used to canvas the background. Can be any color format accepted by context.fillStyle. Use a non-transparent color e.g. "rgb(255,255,255)" if you'd like to save signatures as JPEG images. |
| canvasContextOptions | CanvasRenderingContext2DSettings | undefined | no | ||
| clearLabel | string | undefined | no | "Clear" | clear button text, can be localized |
| confirmLabel | string | undefined | no | "Confirm" | confirm button text, can be localized |
| dotSize | number | undefined | no | (minWidth + maxWidth) / 2 | Radius of a single dot. Also the width of the start of a mark. |
| height | number | undefined | no | 150 | canvas height |
| maxWidth | number | undefined | no | 2.5 | Maximum width of a line |
| minDistance | number | undefined | no | 5 | Add the next point only if the previous one is farther than x pixels. |
| minWidth | number | undefined | no | 0.5 | Minimum width of a line |
| onClear | (() => void) | undefined | no | ||
| onConfirm | (() => void) | undefined | no | ||
| penColor | Fill | undefined | no | "black" | Color used to draw the lines. Can be any color format accepted by context.fillStyle. Defaults to "black" |
| slots | { canvasWrapper?: string | undefined; actions?: string | undefined; clearBtn?: string | undefined; confirmBtn?: string | undefined; } | undefined | no | ||
| throttle | number | undefined | no | 16 | Draw the next point at most once in milliseconds. Set it to 0 to turn off throttling |
| velocityFilterWeight | number | undefined | no | 0.7 | Weight used to modify new velocity based on the previous velocity |
| width | number | undefined | no | 300 | canvas width |
| withClear | boolean | undefined | no |