国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > HTML5 > 正文

html5 利用canvas手寫簽名并保存的實現方法

2024-09-05 07:19:21
字體:
來源:轉載
供稿:網友

最近公司在做一個簽名的功能,主要用到了canvas畫線的功能結合移動端touch事件

js部分是這樣的:

window.onload = function() {    new lineCanvas({        el: document.getElementById("canvas"),//繪制canvas的父級div        clearEl: document.getElementById("clearCanvas"),//清除按鈕        saveEl: document.getElementById("saveCanvas"),//保存按鈕        //      linewidth:1,//線條粗細,選填        //      color:"black",//線條顏色,選填        //      background:"#ffffff"//線條背景,選填    });};function lineCanvas(obj) {    this.linewidth = 1;    this.color = "#000000";    this.background = "#ffffff";    for (var i in obj) {        this[i] = obj[i];    };    this.canvas = document.createElement("canvas");    this.el.appendChild(this.canvas);    this.cxt = this.canvas.getContext("2d");    this.canvas.width = this.el.clientWidth;    this.canvas.height = this.el.clientHeight;    this.cxt.fillStyle = this.background;    this.cxt.fillRect(0, 0, this.canvas.width, this.canvas.width);    this.cxt.strokeStyle = this.color;    this.cxt.lineWidth = this.linewidth;    this.cxt.lineCap = "round";    //開始繪制    this.canvas.addEventListener("touchstart", function(e) {        this.cxt.beginPath();        this.cxt.moveTo(e.changedTouches[0].pageX, e.changedTouches[0].pageY);    }.bind(this), false);    //繪制中    this.canvas.addEventListener("touchmove", function(e) {        this.cxt.lineTo(e.changedTouches[0].pageX, e.changedTouches[0].pageY);        this.cxt.stroke();    }.bind(this), false);    //結束繪制    this.canvas.addEventListener("touchend", function() {        this.cxt.closePath();    }.bind(this), false);    //清除畫布    this.clearEl.addEventListener("click", function() {        this.cxt.clearRect(0, 0, this.canvas.width, this.canvas.height);    }.bind(this), false);    //保存圖片,直接轉base64    this.saveEl.addEventListener("click", function() {        var imgBase64 = this.canvas.toDataURL();        console.log(imgBase64);    }.bind(this), false);};

這是效果圖:

附上html和css

<div id="canvas">    <p id="clearCanvas">清除</p>    <p id="saveCanvas">保存</p></div>html,body{    width: 100%;    height: 100%;}#canvas{    width: 100%;    height: 100%;    position: relative;}#canvas canvas{    display: block;}#clearCanvas{    width: 50%;    height: 40px;    line-height: 40px;    text-align: center;    position: absolute;    bottom: 0;    left: 0;    border: 1px solid #DEDEDE;    z-index: 1;}#saveCanvas{    width: 50%;    height: 40px;    line-height: 40px;    text-align: center;    position: absolute;    bottom: 0;    right: 0;    border: 1px solid #DEDEDE;    z-index: 1;}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乐东| 天祝| 乐东| 盐城市| 宁安市| 秭归县| 岫岩| 郑州市| 砀山县| 宜兰市| 高邮市| 海宁市| 车险| 竹溪县| 新绛县| 邵阳县| 临夏县| 九台市| 南京市| 绥棱县| 阳谷县| 赤壁市| 江西省| 沙河市| 缙云县| 永昌县| 邯郸县| 安宁市| 长乐市| 宝鸡市| 连城县| 上思县| 固原市| 绥滨县| 黄冈市| 山西省| 延川县| 白玉县| 涡阳县| 错那县| 甘泉县|