canvas在线签名插件Tablet

	js内容:
	//将base64转换为blob
	    function dataURLtoBlob(dataurl) {
	        var arr = dataurl.split(','),
	            mime = arr[0].match(/:(.*?);/)[1],
	            bstr = atob(arr[1]),
	            n = bstr.length,
	            u8arr = new Uint8Array(n);
	        while (n--) {
	            u8arr[n] = bstr.charCodeAt(n);
	        }
	        return new Blob([u8arr], { type: mime });
	    };
	    //将blob转换为file
	    function blobToFile(theBlob, fileName) {
	        theBlob.lastModifiedDate = new Date();
	        theBlob.name = fileName;
	        return theBlob;
	    };
	    var height = $window.innerHeight - 100;
	    $scope.load = function () {
        var tablet = new Tablet("#container", {
            defaultColor: "#000000",
            otherHtml: $("#temp").html(),
            onInit: function () {
                var that = this,
                    container = this.container;
                    //默认横屏
                     that.rotate("90");
                that.setLineWidth("3");
                container.find("select").eq(0).on("change", function () {
                    that.setLineWidth($(this).val());
                });
                container.find("select").eq(1).on("change", function () {
                    that.rotate($(this).val());
                });
                container.find(".save-canvas-to-img").on("click", function () {
                    if (that.points.x.length == 0) {
                        showAlert.showToast("请先编写签名");
                        return;
                    }
                    var imgPath = that.getBase64();

                    $http.post("../api/gadx/getEditUserAutograph",
                    {
                        userid: $scope.userid,
                        usertype: sessionStorage.usertype,
                        userautograph: imgPath,
                    }).then(function (resp) {
                        if (resp.data) {
                            showAlert.showToast("编辑成功");
                            that.clear();
                            $ionicHistory.goBack();
                        }
                    }, function (resp) {
                    }, function (evt) {
                        $scope.loaded = ((evt.loaded / evt.total) * 100).toFixed(1) + "%";
                    });
                });
                container.find(".get_blob").on("click", function () {
                    that.getBlob();
                });
            }
        });
    }
    $timeout(function () {
        $scope.load();
    });
   	 html内容:
   	 style
   	 @charset "utf-8";

/*Main container*/
.colpick {
    position: absolute;
    box-sizing: content-box;
    width: 346px;
    height: 170px;
    overflow: hidden;
    display: none;
    font-family: Arial, Helvetica, sans-serif;
    direction: ltr;
    background: #ebebeb;
    border: 1px solid #bbb;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    /*Prevents selecting text when dragging the selectors*/
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    -o-user-select: none;
    user-select: none;
}
/*Color selection box with gradients*/
.colpick_color {
    position: absolute;
    left: 7px;
    top: 7px;
    width: 156px;
    height: 156px;
    overflow: hidden;
    outline: 1px solid #aaa;
    cursor: crosshair;
}

