电子签名(js)

<!DOCTYPE html>
<html>
<head >
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        #canvas {
            border: thin solid blue;
        }
    </style>
    <script src="qm/jquery.min.js"></script>
</head>


<body >
    
        <button type="button" οnclick="overwrite()">重写</button>
        <button type="button" οnclick="submit()">提交签名</button>
        <canvas id="canvas" width="600" height="600"></canvas>
    <script type="text/javascript">
        var context;
        window.onload = function () {

            var device =

             /android|iphone|ipad|ipod|webos|iemobile|opear mini|linux/i.test(navigator.userAgent.toLowerCase());

            var startEvtName = device ? "touchstart" : "mousedown";
            var moveEvtName = device ? "touchmove" : "mousemove";
            var endEvtName = device ? "touchend" : "mouseup";


            var canvas = document.getElementById("canvas");
            canvas.height = window.screen.height - 500; //canvas的宽高必须在js在设置,在css中设置大小是缩放效果
            canvas.width = window.screen.width;
            context = canvas.getContext("2d");

            console.log(context);


            var isdown = false;

            canvas.addEventListener(moveEvtName, function (e) {
                if (!isdown) return;
                var location;
                if (device) {
                    location = getLocation(e.changedTouches[0].clientX, e.changedTouches[0].clientY);
                } else {
                    location = getLocation(e.clientX, e.clientY);
                }
                points[points.length] = location;
                context.beginPath();
                context.moveTo(downlocation.x, downlocation.y);
                context.lineTo(location.x, location.y);
                context.stroke();
                context.closePath();
                downlocation = location;
            }, false);

            var downlocation;
            canvas.addEventListener(startEvtName, function (e) {
                isdown = true;


                if (device) {
                    downlocation = getLocation(e.changedTouches[0].clientX, e.changedTouches[0].clientY);
                } else {
                    downlocation = getLocation(e.clientX, e.clientY);
                }
                console.log(downlocation)
            }, false);
            canvas.addEventListener(endEvtName, function (e) {
                isdown = false;
                points[points.length] = { x: -1, y: -1 };
                console.log(points);
            }, false);
        }
        var points = [];
        function submit() {
            var ps = '';
            for (var i = 0; i < points.length; i++) {
                ps += points[i].x.toFixed(2) + ',' + points[i].y.toFixed(2) + ','
            }
            $.post('/Contract/UpLoadQM', 'PointList=' + ps, function (date) {


            });
        }

        function overwrite() {
            if (context) context.clearRect(0, 0, canvas.width, canvas.height);
            points = [];
        }

        function getLocation(x, y) {
            var bbox = canvas.getBoundingClientRect();
            return {
                x: (x - bbox.left) * (canvas.width / bbox.width),
                y: (y - bbox.top) * (canvas.height / bbox.height)
            };
        }

    </script>
</body>


</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值