react-native-signature-canvas

react-native-signature-canvas

React Native 画布签名组件 支持Android iOS和Expo

react-native-signature-canvas是一个基于Canvas的React Native签名组件,支持Android、iOS和Expo平台。提供背景图片、覆盖图、笔触样式等自定义选项,可生成base64编码的PNG签名图片。组件包含多种回调函数和控制方法,方便开发者在移动应用中实现灵活的电子签名功能。

React Native签名组件Canvas移动应用开发npm包Github开源项目

react-native-signature-canvas

npm GitHub last commit runs with expo

基于Canvas的React Native签名组件,适用于Android、iOS和Expo

  • 支持Android、iOS和Expo
  • 已在RN 0.69上测试
  • 核心使用 signature_pad.js
  • 生成签名的base64编码png图像 注意:React Native Signature Canvas v1.5.0开始支持Expo SDK v33.0.0及以上版本。

安装(适用于React Native V0.60.0或Expo SDK v35.0.0)

yarn add react-native-signature-canvas

npm install --save react-native-signature-canvas

该包依赖于 react-native-webview,特别是在使用React Native CLI时需要。要安装react-native-webview,请按照此处提到的步骤操作。

安装(适用于React Native V0.5x.x或Expo SDK < v33)

npm install --save react-native-signature-canvas@1.4.2

使用方法

基本用法

import Signature from "react-native-signature-canvas";

自定义用法

import SignatureScreen from "react-native-signature-canvas";

属性


属性类型描述
androidHardwareAccelerationDisabledbooleanreact-native-webview的androidHardwareAccelerationDisabled选项。默认为false
autoClearboolean点击确认按钮后是否自动清除签名
backgroundColorstring默认为"rgba(255,255,255,0)"(透明),画布的背景颜色
bgHeightnumber背景图像的高度
bgWidthnumber背景图像的宽度
bgSrcstring背景图像源URI(URL)
clearTextstring清除按钮文本
confirmTextstring保存按钮文本
customHtml(injectedJavaScript: string) => string允许修改布局或元素的HTML字符串
dataURLstring默认为"",Base64字符串,从dataURL绘制保存的签名
descriptionTextstring签名的描述文本
dotSizenumber单个点的半径(非笔画宽度)
imageTypestring"image/png"(默认)、"image/jpeg"、"image/svg+xml",导出签名的图像类型
minWidthnumber线条的最小宽度。默认为0.5
maxWidthnumber线条的最大宽度。默认为2.5
minDistancenumber仅当前一个点距离超过x像素时才添加下一个点。默认为5
nestedScrollEnabledboolean在scrollview内部使用时启用嵌套滚动
showsVerticalScrollIndicatorboolean布尔值,决定是否在WebView中显示垂直滚动指示器。默认值为true
onOKfunction保存非空签名后的回调函数
onEmptyfunction尝试保存空签名后的回调函数
onClearfunction清除签名后的回调函数
onGetDatafunction调用getData()时的回调函数
onBeginfunction开始新笔画时的回调函数
onEndfunction笔画结束时的回调函数
onLoadEndfunctionwebview画布加载结束时的回调函数
onUndofunction调用undo()时的回调函数
onRedofunction调用redo()时的回调函数
onDrawfunction启用绘图时的回调函数
onErasefunction启用擦除时的回调函数
onChangePenColorfunction更改笔颜色后的回调函数
onChangePenSizefunction更改笔大小后的回调函数
overlayHeightnumber覆盖图像的高度
overlayWidthnumber覆盖图像的宽度
overlaySrcstring覆盖图像源URI(URL)必须是具有透明背景的.png文件
penColorstring默认为"black",笔的颜色
rotatedboolean将签名板旋转90度
styleobject包装视图的样式
trimWhitespaceboolean修剪图像白色空间
webStylestring用于覆盖默认样式的webview样式,所有样式:https://github.com/YanYuanFE/react-native-signature-canvas/blob/master/h5/css/signature-pad.css
androidLayerTypenone、software、hardware设置android webview的layerType

方法


函数描述
clearSignature()清除当前签名
changePenColor(color)更改笔颜色
changePenSize(minW, maxW)更改笔大小
draw()启用签名绘制
erase()启用签名擦除
getData()触发onGetData回调,传入单个data JSON字符串
readSignature()读取画布上的当前签名,并触发onOKonEmpty回调
undo()撤销上一笔
redo()重做上一笔

