您好,登錄后才能下訂單哦!
怎么在html5中使用canvas手寫簽名?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
window.onload = function() { new lineCanvas({ el: document.getElementById("canvas"),//繪制canvas的父級div clearEl: document.getElementById("clearCanvas"),//清除按鈕 saveEl: document.getElementById("saveCanvas"),//保存按鈕 // linewidth:1,//線條粗細(xì),選填 // 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); //結(jié)束繪制 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); //保存圖片,直接轉(zhuǎn)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; }
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。