canvas繪制簽名并保存圖片

預(yù)覽

canvas簽名

code

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>canvas簽名并保存圖片</title>
    <style>
    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;
    }
    </style>
</head>

<body>
    <div id="canvas">
        <p id="clearCanvas">清除</p>
        <p id="saveCanvas">保存</p>
    </div>
    <script>
    window.onload = function() {
        new lineCanvas({
            el: document.getElementById("canvas"), //繪制canvas的父級(jí)div
            clearEl: document.getElementById("clearCanvas"), //清除按鈕
            saveEl: document.getElementById("saveCanvas"), //保存按鈕
            //      linewidth:1,//線條粗細(xì),選填
            //      color:"black",//線條顏色,選填
            //      background:"#ffffff"http://線條背景,選填
        });
    };

    function lineCanvas(obj) {
        this.linewidth = 2;
        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);
    };
    </script>
</body>

</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容