要调用这些方法,请使用useRef hook:

import SignatureScreen from "react-native-signature-canvas"; const Sign = ({ text, onOK }) => { const ref = useRef(); // 在ref.current.readSignature()读取非空base64字符串后调用 const handleOK = (signature) => { console.log(signature); onOK(signature); // 来自组件props的回调 }; // 在ref.current.readSignature()读取空字符串后调用 const handleEmpty = () => { console.log("Empty"); }; // 在ref.current.clearSignature()之后调用 const handleClear = () => { console.log("clear success!"); }; // 在笔画结束后调用 const handleEnd = () => { ref.current.readSignature(); }; // 在ref.current.getData()之后调用 const handleData = (data) => { console.log(data); }; return ( <SignatureScreen ref={ref} onEnd={handleEnd} onOK={handleOK} onEmpty={handleEmpty} onClear={handleClear} onGetData={handleData} autoClear={true} descriptionText={text} /> ); }; export default Sign;

使用背景图像

您可以使用bgSrc属性设置一个不可擦除的背景图像来绘制您的签名。请确保提供图像的宽度和高度。

const imgWidth = 300; const imgHeight = 200; const style = `.m-signature-pad {box-shadow: none; border: none; } .m-signature-pad--body {border: none;} .m-signature-pad--footer {display: none; margin: 0px;} body,html { width: ${imgWidth}px; height: ${imgHeight}px;}`; ... <View style={{ width: imgWidth, height: imgHeight }}> <SignatureScreen ref={ref} bgSrc="https://via.placeholder.com/300x200/ff726b" bgWidth={imgWidth} bgHeight={imgHeight} webStyle={style} onOK={handleOK} /> </View>

使用覆盖图像

覆盖图是一个不可擦除的图像,可以用作指引,类似于填色书。请确保图像格式为.png,并且具有透明背景。同时,不要忘记提供图像的宽度和高度。 使用overlaySrc属性提供链接。

const imgWidth = 256; const imgHeight = 256; const style = `.m-signature-pad {box-shadow: none; border: none; } .m-signature-pad--body {border: none;} .m-signature-pad--footer {display: none; margin: 0px;} body,html { width: ${imgWidth}px; height: ${imgHeight}px;}`; ... <View style={{ width: imgWidth, height: imgHeight }}> <SignatureScreen ref={ref} overlaySrc="http://pngimg.com/uploads/circle/circle_PNG63.png" overlayWidth={imgWidth} overlayHeight={imgHeight} webStyle={style} onOK={handleOK} /> </View>

将Base64图像保存为文件

如果您使用的是expo,可以使用expo-file-system将base64图像保存为本地文件;如果您使用的是react-native-cli,请使用react-native-fs

import * as FileSystem from "expo-file-system"; const handleOK = (signature) => { const path = FileSystem.cacheDirectory + "sign.png"; FileSystem.writeAsStringAsync( path, signature.replace("data:image/png;base64,", ""), { encoding: FileSystem.EncodingType.Base64 } ) .then(() => FileSystem.getInfoAsync(path)) .then(console.log) .catch(console.error); };

基本参数

<Signature // 点击保存按钮时的处理函数 onOK={(img) => console.log(img)} onEmpty={() => console.log("empty")} // 签名的描述文本 descriptionText="Sign" // 清除按钮文本 clearText="Clear" // 保存按钮文本 confirmText="Save" // 字符串,用于覆盖默认样式的webview样式,所有样式:https://github.com/YanYuanFE/react-native-signature-canvas/blob/master/h5/css/signature-pad.css webStyle={`.m-signature-pad--footer .button { background-color: red; color: #FFF; }`} autoClear={true} imageType={"image/svg+xml"} />

如果您创建自己的触发器来读取签名和/或清除签名,可以通过传入webStyle属性的css样式来隐藏内置的清除和保存按钮。

const webStyle = `.m-signature-pad--footer .save { display: none; } .clear { display: none; } `; ... <Signature webStyle={webStyle} onOK={handleOK} onEmpty={handleEmpty} onEnd={handleEnd} />

自定义确认和清除按钮

import React, { useRef } from "react"; import { StyleSheet, View, Button } from "react-native"; import SignatureScreen from "react-native-signature-canvas"; const Sign = ({ onOK }) => { const ref = useRef(); const handleOK = (signature) => { console.log(signature); onOK 返回 ( <View style={styles.container}> <SignatureScreen ref={ref} onOK={handleOK} webStyle={style} /> <View style={styles.row}> <Button title="清除" onPress={handleClear} /> <Button title="确认" onPress={handleConfirm} /> </View> </View> ); export default Sign; const styles = StyleSheet.create({ container: { flex: 1, alignItems: "center", justifyContent: "center", height: 250, padding: 10, }, row: { display: "flex", flexDirection: "row", justifyContent: "space-between", width: "100%", alignItems: "center", }, }); ## 示例 - Android <br/> <img src="http://img.yanyuanfe.cn/signature-android.png" width="400" /> - iOS <br/> <img src="http://img.yanyuanfe.cn/signature-ios.png" width="400" /> import React, { useState } from "react"; import { StyleSheet, Text, View, Image } from "react-native"; import Signature from "react-native-signature-canvas"; export const SignatureScreen = () => { const [signature, setSign] = useState(null); const handleOK = (signature) => { console.log(signature); setSign(signature); }; const handleEmpty = () => { console.log("空白"); }; const style = `.m-signature-pad--footer .button { background-color: red; color: #FFF; }`; return ( <View style={{ flex: 1 }}> <View style={styles.preview}> {signature ? ( <Image resizeMode={"contain"} style={{ width: 335, height: 114 }} source={{ uri: signature }} /> ) : null} </View> <Signature onOK={handleOK} onEmpty={handleEmpty} descriptionText="签名" clearText="清除" confirmText="保存" webStyle={style} /> </View> ); }; const styles = StyleSheet.create({ preview: { width: 335, height: 114, backgroundColor: "#F8F8F8", justifyContent: "center", alignItems: "center", marginTop: 15, }, previewText: { color: "#FFF", fontSize: 14, height: 40, lineHeight: 40, paddingLeft: 10, paddingRight: 10, backgroundColor: "#69B2FF", width: 120, textAlign: "center", marginTop: 10, }, }); ## 使用 TypeScript 要使用 TypeScript,只需导入 `SignatureViewRef`,并在 [useRef hook](https://reactjs.org/docs/hooks-reference.html#useref) 中指明引用的类型为 `SignatureViewRef`,这样就可以使用常规的 `ref` 方法。 import React, { useRef } from "react"; import SignatureScreen, { SignatureViewRef, } from "react-native-signature-canvas"; interface Props { text: string; onOK: (signature) => void; } const Sign: React.FC<Props> = ({ text, onOK }) => { const ref = useRef<SignatureViewRef>(null); const handleSignature = (signature) => { console.log(signature); onOK(signature); }; const handleEmpty = () => { console.log("空白"); }; const handleClear = () => { console.log("清除成功!"); }; const handleEnd = () => { ref.current?.readSignature(); }; return ( <SignatureScreen ref={ref} onEnd={handleEnd} onOK={handleSignature} onEmpty={handleEmpty} onClear={handleClear} autoClear={true} descriptionText={text} /> ); }; export default Sign; ## 在 ScrollView 中使用的示例 当在 ScrollView 中使用 `react-native-signature-canvas` 时,你只能在画布上得到一个点,而 ScrollView 会处理手势,使其无法用于画布。 解决方法是使用 `ScrollView``scrollEnabled` 属性。 这里有一个示例: import React, {useState} from 'react'; import {ScrollView, View} from 'react-native'; import Signature from 'react-native-signature-canvas'; const SignInScroll = () => { const [scrollEnabled, setScrollEnabled] = useState(true); return ( <ScrollView scrollEnabled={scrollEnabled}> <View style={{height: 300}}> <Signature onOK={(img) => console.log(img)} onBegin={() => setScrollEnabled(false)} onEnd={() => setScrollEnabled(true)} descriptionText="签名" clearText="清除" confirmText="保存" imageType="image/jpeg" /> </View> </ScrollView> ); }; export default SignInScroll;

编辑推荐精选

酷表ChatExcel

酷表ChatExcel

大模型驱动的Excel数据处理工具

基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。

AI工具酷表ChatExcelAI智能客服AI营销产品使用教程
DeepEP

DeepEP

DeepSeek开源的专家并行通信优化框架

DeepEP是一个专为大规模分布式计算设计的通信库,重点解决专家并行模式中的通信瓶颈问题。其核心架构采用分层拓扑感知技术,能够自动识别节点间物理连接关系,优化数据传输路径。通过实现动态路由选择与负载均衡机制,系统在千卡级计算集群中维持稳定的低延迟特性,同时兼容主流深度学习框架的通信接口。

DeepSeek

DeepSeek

全球领先开源大模型,高效智能助手

DeepSeek是一家幻方量化创办的专注于通用人工智能的中国科技公司,主攻大模型研发与应用。DeepSeek-R1是开源的推理模型,擅长处理复杂任务且可免费商用。

问小白

问小白

DeepSeek R1 满血模型上线

问小白是一个基于 DeepSeek R1 模型的智能对话平台,专为用户提供高效、贴心的对话体验。实时在线,支持深度思考和联网搜索。免费不限次数,帮用户写作、创作、分析和规划,各种任务随时完成!

AI主流办公工具有哪些办公热门AI 助手
KnowS

KnowS

AI医学搜索引擎 整合4000万+实时更新的全球医学文献

医学领域专用搜索引擎整合4000万+实时更新的全球医学文献,通过自主研发AI模型实现精准知识检索。系统每日更新指南、中英文文献及会议资料,搜索准确率较传统工具提升80%,同时将大模型幻觉率控制在8%以下。支持临床建议生成、文献深度解析、学术报告制作等全流程科研辅助,典型用户反馈显示每周可节省医疗工作者70%时间。

Windsurf Wave 3

Windsurf Wave 3

Windsurf Editor推出第三次重大更新Wave 3

新增模型上下文协议支持与智能编辑功能。本次更新包含五项核心改进:支持接入MCP协议扩展工具生态,Tab键智能跳转提升编码效率,Turbo模式实现自动化终端操作,图片拖拽功能优化多模态交互,以及面向付费用户的个性化图标定制。系统同步集成DeepSeek、Gemini等新模型,并通过信用点数机制实现差异化的资源调配。

AI IDE
腾讯元宝

腾讯元宝

腾讯自研的混元大模型AI助手

腾讯元宝是腾讯基于自研的混元大模型推出的一款多功能AI应用,旨在通过人工智能技术提升用户在写作、绘画、翻译、编程、搜索、阅读总结等多个领域的工作与生活效率。

AI助手AI对话AI工具腾讯元宝智能体热门 AI 办公助手
Grok3

Grok3

埃隆·马斯克旗下的人工智能公司 xAI 推出的第三代大规模语言模型

Grok3 是由埃隆·马斯克旗下的人工智能公司 xAI 推出的第三代大规模语言模型,常被马斯克称为“地球上最聪明的 AI”。它不仅是在前代产品 Grok 1 和 Grok 2 基础上的一次飞跃,还在多个关键技术上实现了创新突破。

OmniParser

OmniParser

帮助AI理解电脑屏幕 纯视觉GUI元素的自动化解析方案

开源工具通过计算机视觉技术实现图形界面元素的智能识别与结构化处理,支持自动化测试脚本生成和辅助功能开发。项目采用模块化设计,提供API接口与多种输出格式,适用于跨平台应用场景。核心算法优化了元素定位精度,在动态界面和复杂布局场景下保持稳定解析能力。

OmniParser界面解析交互区域检测Github开源项目
流畅阅读

流畅阅读

AI网页翻译插件 双语阅读工具,还原母语级体验

流畅阅读是一款浏览器翻译插件,通过上下文智能分析提升翻译准确性,支持中英双语对照显示。集成多翻译引擎接口,允许用户自定义翻译规则和快捷键配置,操作数据全部存储在本地设备保障隐私安全。兼容Chrome、Edge、Firefox等主流浏览器,基于GPL-3.0开源协议开发,提供持续的功能迭代和社区支持。

AI翻译AI翻译引擎AI翻译工具
下拉加载更多