.colpick_color_overlay1 {
    position: absolute;
    left: 0;
    top: 0;
    width: 156px;
    height: 156px;
    -ms-filter: "progid:DXImageTransform.Microsoft.gradient(GradientType=1,startColorstr='#ffffff', endColorstr='#00ffffff')"; /* IE8 */
    background: -moz-linear-gradient(left, rgba(255,255,255,1) 0%, rgba(255,255,255,0) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, right top, color-stop(0%,rgba(255,255,255,1)), color-stop(100%,rgba(255,255,255,0))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(left, rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(left, rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(left, rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%); /* IE10+ */
    background: linear-gradient(to right, rgba(255,255,255,1) 0%, rgba(255,255,255,0) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient(GradientType=1,startColorstr='#ffffff', endColorstr='#00ffffff'); /* IE6 & IE7 */
}

.colpick_color_overlay2 {
    position: absolute;
    left: 0;
    top: 0;
    width: 156px;
    height: 156px;
    -ms-filter: "progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr='#00000000', endColorstr='#000000')"; /* IE8 */
    background: -moz-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0)), color-stop(100%,rgba(0,0,0,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,1) 100%); /* IE10+ */
    background: linear-gradient(to bottom, rgba(0,0,0,0) 0%,rgba(0,0,0,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00000000', endColorstr='#000000',GradientType=0 ); /* IE6-9 */
}
/*Circular color selector*/
.colpick_selector_outer {
    background: none;
    position: absolute;
    width: 11px;
    height: 11px;
    margin: -6px 0 0 -6px;
    border: 1px solid black;
    border-radius: 50%;
}

.colpick_selector_inner {
    position: absolute;
    width: 9px;
    height: 9px;
    border: 1px solid white;
    border-radius: 50%;
}
/*Vertical hue bar*/
.colpick_hue {
    position: absolute;
    top: 6px;
    left: 175px;
    width: 19px;
    height: 156px;
    border: 1px solid #aaa;
    cursor: n-resize;
}
/*Hue bar sliding indicator*/
.colpick_hue_arrs {
    position: absolute;
    left: -8px;
    width: 35px;
    height: 7px;
    margin: -7px 0 0 0;
}

.colpick_hue_larr {
    position: absolute;
    width: 0;
    height: 0;
    border-top: 6px solid transparent;
    border-bottom: 6px solid transparent;
    border-left: 7px solid #858585;
}

.colpick_hue_rarr {
    position: absolute;
    right: 0;
    width: 0;
    height: 0;
    border-top: 6px solid transparent;
    border-bottom: 6px solid transparent;
    border-right: 7px solid #858585;
}
/*New color box*/
.colpick_new_color {
    position: absolute;
    left: 207px;
    top: 6px;
    width: 60px;
    height: 27px;
    background: #f00;
    border: 1px solid #8f8f8f;
}
/*Current color box*/
.colpick_current_color {
    position: absolute;
    left: 277px;
    top: 6px;
    width: 60px;
    height: 27px;
    background: #f00;
    border: 1px solid #8f8f8f;
}
/*Input field containers*/
.colpick_field, .colpick_hex_field {
    position: absolute;
    height: 20px;
    width: 60px;
    overflow: hidden;
    background: #f3f3f3;
    color: #b8b8b8;
    font-size: 12px;
    border: 1px solid #bdbdbd;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;
}

.colpick_rgb_r {
    top: 40px;
    left: 207px;
}

.colpick_rgb_g {
    top: 67px;
    left: 207px;
}

.colpick_rgb_b {
    top: 94px;
    left: 207px;
}

.colpick_hsb_h {
    top: 40px;
    left: 277px;
}

.colpick_hsb_s {
    top: 67px;
    left: 277px;
}

.colpick_hsb_b {
    top: 94px;
    left: 277px;
}

.colpick_hex_field {
    width: 68px;
    left: 207px;
    top: 121px;
}
/*Text field container on focus*/
.colpick_focus {
    border-color: #999;
}
/*Field label container*/
.colpick_field_letter {
    position: absolute;
    width: 12px;
    height: 20px;
    line-height: 20px;
    padding-left: 4px;
    background: #efefef;
    border-right: 1px solid #bdbdbd;
    font-weight: bold;
    color: #777;
}
/*Text inputs*/
.colpick_field input, .colpick_hex_field input {
    position: absolute;
    right: 11px;
    margin: 0;
    padding: 0;
    height: 20px;
    line-height: 20px;
    background: transparent;
    border: none;
    font-size: 12px;
    font-family: Arial, Helvetica, sans-serif;
    color: #555;
    text-align: right;
    outline: none;
}

.colpick_hex_field input {
    right: 4px;
}
/*Field up/down arrows*/
.colpick_field_arrs {
    position: absolute;
    top: 0;
    right: 0;
    width: 9px;
    height: 21px;
    cursor: n-resize;
}

.colpick_field_uarr {
    position: absolute;
    top: 5px;
    width: 0;
    height: 0;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-bottom: 4px solid #959595;
}

.colpick_field_darr {
    position: absolute;
    bottom: 5px;
    width: 0;
    height: 0;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-top: 4px solid #959595;
}
/*Submit/Select button*/
.colpick_submit {
    position: absolute;
    left: 207px;
    top: 149px;
    width: 130px;
    height: 22px;
    line-height: 22px;
    background: #efefef;
    text-align: center;
    color: #555;
    font-size: 12px;
    font-weight: bold;
    border: 1px solid #bdbdbd;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;
}

    .colpick_submit:hover {
        background: #f3f3f3;
        border-color: #999;
        cursor: pointer;
    }

/*full layout with no submit button*/
.colpick_full_ns .colpick_submit, .colpick_full_ns .colpick_current_color {
    display: none;
}

.colpick_full_ns .colpick_new_color {
    width: 130px;
    height: 25px;
}

.colpick_full_ns .colpick_rgb_r, .colpick_full_ns .colpick_hsb_h {
    top: 42px;
}

.colpick_full_ns .colpick_rgb_g, .colpick_full_ns .colpick_hsb_s {
    top: 73px;
}

.colpick_full_ns .colpick_rgb_b, .colpick_full_ns .colpick_hsb_b {
    top: 104px;
}

.colpick_full_ns .colpick_hex_field {
    top: 135px;
}

/*rgbhex layout*/
.colpick_rgbhex .colpick_hsb_h, .colpick_rgbhex .colpick_hsb_s, .colpick_rgbhex .colpick_hsb_b {
    display: none;
}

.colpick_rgbhex {
    width: 282px;
}

    .colpick_rgbhex .colpick_field, .colpick_rgbhex .colpick_submit {
        width: 68px;
    }

    .colpick_rgbhex .colpick_new_color {
        width: 34px;
        border-right: none;
    }

    .colpick_rgbhex .colpick_current_color {
        width: 34px;
        left: 240px;
        border-left: none;
    }

/*rgbhex layout, no submit button*/
.colpick_rgbhex_ns .colpick_submit, .colpick_rgbhex_ns .colpick_current_color {
    display: none;
}

.colpick_rgbhex_ns .colpick_new_color {
    width: 68px;
    border: 1px solid #8f8f8f;
}

.colpick_rgbhex_ns .colpick_rgb_r {
    top: 42px;
}

.colpick_rgbhex_ns .colpick_rgb_g {
    top: 73px;
}

.colpick_rgbhex_ns .colpick_rgb_b {
    top: 104px;
}

.colpick_rgbhex_ns .colpick_hex_field {
    top: 135px;
}

/*hex layout*/
.colpick_hex .colpick_hsb_h, .colpick_hex .colpick_hsb_s, .colpick_hex .colpick_hsb_b, .colpick_hex .colpick_rgb_r, .colpick_hex .colpick_rgb_g, .colpick_hex .colpick_rgb_b {
    display: none;
}

.colpick_hex {
    width: 206px;
    height: 201px;
}

    .colpick_hex .colpick_hex_field {
        width: 72px;
        height: 25px;
        top: 168px;
        left: 80px;
    }

        .colpick_hex .colpick_hex_field div, .colpick_hex .colpick_hex_field input {
            height: 25px;
            line-height: 25px;
        }

    .colpick_hex .colpick_new_color {
        left: 9px;
        top: 168px;
        width: 30px;
        border-right: none;
    }

    .colpick_hex .colpick_current_color {
        left: 39px;
        top: 168px;
        width: 30px;
        border-left: none;
    }

    .colpick_hex .colpick_submit {
        left: 164px;
        top: 168px;
        width: 30px;
        height: 25px;
        line-height: 25px;
    }

/*hex layout, no submit button*/
.colpick_hex_ns .colpick_submit, .colpick_hex_ns .colpick_current_color {
    display: none;
}

.colpick_hex_ns .colpick_hex_field {
    width: 80px;
}

.colpick_hex_ns .colpick_new_color {
    width: 60px;
    border: 1px solid #8f8f8f;
}

/*Dark color scheme*/
.colpick_dark {
    background: #161616;
    border-color: #2a2a2a;
}

    .colpick_dark .colpick_color {
        outline-color: #333;
    }

    .colpick_dark .colpick_hue {
        border-color: #555;
    }

    .colpick_dark .colpick_field, .colpick_dark .colpick_hex_field {
        background: #101010;
        border-color: #2d2d2d;
    }

    .colpick_dark .colpick_field_letter {
        background: #131313;
        border-color: #2d2d2d;
        color: #696969;
    }

    .colpick_dark .colpick_field input, .colpick_dark .colpick_hex_field input {
        color: #7a7a7a;
    }

    .colpick_dark .colpick_field_uarr {
        border-bottom-color: #696969;
    }

    .colpick_dark .colpick_field_darr {
        border-top-color: #696969;
    }

    .colpick_dark .colpick_focus {
        border-color: #444;
    }

    .colpick_dark .colpick_submit {
        background: #131313;
        border-color: #2d2d2d;
        color: #7a7a7a;
    }

        .colpick_dark .colpick_submit:hover {
            background-color: #101010;
            border-color: #444;
        }


/* 签名板 start */
.-tablet.flex {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

    .-tablet.flex .-tablet-container {
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -webkit-box-orient: vertical;
        -webkit-box-direction: normal;
        -ms-flex-direction: column;
        flex-direction: column;
        height: 100%;
    }

.tablet-btns {
    padding: 8px 0;
    background-color: #f5f5f5;
}

.color-picker-wrap {
    display: inline-block;
}

.-color-picker {
    display: inline-block !important;
    vertical-align: middle !important;
    width: 70px !important;
    outline: none !important;
    padding: 5px 0 !important;
    border: 1px solid #000;
    border-right-width: 25px !important;
    text-align: center !important;
    font-size: 14px !important;
    color: #000 !important;
    background-color: #fff !important;
    margin-right: 10px;
}

    .-color-picker.bg-color {
        border-color: transparent !important;
    }

.clear-canvas,
.save-canvas-to-img {
    display: inline-block;
    vertical-align: middle;
    padding: 5px 10px;
    font-size: 14px;
    color: #fff;
    cursor: pointer;
    border-radius: 5px;
}

.clear-canvas {
    background-color: #ef473a;
}

.save-canvas-to-img {
    background-color: #049ee8;
}

.-canvas-wrapper > canvas {
    cursor: crosshair;
    border: 2px dashed #ccc;
}
 .clear-canvas {
        margin-right: 10px;
        padding: 5px 15px !important;
        border-radius: 5px;
    }
    .-tablet.flex {
        position: relative;
    }

    .shortselect {
        background: #fafdfe;
        height: 25px; 
        line-height: 25px;
        border: 1px solid #9bc0dd;
        -moz-border-radius: 2px;
        -webkit-border-radius: 2px;
        border-radius: 2px;
    }  
/* 签名板 end */

	   	 <body>
			    <span style="padding :0px;" class="container" id="container"> </span>
			    <script type="text/html" id="temp">
			        <span>
			            <span style="display:none;">画笔粗细:</span>
			            <select class="shortselect" style="display:none;">
			                <option value="1">1</option>
			                <option value="3" selected>3</option>
			                <option value="5">5</option>
			                <option value="8">8</option>
			                <option value="10">10</option>
			                <option value="15">15</option>
			                <option value="20">20</option>
			            </select>
			            <span class="save-canvas-to-img" style="margin-left: 10px;">
			                保存
			            </span>
			            <select style="margin:5px 0 0 0" class="shortselect" data-tap-disabled="true">
			                <option value="0">正常</option>
			                <option value="90">顺时针旋转90</option>
			            </select>
			        </span>
			    </script>
			    <script src="../JS/Tablet-1.0.js"></script> 
		    </body>
		    
		配置项:
		    var tablet = new Tablet("#my_tablet", {
		        // 默认字体颜色
		        defaultColor: "#2e76da",
		        // 是否允许选择字体颜色
		        selectColor: true,
		        /* canvas画布是否响应式,默认为true。当设置为响应式后浏览器大小改变后会重新计算canvas画布的宽高,
		        并且之前绘制的内容会被清除掉(canvas的一个特性)*/
		        response: true,
		        // canvas的宽度,宽度可以传递函数。不传宽度默认为canvas的父元素的宽度
		        width: 0,
		        // canvas的宽度,高度可以传递函数。不传宽度默认为canvas的父元素的高度
		        height: 0,
		        // 签名板的额外class
		        extraClass: "",
		        // 清屏按钮的innerHTML
		        clearBtnHtml: "",
		        // 保存图片的innerHTML
		        saveBtnHtml: "",
		        // 工具栏中额外的html(可以实现一些自定义功能)
		        otherHtml: $("#temp").html(),
		        // tablet初始化后执行的函数(此时canvas上下文并未初始化)
		        onInit: function() {
		            var that = this,
		                container = this.container;
		            container.find("select").eq(0).on("change", function() {
		                that.setLineWidth($(this).val());
		            });
		            container.find("select").eq(1).on("change", function() {
		                that.rotate($(this).val());
		            });
		            container.find(".save-canvas-to-img").on("click", function() {
		                that.getBase64();
		            });
		            container.find(".get_blob").on("click", function() {
		                that.getBlob();
		            });
		            /*container.find(".download").on("click", function (){
		                document.getElementById("preview_img").src = that.getBase64();
		            });*/
		        },
		        // 清除画布前执行的函数,如果该函数返回false,则不会进行清除
		        onBeforeClear: function() {},
		        // 清除画布后执行的函数
		        onClear: function() {}
		    });
			实例方法
				clear
			
			clear()方法用于清空画布。返回值为当前Tablet实例
			
			getBase64
			
			getBase64(type)获取画布的base64数据,拿到的是base64字符串。 type参数为图片类型,可选值有jpg、png,默认png
			
			getBlob
			
			getBlob(type)获取画布的Blob数据(二进制数据),返回的是Blob对象。(内部实现:先获取base64字符串,再转换成Blob对象) type参数为图片类型,可选值有jpg、png,默认png
			
			canvasReset
			
			canvasReset()该方法可以用来重置canvas画布的属性。重置只会重置canvas的lineWidth、strokeStyle、lineCap、lineJoin、shadowBlur、shadowColor属性
			
			rotate
			
			rotate(degree)旋转画布。degree为旋转的角度,可选角度为90-90180-180
			
			setCanvasWH
			
			setCanvasWH(width, height)设置签名板的宽高。如果不传递宽高、或只传递了宽或高则会使用canvas的父容器的宽高。在移动端中签名板的宽高默认为window的宽高,这样做是为了在移动端中更好的进行写字。
			
			注意:手动设置签名板的宽高(或旋转画布)后canvas之前绘制的内容会被清除掉。
			
			setColor
			
			setColor(color)设置canvas画笔的颜色。color为画笔颜色,默认为:#000。它的值可以为css表达颜色的值
			
			setLineWidth
			
			setLineWidth(width)设置canvas画笔的粗细。width为画笔粗细,pc端默认为8,移动端根据屏幕大小自动判断
			
			实例属性
			id
			
			该id为当前签名板元素的id,可通过该id获取到当前签名板
			
			$canvas
			
			通过jQuery获取到的当前签名板的canvas元素
			
			canvas
			
			当前签名板的canvas dom元素
			
			ctx
			
			当前签名板的canvas的上下文
			
			point
			
			签名板最后一次绘制时的坐标

在这里插入图片描述

这里是引用
Tablet引用地址 http://www.jq22.com/jquery-info17341

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值