JS画布实现签名,生成base64位图片

JS画布实现签名,生成base64位图片

需要引入jq,自行下载并引入

代码:

<html>
    <title>test</title>
    <body>
        <div id="canvasParcel" style="position: absolute;top: 25%;left:25%;cursor:default;z-index:999999999;">
            <canvas id="canvas" width="800px" height="300px"></canvas>
            <button onclick="exportSignature(this)">确定</button>
            <button onclick="empty(this)">清空</button>
        </div>
        <div id="exportImg">
        </div>

    </body>
    //路径根据自己的修改
    <script src="./jquery-3.3.1/jquery-3.3.1.min.js" type="text/javascript"></script>    <script>
// 加载画布
var canvaDom = document.getElementById("canvas");   // 画布对象
var context = canvaDom.getContext("2d");            // 画板的上下文
var canvasX = canvaDom.getBoundingClientRect().left;// 画板的坐标x
var canvasY = canvaDom.getBoundingClientRect().top; // 画板的坐标y
context.fillStyle = "gray";                        // 画布背景色
context.lineWidth = 4;                              // 线的宽度
context.strokeStyle = "white";                      // 线的颜色
context.fillRect(0, 0, canvaDom.width, canvaDom.height);// 画板的范围
canvaDom.addEventListener("mousedown", down, false);// 鼠标按下去的事件
canvaDom.addEventListener("mousemove", draw, false);// 鼠标移动事件
canvaDom.addEventListener("mouseup", up, false);    // 鼠标松开事件

// 鼠标按下
var canvaDom;
var onoff = false;  // 锁定开关
var oldx = canvasX; // 起始坐标x
var oldy = canvasY; // 起始坐标y
var newx;           // 结束坐标x
var newy;           // 结束坐标y

function down(event) {
    onoff = true;                   // 打开开关
    oldx = event.clientX -canvasX;  // 鼠标在画板中点击的X的坐标
    oldy = event.clientY -canvasY;  // 鼠标在画板中点击的Y的坐标
    context.beginPath();            // 开始路径
}

// 鼠标移动
function draw(event) {
    // 开关
    if (onoff) {
        newx = event.clientX-canvasX;
        newy = event.clientY-canvasY;
        context.moveTo(oldx, oldy); // 线的起点坐标
        context.lineTo(newx, newy); // 线的始点坐标
        context.stroke();           // 初始化到画布中
        oldx = newx;
        oldy = newy;
    }
}
// 鼠标离开
function up() {
    onoff = false;      // 关闭开关
    context.closePath();// 关闭路径
}

// 清空
function empty() {
    context.clearRect(0,0,canvaDom.width,canvaDom.height);  // 清空的范围
    context.fillRect(0, 0, canvaDom.width, canvaDom.height);// 重设的范围
}

//导出信息
function exportSignature() {
    var exportImg = canvas.toDataURL("image/png",0.1);// 0.1代表压缩的比率,默认为1
    console.info(exportImg);
    $("#exportImg").append("<img src='"+exportImg+"' style='width:100px;margin-right:20px;'/>");
    empty();
}

</script>
</html>

效果:
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值