react手写签名功能

步骤:

首先安装一下依赖 cnpm i react-canvas-draw
这边点击确定,返回的是base64格式,可以做一下处理之后页面回显,因为需要后端配合,这边就不展示了。
在这里插入图片描述

import React, { memo, useRef, useState } from "react";
import SignatureCanvas from "react-canvas-draw";
import { Modal, Button } from "antd";

export default memo(() => {
  const signatureCanvas = useRef(null);
  const [visible, setVisible] = useState(false);

  const cleanCanvase = () => {
    signatureCanvas.current.clear();
  };

  const handleOk = async () => {
    const imageData = signatureCanvas.current.canvas.drawing.toDataURL("image/png");
    //  const imageData = signatureCanvas.current.canvas.toDataURL('image/png')
    console.log(imageData, "imageData");
    /*  if (!signatureCanvas.current.isEmpty()) {
      const imgId = await imageFormPost(imageData);
       
    } */
    // setVisible(false);
  };

  const cancelHandle = () => {
    signatureCanvas.current.clear();
    setVisible(false);
  };

  return (
    <div>
      <Button type="primary" onClick={() => setVisible(true)}>
        Open Modal
      </Button>
      {visible && (
        <Modal
          title="Basic Modal"
          visible={visible}
          onOk={handleOk}
          onCancel={() => cancelHandle()}
        >
          <div style={{ border: "dashed 1px #e3e3e3" }}>
            <SignatureCanvas ref={signatureCanvas} canvasProps={{ width: 500, height: 200 }} />
          </div>
          <div
            style={{
              color: "#333",
              fontSize: 13,
              display: "flex",
              justifyContent: "space-between",
              alignItems: "center",
              marginTop: 12
            }}
          >
            <div>请在虚线框内进行签名</div>
            <div>
              <Button type="danger" onClick={cleanCanvase}>
                重写
              </Button>
            </div>
          </div>
        </Modal>
      )}
    </div>
  );
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值